Kotlin (Flutter) - 删除后台服务的粘性通知
Kotlin (Flutter)- Remove Sticky notification for background service
我正在构建一个在后台进行某些处理的应用程序。
这是我的代码:-
@RequiresApi(Build.VERSION_CODES.N)
override fun onStartCommand(intent: Intent, flags: Int, startId: Int): Int {
Intent notificationIntent = new Intent();
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
Notification notification = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(getString(R.string.app_name))
.setContentText("Welcome To TestApp")
.setContentIntent(pendingIntent).build();
// startForeground(105, builder.setNotificationSilent().build())
// Handler
eventLoop = object : Runnable {
override fun run() {
println("Inside Run.") // does not prints when `startForeground` is commented, and the code is broken.
handler.postDelayed(this, 1000) // Subsequent runs
}
}
CoroutineScope(IO).launch {
handler.post(eventLoop) // first launch
}
return START_STICKY
}
当 startForeground
没有注释时,上面的代码工作正常。这是调出粘性通知的函数。我在 docs 中读到过它,从它的外观来看,如果我的服务被杀死以回收更多内存,我没问题。我有我的工作管理器设置,如果被杀死,它会自动启动服务。
但是,当我评论它时,代码中断了。 run
将应用移到后台或杀死时,即使通知栏被拉下也不执行
这是抛出的异常。
I/in.zoffers(20132): Wrote stack traces to tombstoned
F/crash_dump64(21223): crash_dump.cpp:465] failed to attach to thread 567: Permission denied
D/AndroidRuntime(20132): Shutting down VM
E/AndroidRuntime(20132): FATAL EXCEPTION: main
E/AndroidRuntime(20132): Process: in.zoffers, PID: 20132
E/AndroidRuntime(20132): android.app.RemoteServiceException: Context.startForegroundService() did not then call Service.startForeground(): ServiceRecord{737e848 u0 in.zoffers/.AutoOffersService}
E/AndroidRuntime(20132): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2151)
E/AndroidRuntime(20132): at android.os.Handler.dispatchMessage(Handler.java:107)
E/AndroidRuntime(20132): at android.os.Looper.loop(Looper.java:228)
E/AndroidRuntime(20132): at android.app.ActivityThread.main(ActivityThread.java:7782)
E/AndroidRuntime(20132): at java.lang.reflect.Method.invoke(Native Method)
E/AndroidRuntime(20132): at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
E/AndroidRuntime(20132): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:981)
W/MixpanelAPI.SysInfo(20132): Permission READ_PHONE_STATE not granted. Property $radio will not be available.
Lost connection to device.
这里到底发生了什么??因为,我可以接受我的服务在内存管理时被杀死,我不需要 startForeground
.
I read about it in the docs and by the looks of it, I'm ok if my service is killed to reclaim more memory
在 Android 8.0+ 上它将在 60 秒内被杀死,除非你使用 startForeground()
.
将其设为前台服务
What exactly is happening here?
您调用 startForegroundService()
启动服务。这意味着您需要在 ANR 时间段(~15 秒 IIRC)内拨打 startForeground()
。如果您不想调用startForeground()
,请不要使用startForegroundService()
启动服务。
我正在构建一个在后台进行某些处理的应用程序。
这是我的代码:-
@RequiresApi(Build.VERSION_CODES.N)
override fun onStartCommand(intent: Intent, flags: Int, startId: Int): Int {
Intent notificationIntent = new Intent();
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
Notification notification = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(getString(R.string.app_name))
.setContentText("Welcome To TestApp")
.setContentIntent(pendingIntent).build();
// startForeground(105, builder.setNotificationSilent().build())
// Handler
eventLoop = object : Runnable {
override fun run() {
println("Inside Run.") // does not prints when `startForeground` is commented, and the code is broken.
handler.postDelayed(this, 1000) // Subsequent runs
}
}
CoroutineScope(IO).launch {
handler.post(eventLoop) // first launch
}
return START_STICKY
}
当 startForeground
没有注释时,上面的代码工作正常。这是调出粘性通知的函数。我在 docs 中读到过它,从它的外观来看,如果我的服务被杀死以回收更多内存,我没问题。我有我的工作管理器设置,如果被杀死,它会自动启动服务。
但是,当我评论它时,代码中断了。 run
将应用移到后台或杀死时,即使通知栏被拉下也不执行
这是抛出的异常。
I/in.zoffers(20132): Wrote stack traces to tombstoned
F/crash_dump64(21223): crash_dump.cpp:465] failed to attach to thread 567: Permission denied
D/AndroidRuntime(20132): Shutting down VM
E/AndroidRuntime(20132): FATAL EXCEPTION: main
E/AndroidRuntime(20132): Process: in.zoffers, PID: 20132
E/AndroidRuntime(20132): android.app.RemoteServiceException: Context.startForegroundService() did not then call Service.startForeground(): ServiceRecord{737e848 u0 in.zoffers/.AutoOffersService}
E/AndroidRuntime(20132): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2151)
E/AndroidRuntime(20132): at android.os.Handler.dispatchMessage(Handler.java:107)
E/AndroidRuntime(20132): at android.os.Looper.loop(Looper.java:228)
E/AndroidRuntime(20132): at android.app.ActivityThread.main(ActivityThread.java:7782)
E/AndroidRuntime(20132): at java.lang.reflect.Method.invoke(Native Method)
E/AndroidRuntime(20132): at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
E/AndroidRuntime(20132): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:981)
W/MixpanelAPI.SysInfo(20132): Permission READ_PHONE_STATE not granted. Property $radio will not be available.
Lost connection to device.
这里到底发生了什么??因为,我可以接受我的服务在内存管理时被杀死,我不需要 startForeground
.
I read about it in the docs and by the looks of it, I'm ok if my service is killed to reclaim more memory
在 Android 8.0+ 上它将在 60 秒内被杀死,除非你使用 startForeground()
.
What exactly is happening here?
您调用 startForegroundService()
启动服务。这意味着您需要在 ANR 时间段(~15 秒 IIRC)内拨打 startForeground()
。如果您不想调用startForeground()
,请不要使用startForegroundService()
启动服务。