我正在开发一款应用程序,我在该应用程序中启动计数很长一段时间,为此我启动了前台服务,该服务在所有设备中都运行良好,只有少数设备除外,如android 10 vivo funtouch Os。在这种情况下,当你刷通知时,它会杀死放弃的rund服务,所以为了解决这个问题,只有少数设备表现出这样的行为,这要归功于
class MyCountdownService : Service(){
@RequiresApi(Build.VERSION_CODES.O)
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
Log.i("Boot","intent is ${intent?.action}")
showNotification()
wakeLock =
(getSystemService(Context.POWER_SERVICE) as PowerManager).run {
newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "EndlessService::lock").apply {
acquire()
}
}
println("intent ${intent }")
isServiceStarted =true
if(intent!=null) {
val action = intent?.action
when(action){
MyCountdownService.ADD_SERVICE ->{
val countdown = intent.getParcelableExtra<CountDown>(Add_kEY)
schedule(countdown !!)
}
else ->{
Log.i("Boot","Service boot ")
serviceScope.launch(Dispatchers.IO) {
smsScheduler.reSchedule()
}
}
}
}else{
serviceScope.launch(Dispatchers.IO) {
smsScheduler.reSchedule()
}
}
return START_STICKY } }
//清单文件
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>
<service android:name=".service.MyService"
/>
请按照步骤在Android 10 Vivo funtouch Os设备中启用前台服务。
设置<电池<后台功耗管理<您的应用程序<选择"不限制后台电源使用"。更多详细信息