【发布时间】:2023-01-27 13:49:44
【问题描述】:
我正在开发我开始计数很长一段时间的应用程序,为此我有 startforeground 服务该服务在所有设备上都运行良好,除了像 android 10 vivo funtouch Os 这样的少数设备。其中,当您滑动通知时,它会终止前台服务,因此要解决此问题,只有少数设备会出现这种情况,谢谢
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 } }
//Manifest file
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>
<service android:name=".service.MyService"
/>
【问题讨论】: