安卓Kotlin前台服务在一段时间后停止



我正在为用户跟踪开发前台定位服务。每次位置更新时,此服务都会向API发送更新当前位置的请求。问题是,当应用程序被放在后台或屏幕被锁定时,服务会在一段时间后停止发送请求(通常在1分钟左右,在此期间会发送大约10个请求(。在应用程序恢复后,服务再次开始工作,并且在最小化/锁定屏幕后,场景重复。

onStartCommand中,我尝试返回多个启动选项,但都不起作用。我已经在安卓10和11上测试了这个应用程序。

服务源代码:

class LocationService: Service()  {
@Inject
lateinit var apiService: ApiService
private val composite = CompositeDisposable()
private var locationManager: LocationManager? = null
private var locationListener: LocationListener? = null
override fun onBind(p0: Intent?): IBinder? =
null
val NOTIFICATION_CHANNEL_ID = "location_tracking"
val NOTIFICATION_CHANNEL_NAME = "Location tracking"
val NOTIFICATION_ID = 101
var isFirstRun = true
@SuppressLint("MissingPermission")
override fun onCreate() {
App.component.inject(this)
setupLocationListener()
locationManager = getSystemService(LOCATION_SERVICE) as LocationManager?
val criteria = Criteria()
criteria.accuracy = Criteria.ACCURACY_FINE
val provider = locationManager?.getBestProvider(criteria, true)
val minTime = 5*1000L
if(provider != null) locationManager?.requestLocationUpdates(provider, minTime, 0f, locationListener)
super.onCreate()
}
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
if (isFirstRun) {
startForegroundService()
isFirstRun = false
} else {
Timber.d {"Resuming service"}
}
return super.onStartCommand(intent, flags, startId)
}
private fun startForegroundService() {
val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) createNotificationChannel(notificationManager)
val notificationBuilder = NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID)
.setAutoCancel(false)
.setOngoing(true)
.setSmallIcon(R.drawable.ic_notification)
.setContentTitle(NOTIFICATION_CHANNEL_NAME)
.setContentIntent(getMainActivityIntent())
startForeground(NOTIFICATION_ID, notificationBuilder.build())
}
private fun getMainActivityIntent()
= PendingIntent.getActivity(this, 0, Intent(this, MainActivity::class.java)
.also { it.action = R.id.action_global_navigationScreenFragment.toString() }, FLAG_UPDATE_CURRENT)
@RequiresApi(Build.VERSION_CODES.O)
private fun createNotificationChannel(notificationManager: NotificationManager) {
val channel = NotificationChannel(NOTIFICATION_CHANNEL_ID, NOTIFICATION_CHANNEL_NAME, IMPORTANCE_LOW)
notificationManager.createNotificationChannel(channel)
}
private fun setupLocationListener() {
locationListener = object: LocationListener {
override fun onLocationChanged(location: Location) {
val cords = GeoCoordinatesDTO(location.latitude.toFloat(), location.longitude.toFloat())
try {
composite.add(apiService.mobileUserAccountReportPosition(cords)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(
{},
{ t ->
if(t is RuntimeException) {
e(t)
}
}
))
} catch(e: Exception) {
Log.e("GPS", "error: $e")
}
}
override fun onStatusChanged(provider: String, status: Int, extras: Bundle) {}
override fun onProviderEnabled(provider: String) {}
override fun onProviderDisabled(provider: String) {}
}
}
override fun onDestroy() {
try {
locationManager?.removeUpdates(locationListener)
} catch(e: DeadObjectException) {}
super.onDestroy()
}
}

服务从MainActivity中的onStart功能启动

private fun initializeLocationMonitor() {
locationService = Intent(this, LocationService::class.java)
if(!this.isServiceRunning(LocationService::class.java)) {
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
startForegroundService(locationService)
} else {
startService(locationService)
}
sendBroadcast(locationService)
}
}

我在清单中有以下权限,并注册了服务:

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<service
android:name=".Services.LocationService"
android:enabled="true"
android:exported="true"
tools:ignore="ExportedService,InnerclassSeparator"
android:foregroundServiceType="location"/>
  1. 保持设备清醒-https://developer.android.com/training/scheduling/wakelock
// global service variable
private var wakeLock: PowerManager.WakeLock? = null
...
// initialize before setupLocationListener() is called
wakeLock =
(getSystemService(Context.POWER_SERVICE) as PowerManager).run {
newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "LocationService::lock").apply {
acquire(10*1000L)  // 10 seconds
}
}

清单:

<uses-permission android:name="android.permission.WAKE_LOCK" />
  1. 检查您的应用程序是否在应用程序设置中限制了数据使用和电池服务器

如果您的最小sdk为14+,则最好使用WorkManager。它将使您省去使用和管理前台服务的许多麻烦。

https://developer.android.com/topic/libraries/architecture/workmanager

发送位置更新是WorkManager的最佳用例之一。