Android 服务不更新 GPS
温馨提示:
本文最后更新于 2024年04月12日,已超过 37 天没有更新。若文章内的图片失效(无法正常加载),请留言反馈或直接联系我。
the above code does not update the gps from service the log cat only shows one update.
I need to update fused location every 5 seconds and As per Service class the onLocationChanged should called every 5 seconds but it never calls again even my GPS turn on almost every 5 seconds and then turn off after while but still there is no Location Update in Logcat.I need service class to keep update locatio.
type herclass TrackingService : LifecycleService() {
private var fusedLocationProviderClient: FusedLocationProviderClient? = null
var locationCallback: LocationCallback? = null
var lat = 0.0
var lng = 0.0
@Inject
lateinit var repository: TrackingRepository
@Inject
lateinit var gpsRepository: GpsRepository
@Inject
lateinit var userPreferences: UserPreferences
private lateinit var connectivityJob: Job
private var isConnected = false
private lateinit var connectivityManager: ConnectivityManager
override fun onBind(intent: Intent): IBinder? {
super.onBind(intent)
return null
}
override fun onCreate() {
super.onCreate()
fusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(this)
locationCallback = object : LocationCallback() {
override fun onLocationResult(locationResult: LocationResult) {
super.onLocationResult(locationResult)
if (isConnected) {
lifecycle.coroutineScope.launch {
lat = locationResult.lastLocation!!.latitude
lng = locationResult.lastLocation!!.longitude
val latitude = lat.toString()
val longitude = lng.toString()
gpsRepository.gps.collect {
if (it.isNotEmpty()) {
Log.e("gps", it.toString())
Log.e("connection", "Internet connected")
// gpsRepository.deleteGps()
} else {
Log.e("posting", it.toString())
userPreferences.getDispatchId().collect { dispatchId ->
repository.getGps(dispatchId, latitude, longitude)
}
}
}
}
} else {
Log.e("connection", "No Internet")
lifecycle.coroutineScope.launch {
lat = locationResult.lastLocation!!.latitude
lng = locationResult.lastLocation!!.longitude
val latitude = lat.toString()
val longitude = lng.toString()
val gps = Gps(latitude, longitude)
gpsRepository.addGps(gps)
gpsRepository.gps.collect {
val coodList = JSONArray()
for (item in it) {
val assoc = JSONObject()
assoc.put("latitude", item.lat)
assoc.put("longitude", item.lon)
coodList.put(assoc)
}
val jsonObject = JSONObject()
jsonObject.put("coordinates", coodList)
Log.e("over", jsonObject.toString())
Log.e("connection", "No Internet")
}
}
}
Log.e(
"Gps ", "lat is" + locationResult.lastLocation!!.latitude
+ "lng is" + locationResult.lastLocation!!.longitude
)
}
}
}
@SuppressLint("MissingSuperCall")
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
connectivityManager = getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
CoroutineScope(Dispatchers.Default).launch {
checkInternetConnection()
}
requestLocation();
return START_STICKY
}
private suspend fun checkInternetConnection() {
while (true) {
val activeNetworkInfo = connectivityManager.activeNetworkInfo
val isConnectedNow = activeNetworkInfo != null && activeNetworkInfo.isConnected
if (isConnectedNow != isConnected) {
isConnected = isConnectedNow
// Handle internet connectivity change
withContext(Dispatchers.Main) {
if (isConnected) {
// Internet is connected
// Perform necessary actions
} else {
// Internet is disconnected
// Perform necessary actions
}
}
}
delay(CHECK_INTERVAL) // Wait for some time before checking again
}
}
@SuppressLint("MissingPermission")
private fun requestLocation() {
val locationRequest = LocationRequest()
locationRequest.interval = 7000
locationRequest.fastestInterval = 6000
locationRequest.priority = LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY
if (ActivityCompat.checkSelfPermission(
this,
Manifest.permission.ACCESS_FINE_LOCATION
) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(
this,
Manifest.permission.ACCESS_COARSE_LOCATION
) != PackageManager.PERMISSION_GRANTED
) {
return
}
fusedLocationProviderClient!!.requestLocationUpdates(
locationRequest,
locationCallback!!, Looper.myLooper()!!
)
}
override fun onDestroy() {
super.onDestroy()
}
companion object {
private const val CHECK_INTERVAL = 2000L // Check every 5 seconds
}
}
正文到此结束
- 本文标签: 家庭宠物
- 本文链接: https://www.coder6.net/article/2176
- 版权声明: 本文由蚂蚁原创发布,转载请遵循《署名-非商业性使用-相同方式共享 4.0 国际 (CC BY-NC-SA 4.0)》许可协议授权