Android8.0 java.lang.IllegalStateException: Not allowed to start service Intent
01-01 08:12:20.825  4927  4927 E AndroidRuntime: Caused by: java.lang.IllegalStateException: Not allowed to start service Intent { act=com.android.music.musicservicecommand flg=0x40000000 cmp=com.android.music/.MediaPlaybackService (has extras) }: app is in background uid UidRecord{aa080a7 u0a77 RCV
R idle change:idle|uncached procs:1 seq(0,0,0)}
01-01 08:12:20.825  4927  4927 E AndroidRuntime:        at android.app.ContextImpl.startServiceCommon(ContextImpl.java:1522)
01-01 08:12:20.825  4927  4927 E AndroidRuntime:        at android.app.ContextImpl.startService(ContextImpl.java:1478)
01-01 08:12:20.825  4927  4927 E AndroidRuntime:        at android.content.ContextWrapper.startService(ContextWrapper.java:650)
01-01 08:12:20.825  4927  4927 E AndroidRuntime:        at android.content.ContextWrapper.startService(ContextWrapper.java:650)
01-01 08:12:20.825  4927  4927 E AndroidRuntime:        at com.android.music.MediaAppWidgetProvider.onUpdate(MediaAppWidgetProvider.java:70)
01-01 08:12:20.825  4927  4927 E AndroidRuntime:        at android.appwidget.AppWidgetProvider.onReceive(AppWidgetProvider.java:66)
01-01 08:12:20.825  4927  4927 E AndroidRuntime:        at android.app.ActivityThread.handleReceiver(ActivityThread.java:3214)
01-01 08:12:20.825  4927  4927 E AndroidRuntime:        ... 8 more

    Android8.1版本在桌面加载原生音乐的小部件时,音乐停止运行问题。

在8.0以前使用startService(intent),但是从8.0开始要使用startForegroundService(intent),因此在MediaAppWidgetProvider.java中将startService(intent)替换为startForegroundService(intent)。此时编译运行程序之后仍会报错。

01-01 08:04:43.791  3967  3967 E AndroidRuntime: FATAL EXCEPTION: main
01-01 08:04:43.791  3967  3967 E AndroidRuntime: Process: com.android.music, PID: 3967
01-01 08:04:43.791  3967  3967 E AndroidRuntime: android.app.RemoteServiceException: Context.startForegroundService() did not then call Service.startForeground()
01-01 08:04:43.791  3967  3967 E AndroidRuntime:        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1792)
01-01 08:04:43.791  3967  3967 E AndroidRuntime:        at android.os.Handler.dispatchMessage(Handler.java:106)
01-01 08:04:43.791  3967  3967 E AndroidRuntime:        at android.os.Looper.loop(Looper.java:164)
01-01 08:04:43.791  3967  3967 E AndroidRuntime:        at android.app.ActivityThread.main(ActivityThread.java:6523)
01-01 08:04:43.791  3967  3967 E AndroidRuntime:        at java.lang.reflect.Method.invoke(Native Method)
01-01 08:04:43.791  3967  3967 E AndroidRuntime:        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
01-01 08:04:43.791  3967  3967 E AndroidRuntime:        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:857)
^C

    因为使用startForegroundService(intent)必须要调用startForeground()否则会报错,因此需要在MediaPlaybackService.java中的onCreate()方法中添加

      Notification status = new Notification.Builder(MediaPlaybackService.this,

 MusicBrowserActivity.MUSIC_NOTIFICATION_CHANNEL).build();
      startForeground(PLAYBACKSERVICE_STATUS, status);


阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载,转载请注明出处。 https://blog.csdn.net/Jia_zero/article/details/79969302
个人分类: 源码开发
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

不良信息举报

Android8.0 java.lang.IllegalStateException: Not allowed to start service Intent

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭