每次执行应用程序时,我都会收到“startForeground 的错误通知”错误
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.pennskanvtic, PID: 17447
android.app.RemoteServiceException$CannotPostForegroundServiceNotificationException:
Bad notification for startForeground
at android.app.ActivityThread.throwRemoteServiceException(ActivityThread.java:1978)
at android.app.ActivityThread.-$$Nest$mthrowRemoteServiceException(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2237)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loopOnce(Looper.java:201)
at android.os.Looper.loop(Looper.java:288)
at android.app.ActivityThread.main(ActivityThread.java:7884)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:936)`
Run Code Online (Sandbox Code Playgroud)
我的服务仅启动计时器并显示通知(我尝试在NotificationCompat.Builder中使用setChannelId而不进行任何更改):
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.pennskanvtic, PID: 17447
android.app.RemoteServiceException$CannotPostForegroundServiceNotificationException:
Bad notification for startForeground
at android.app.ActivityThread.throwRemoteServiceException(ActivityThread.java:1978)
at android.app.ActivityThread.-$$Nest$mthrowRemoteServiceException(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2237)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loopOnce(Looper.java:201)
at android.os.Looper.loop(Looper.java:288)
at android.app.ActivityThread.main(ActivityThread.java:7884)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:936)`
Run Code Online (Sandbox Code Playgroud)
我的 MainActivity 在 onStart() 内请求许可:
public class …Run Code Online (Sandbox Code Playgroud)