Commit f53b130d authored by wanglei's avatar wanglei

...移动位置获取代码到常驻服务

parent 501101fb
......@@ -10,6 +10,7 @@ import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.content.pm.ServiceInfo
import android.location.Location
import android.os.Build
import android.os.CountDownTimer
import android.os.Looper
......@@ -22,6 +23,13 @@ import com.google.android.gms.location.LocationCallback
import com.google.android.gms.location.LocationRequest
import com.google.android.gms.location.LocationResult
import com.google.android.gms.location.LocationServices
import com.google.android.gms.maps.model.LatLng
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.MainScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.isActive
import kotlinx.coroutines.launch
import kotlin.random.Random
......@@ -40,8 +48,17 @@ class StayJobService : JobService() {
companion object {
val ACTION_LOCATION_UPDATES = "ACTION_REQUEST_LOCATION_UPDATES"
private val NOTIFICATION_PERMANENT_ID = 186
var googleLocation: LatLng? = null
//位置更新回调
private var locationCallback: LocationCallback? = null
//位置的一次性回调
var oneShotLocationCallBack: (() -> Unit)? = null
//更新频率
var getGoogleServiceLocationDelay: Int = 60
fun Context.requestServiceLocationUpdates() {
startService(Intent(this, StayJobService::class.java).apply { action = ACTION_LOCATION_UPDATES })
}
......@@ -99,6 +116,9 @@ class StayJobService : JobService() {
override fun onDestroy() {
isRunning = false
cancelLocationUpdates()
getGoogleServiceJob?.cancel()
getGoogleServiceJob = null
super.onDestroy()
}
......@@ -162,7 +182,7 @@ class StayJobService : JobService() {
// 请求位置更新
fusedLocationClient.requestLocationUpdates(locationRequest, it, Looper.getMainLooper())
}
getGoogleServiceLocation()
}
......@@ -175,5 +195,33 @@ class StayJobService : JobService() {
}
}
private var getGoogleServiceJob: Job? = null
@SuppressLint("MissingPermission")
fun getGoogleServiceLocation() {
if (getGoogleServiceJob == null) {
getGoogleServiceJob = MainScope().launch(Dispatchers.IO) {
while (isActive) {
fusedLocationClient.lastLocation
.addOnSuccessListener { location: Location? ->
if (location != null) {
val latLng = LatLng(location.latitude, location.longitude)
googleLocation = latLng
LogEx.logDebug(TAG, "getGoogleServiceLocation latLng=$latLng")
oneShotLocationCallBack?.invoke()
oneShotLocationCallBack = null
}
}.addOnFailureListener { e ->
LogEx.logDebug(TAG, "e=$e")
}
delay(getGoogleServiceLocationDelay * 1000L)
}
}
}
}
}
......@@ -20,6 +20,7 @@ import com.base.locationsharewhite.location.LocationPositionUtils
import com.base.locationsharewhite.location.LocationShareListUtils
import com.base.locationsharewhite.map.MapUtils.addLocationMarker
import com.base.locationsharewhite.map.MapUtils.getLatLngByLocationManager
import com.base.locationsharewhite.service.StayJobService
import com.base.locationsharewhite.service.StayJobService.Companion.requestServiceLocationUpdates
import com.base.locationsharewhite.ui.set.RenameActivity
import com.base.locationsharewhite.ui.set.RenameActivity.Companion.RENAME_VIEWING_NICK_NAME
......@@ -270,8 +271,7 @@ class LocationMapActivity : BaseActivity<ActivityLocationMapBinding>(), OnMapRea
val flag = changeMyLocation(locationPresenter.getMyLocation(), true, true, true)
if (!flag) {
locationPresenter.getGoogleServiceLocation()
locationPresenter.oneShotLocationCallBack = {
StayJobService.oneShotLocationCallBack = {
changeMyLocation(locationPresenter.getMyLocation(), true, true, true)
}
}
......@@ -322,7 +322,7 @@ class LocationMapActivity : BaseActivity<ActivityLocationMapBinding>(), OnMapRea
if (myMarker == null) {
var myLatLng: LatLng? = getLatLngByLocationManager()
if (myLatLng == null) {
myLatLng = locationPresenter.googleLocation
myLatLng = StayJobService.googleLocation
}
myLatLng?.let {
LogEx.logDebug(TAG, "getLastKnowLocation ${myLatLng.latitude} ${myLatLng.longitude}")
......@@ -404,6 +404,7 @@ class LocationMapActivity : BaseActivity<ActivityLocationMapBinding>(), OnMapRea
override fun onPause() {
super.onPause()
locationPresenter.cancelUploadJob()
StayJobService.getGoogleServiceLocationDelay = 60
}
override fun onResume() {
......@@ -415,6 +416,7 @@ class LocationMapActivity : BaseActivity<ActivityLocationMapBinding>(), OnMapRea
if (isRecreate) {
return
}
StayJobService.getGoogleServiceLocationDelay = 30
if (!checkLocationPermission() || !checkBackgroundLocationPermission()) {
binding.ivPermission.visibility = View.VISIBLE
......
......@@ -7,6 +7,8 @@ import android.os.Looper
import androidx.lifecycle.LifecycleCoroutineScope
import com.base.locationsharewhite.location.LocationPositionUtils.uploadMyLocation
import com.base.locationsharewhite.map.MapUtils.getLatLngByLocationManager
import com.base.locationsharewhite.service.StayJobService
import com.base.locationsharewhite.service.StayJobService.Companion.oneShotLocationCallBack
import com.base.locationsharewhite.service.StayJobService.Companion.requestServiceLocationUpdates
import com.base.locationsharewhite.utils.BatteryUtils.getBatteryLevel
import com.base.locationsharewhite.utils.LogEx
......@@ -30,12 +32,6 @@ class LocationPresenter(
private var TAG = "LocationPresenter"
private var uploadJob: Job? = null
//google的地理位置服务
private var fusedLocationClient: FusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(activity)
var googleLocation: LatLng? = null
//一次性更新自己的位置
var oneShotLocationCallBack: (() -> Unit)? = null
//刷新自己的位置
var refreshLocationCallBack: ((latLng: LatLng?) -> Unit)? = null
......@@ -43,7 +39,7 @@ class LocationPresenter(
fun getMyLocation(): LatLng? {
var latLng = activity.getLatLngByLocationManager()
if (latLng == null) {
latLng = googleLocation
latLng = StayJobService.googleLocation
}
return latLng
}
......@@ -52,7 +48,6 @@ class LocationPresenter(
if (uploadJob == null) {
uploadJob = lifecycleCoroutineScope.launch(Dispatchers.IO) {
while (isActive) {
getGoogleServiceLocation()
val latLng = getMyLocation()
......@@ -69,22 +64,6 @@ class LocationPresenter(
}
}
@SuppressLint("MissingPermission")
fun getGoogleServiceLocation() {
fusedLocationClient.lastLocation
.addOnSuccessListener { location: Location? ->
if (location != null) {
val latLng = LatLng(location.latitude, location.longitude)
googleLocation = latLng
oneShotLocationCallBack?.invoke()
oneShotLocationCallBack = null
}
}.addOnFailureListener { e ->
LogEx.logDebug(TAG, "e=$e")
}
}
fun cancelUploadJob() {
uploadJob?.cancel()
......
......@@ -6,6 +6,7 @@ import android.location.Location
import android.os.Looper
import androidx.lifecycle.LifecycleCoroutineScope
import com.base.locationsharewhite.map.MapUtils.getLatLngByLocationManager
import com.base.locationsharewhite.service.StayJobService
import com.base.locationsharewhite.utils.LogEx
import com.google.android.gms.location.FusedLocationProviderClient
import com.google.android.gms.location.LocationCallback
......@@ -26,22 +27,6 @@ class MainPresenter(
private val TAG = "MainPresenter"
//google的地理位置服务
private var fusedLocationClient: FusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(activity)
private var googleLocation: LatLng? = null
@SuppressLint("MissingPermission")
private fun getGoogleServiceLocation() {
fusedLocationClient.lastLocation
.addOnSuccessListener { location: Location? ->
LogEx.logDebug(TAG, "getGoogleServiceLocation location=$location")
if (location != null) {
val latLng = LatLng(location.latitude, location.longitude)
googleLocation = latLng
LogEx.logDebug(TAG, "getGoogleServiceLocation latLng=$latLng")
}
}
}
/**
* 获取自身位置,
......@@ -50,7 +35,7 @@ class MainPresenter(
var latLng = activity.getLatLngByLocationManager()
LogEx.logDebug(TAG, "getMyLocation latLng=$latLng")
if (latLng == null) {
latLng = googleLocation
latLng = StayJobService.googleLocation
}
return latLng
}
......@@ -64,7 +49,6 @@ class MainPresenter(
locationJob = lifecycleCoroutineScope.launch(Dispatchers.IO) {
while (isActive) {
getGoogleServiceLocation()
val latLng = getMyLocation()
launch(Dispatchers.Main) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment