设备进入睡眠状态时,前台服务将停止工作

问题描述

我有一个前台服务,使用LocationManager和Geofences跟踪某些设备。 我注意到,当我在Moto g7中运行服务时,在3或4个小时后,我的服务停止了,这种情况发生时,如果我按下设备的电源按钮,我的设备就会“唤醒”,这会使我的服务恢复运行! 我想要一个无限的前台服务,并且我的服务具有唤醒锁,这不应该使我的服务在手机休眠时不会停止吗?

class myService : Service() {

    override fun onBind(intent: Intent?): IBinder? {
        Todo("Not yet implemented")
    }


    override fun onStartCommand(intent: Intent?,flags: Int,startId: Int): Int {
        wakeLock =
        (getSystemService(Context.POWER_SERVICE) as PowerManager).run {
            newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,"myService::lock").apply {
                acquire()
            }
        }
        startForegroundService()
        /*business rules*/
        return START_STICKY
    }
    
    override fun onDestroy() {
        super.onDestroy()
        wakeLock?.let {
            if (it.isHeld) {
                it.release()
            }
        }
    }


    private fun startForegroundService(){
        var notification = createNotification()
        val idService = 1
        startForeground(idService,notification)
    }

    private fun createNotification(): Notification {
        val notificationIntent = Intent(this,RastreioService::class.java)
        val pendingIntent = PendingIntent.getActivity(this,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()

        return notification
    }


}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...