Commit f53b130d authored by wanglei's avatar wanglei

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

parent 501101fb
...@@ -10,6 +10,7 @@ import android.content.ComponentName ...@@ -10,6 +10,7 @@ import android.content.ComponentName
import android.content.Context import android.content.Context
import android.content.Intent import android.content.Intent
import android.content.pm.ServiceInfo import android.content.pm.ServiceInfo
import android.location.Location
import android.os.Build import android.os.Build
import android.os.CountDownTimer import android.os.CountDownTimer
import android.os.Looper import android.os.Looper
...@@ -22,6 +23,13 @@ import com.google.android.gms.location.LocationCallback ...@@ -22,6 +23,13 @@ import com.google.android.gms.location.LocationCallback
import com.google.android.gms.location.LocationRequest import com.google.android.gms.location.LocationRequest
import com.google.android.gms.location.LocationResult import com.google.android.gms.location.LocationResult
import com.google.android.gms.location.LocationServices 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 import kotlin.random.Random
...@@ -40,8 +48,17 @@ class StayJobService : JobService() { ...@@ -40,8 +48,17 @@ class StayJobService : JobService() {
companion object { companion object {
val ACTION_LOCATION_UPDATES = "ACTION_REQUEST_LOCATION_UPDATES" val ACTION_LOCATION_UPDATES = "ACTION_REQUEST_LOCATION_UPDATES"
private val NOTIFICATION_PERMANENT_ID = 186 private val NOTIFICATION_PERMANENT_ID = 186
var googleLocation: LatLng? = null
//位置更新回调
private var locationCallback: LocationCallback? = null private var locationCallback: LocationCallback? = null
//位置的一次性回调
var oneShotLocationCallBack: (() -> Unit)? = null
//更新频率
var getGoogleServiceLocationDelay: Int = 60
fun Context.requestServiceLocationUpdates() { fun Context.requestServiceLocationUpdates() {
startService(Intent(this, StayJobService::class.java).apply { action = ACTION_LOCATION_UPDATES }) startService(Intent(this, StayJobService::class.java).apply { action = ACTION_LOCATION_UPDATES })
} }
...@@ -99,6 +116,9 @@ class StayJobService : JobService() { ...@@ -99,6 +116,9 @@ class StayJobService : JobService() {
override fun onDestroy() { override fun onDestroy() {
isRunning = false isRunning = false
cancelLocationUpdates() cancelLocationUpdates()
getGoogleServiceJob?.cancel()
getGoogleServiceJob = null
super.onDestroy() super.onDestroy()
} }
...@@ -162,7 +182,7 @@ class StayJobService : JobService() { ...@@ -162,7 +182,7 @@ class StayJobService : JobService() {
// 请求位置更新 // 请求位置更新
fusedLocationClient.requestLocationUpdates(locationRequest, it, Looper.getMainLooper()) fusedLocationClient.requestLocationUpdates(locationRequest, it, Looper.getMainLooper())
} }
getGoogleServiceLocation()
} }
...@@ -175,5 +195,33 @@ class StayJobService : JobService() { ...@@ -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 ...@@ -20,6 +20,7 @@ import com.base.locationsharewhite.location.LocationPositionUtils
import com.base.locationsharewhite.location.LocationShareListUtils import com.base.locationsharewhite.location.LocationShareListUtils
import com.base.locationsharewhite.map.MapUtils.addLocationMarker import com.base.locationsharewhite.map.MapUtils.addLocationMarker
import com.base.locationsharewhite.map.MapUtils.getLatLngByLocationManager 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.service.StayJobService.Companion.requestServiceLocationUpdates
import com.base.locationsharewhite.ui.set.RenameActivity import com.base.locationsharewhite.ui.set.RenameActivity
import com.base.locationsharewhite.ui.set.RenameActivity.Companion.RENAME_VIEWING_NICK_NAME import com.base.locationsharewhite.ui.set.RenameActivity.Companion.RENAME_VIEWING_NICK_NAME
...@@ -270,8 +271,7 @@ class LocationMapActivity : BaseActivity<ActivityLocationMapBinding>(), OnMapRea ...@@ -270,8 +271,7 @@ class LocationMapActivity : BaseActivity<ActivityLocationMapBinding>(), OnMapRea
val flag = changeMyLocation(locationPresenter.getMyLocation(), true, true, true) val flag = changeMyLocation(locationPresenter.getMyLocation(), true, true, true)
if (!flag) { if (!flag) {
locationPresenter.getGoogleServiceLocation() StayJobService.oneShotLocationCallBack = {
locationPresenter.oneShotLocationCallBack = {
changeMyLocation(locationPresenter.getMyLocation(), true, true, true) changeMyLocation(locationPresenter.getMyLocation(), true, true, true)
} }
} }
...@@ -322,7 +322,7 @@ class LocationMapActivity : BaseActivity<ActivityLocationMapBinding>(), OnMapRea ...@@ -322,7 +322,7 @@ class LocationMapActivity : BaseActivity<ActivityLocationMapBinding>(), OnMapRea
if (myMarker == null) { if (myMarker == null) {
var myLatLng: LatLng? = getLatLngByLocationManager() var myLatLng: LatLng? = getLatLngByLocationManager()
if (myLatLng == null) { if (myLatLng == null) {
myLatLng = locationPresenter.googleLocation myLatLng = StayJobService.googleLocation
} }
myLatLng?.let { myLatLng?.let {
LogEx.logDebug(TAG, "getLastKnowLocation ${myLatLng.latitude} ${myLatLng.longitude}") LogEx.logDebug(TAG, "getLastKnowLocation ${myLatLng.latitude} ${myLatLng.longitude}")
...@@ -404,6 +404,7 @@ class LocationMapActivity : BaseActivity<ActivityLocationMapBinding>(), OnMapRea ...@@ -404,6 +404,7 @@ class LocationMapActivity : BaseActivity<ActivityLocationMapBinding>(), OnMapRea
override fun onPause() { override fun onPause() {
super.onPause() super.onPause()
locationPresenter.cancelUploadJob() locationPresenter.cancelUploadJob()
StayJobService.getGoogleServiceLocationDelay = 60
} }
override fun onResume() { override fun onResume() {
...@@ -415,6 +416,7 @@ class LocationMapActivity : BaseActivity<ActivityLocationMapBinding>(), OnMapRea ...@@ -415,6 +416,7 @@ class LocationMapActivity : BaseActivity<ActivityLocationMapBinding>(), OnMapRea
if (isRecreate) { if (isRecreate) {
return return
} }
StayJobService.getGoogleServiceLocationDelay = 30
if (!checkLocationPermission() || !checkBackgroundLocationPermission()) { if (!checkLocationPermission() || !checkBackgroundLocationPermission()) {
binding.ivPermission.visibility = View.VISIBLE binding.ivPermission.visibility = View.VISIBLE
......
...@@ -7,6 +7,8 @@ import android.os.Looper ...@@ -7,6 +7,8 @@ import android.os.Looper
import androidx.lifecycle.LifecycleCoroutineScope import androidx.lifecycle.LifecycleCoroutineScope
import com.base.locationsharewhite.location.LocationPositionUtils.uploadMyLocation import com.base.locationsharewhite.location.LocationPositionUtils.uploadMyLocation
import com.base.locationsharewhite.map.MapUtils.getLatLngByLocationManager 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.service.StayJobService.Companion.requestServiceLocationUpdates
import com.base.locationsharewhite.utils.BatteryUtils.getBatteryLevel import com.base.locationsharewhite.utils.BatteryUtils.getBatteryLevel
import com.base.locationsharewhite.utils.LogEx import com.base.locationsharewhite.utils.LogEx
...@@ -30,12 +32,6 @@ class LocationPresenter( ...@@ -30,12 +32,6 @@ class LocationPresenter(
private var TAG = "LocationPresenter" private var TAG = "LocationPresenter"
private var uploadJob: Job? = null 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 var refreshLocationCallBack: ((latLng: LatLng?) -> Unit)? = null
...@@ -43,7 +39,7 @@ class LocationPresenter( ...@@ -43,7 +39,7 @@ class LocationPresenter(
fun getMyLocation(): LatLng? { fun getMyLocation(): LatLng? {
var latLng = activity.getLatLngByLocationManager() var latLng = activity.getLatLngByLocationManager()
if (latLng == null) { if (latLng == null) {
latLng = googleLocation latLng = StayJobService.googleLocation
} }
return latLng return latLng
} }
...@@ -52,7 +48,6 @@ class LocationPresenter( ...@@ -52,7 +48,6 @@ class LocationPresenter(
if (uploadJob == null) { if (uploadJob == null) {
uploadJob = lifecycleCoroutineScope.launch(Dispatchers.IO) { uploadJob = lifecycleCoroutineScope.launch(Dispatchers.IO) {
while (isActive) { while (isActive) {
getGoogleServiceLocation()
val latLng = getMyLocation() val latLng = getMyLocation()
...@@ -69,22 +64,6 @@ class LocationPresenter( ...@@ -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() { fun cancelUploadJob() {
uploadJob?.cancel() uploadJob?.cancel()
......
...@@ -6,6 +6,7 @@ import android.location.Location ...@@ -6,6 +6,7 @@ import android.location.Location
import android.os.Looper import android.os.Looper
import androidx.lifecycle.LifecycleCoroutineScope import androidx.lifecycle.LifecycleCoroutineScope
import com.base.locationsharewhite.map.MapUtils.getLatLngByLocationManager import com.base.locationsharewhite.map.MapUtils.getLatLngByLocationManager
import com.base.locationsharewhite.service.StayJobService
import com.base.locationsharewhite.utils.LogEx import com.base.locationsharewhite.utils.LogEx
import com.google.android.gms.location.FusedLocationProviderClient import com.google.android.gms.location.FusedLocationProviderClient
import com.google.android.gms.location.LocationCallback import com.google.android.gms.location.LocationCallback
...@@ -26,22 +27,6 @@ class MainPresenter( ...@@ -26,22 +27,6 @@ class MainPresenter(
private val TAG = "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( ...@@ -50,7 +35,7 @@ class MainPresenter(
var latLng = activity.getLatLngByLocationManager() var latLng = activity.getLatLngByLocationManager()
LogEx.logDebug(TAG, "getMyLocation latLng=$latLng") LogEx.logDebug(TAG, "getMyLocation latLng=$latLng")
if (latLng == null) { if (latLng == null) {
latLng = googleLocation latLng = StayJobService.googleLocation
} }
return latLng return latLng
} }
...@@ -64,7 +49,6 @@ class MainPresenter( ...@@ -64,7 +49,6 @@ class MainPresenter(
locationJob = lifecycleCoroutineScope.launch(Dispatchers.IO) { locationJob = lifecycleCoroutineScope.launch(Dispatchers.IO) {
while (isActive) { while (isActive) {
getGoogleServiceLocation()
val latLng = getMyLocation() val latLng = getMyLocation()
launch(Dispatchers.Main) { 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