提问者:小点点

前台服务在下班后停止工作


我用Kotlin做了一个前台服务。 它的工作,但在运行了七个小时后,我的服务停止和我的应用程序返回到它的第一页(登录页)。 但是停止我的服务的唯一方法是在我点击“停止服务”按钮时执行的,那么如果我没有按任何按钮,为什么我的服务会在7小时后停止呢? 我用的是moto g7,安卓9.0

class RastreioService : Service() {

companion object {
    var serviceAtivado = false //service activated
}
override fun onBind(intent: Intent?): IBinder? {
    TODO("Not yet implemented")
}

override fun onCreate() {
    super.onCreate()
    serviceAtivado = true

    val notificationIntent = Intent(this, RastreioService::class.java)
    val pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);

    val notification = Notification.Builder(this, "1")
        .setSmallIcon(R.drawable.ic_gps_ativo)
        .setContentTitle("Localização Sendo Acessada")
        .setContentText("A sua localização está sendo acessada")
        .setContentIntent(pendingIntent)
        .build()

    startForeground(1, notification)

    /*request location methods*/
}

override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {

    return START_NOT_STICKY
}

override fun onDestroy() {
    serviceAtivado = false
    super.onDestroy()
    this.gerenciadorDeLocalizacao.DesativarBuscaPorLocalizaca()
}

}

共1个答案

匿名用户

前台服务仍然可以被操作系统杀死,您不能保证该服务将永远持续下去。 前台服务只会降低它被杀死的可能性