Caused by: java.lang.IllegalStateException: Not allowed to start service Intent { act=android.intent.action.PACKAGE_CHANGED报错处理

1 log 分析

04-04 16:28:56.738 E/AndroidRuntime( 2913): FATAL EXCEPTION: main

04-04 16:28:56.738 E/AndroidRuntime( 2913): Process: org.codeaurora.gallery, PID: 2913

04-04 16:28:56.738 E/AndroidRuntime( 2913): java.lang.RuntimeException: Unable to start receiver com.android.gallery3d.app.PackagesMonitor: java.lang.IllegalStateException: Not allowed to start service Intent { act=android.intent.action.PACKAGE_CHANGED dat=package:com.android.vending flg=0x5000010 cmp=org.codeaurora.gallery/com.android.gallery3d.app.PackagesMonitor$AsyncService (has extras) }: app is in background uid UidRecord{415e430 u0a36 RCVR idle change:uncached procs:1 seq(0,0,0)}

04-04 16:28:56.738 E/AndroidRuntime( 2913): at android.app.ActivityThread.handleReceiver(ActivityThread.java:3197)

04-04 16:28:56.738 E/AndroidRuntime( 2913): at android.app.ActivityThread.-wrap17(Unknown Source:0)

04-04 16:28:56.738 E/AndroidRuntime( 2913): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1675)

04-04 16:28:56.738 E/AndroidRuntime( 2913): at android.os.Handler.dispatchMessage(Handler.java:106)

04-04 16:28:56.738 E/AndroidRuntime( 2913): at android.os.Looper.loop(Looper.java:164)

04-04 16:28:56.738 E/AndroidRuntime( 2913): at android.app.ActivityThread.main(ActivityThread.java:6518)

04-04 16:28:56.738 E/AndroidRuntime( 2913): at java.lang.reflect.Method.invoke(Native Method)

04-04 16:28:56.738 E/AndroidRuntime( 2913): at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)

04-04 16:28:56.738 E/AndroidRuntime( 2913): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)

04-04 16:28:56.738 E/AndroidRuntime( 2913): Caused by: java.lang.IllegalStateException: Not allowed to start service Intent { act=android.intent.action.PACKAGE_CHANGED dat=package:com.android.vending flg=0x5000010 cmp=org.codeaurora.gallery/com.android.gallery3d.app.PackagesMonitor$AsyncService (has extras) }: app is in background uid UidRecord{415e430 u0a36 RCVR idle change:uncached procs:1 seq(0,0,0)}

04-04 16:28:56.738 E/AndroidRuntime( 2913): at android.app.ContextImpl.startServiceCommon(ContextImpl.java:1521)

04-04 16:28:56.738 E/AndroidRuntime( 2913): at android.app.ContextImpl.startService(ContextImpl.java:1477)

04-04 16:28:56.738 E/AndroidRuntime( 2913): at android.content.ContextWrapper.startService(ContextWrapper.java:650)

04-04 16:28:56.738 E/AndroidRuntime( 2913): at android.content.ContextWrapper.startService(ContextWrapper.java:650)

04-04 16:28:56.738 E/AndroidRuntime( 2913): at com.android.gallery3d.app.PackagesMonitor.onReceive(PackagesMonitor.java:40)

04-04 16:28:56.738 E/AndroidRuntime( 2913): at android.app.ActivityThread.handleReceiver(ActivityThread.java:3190)

04-04 16:28:56.738 E/AndroidRuntime( 2913): ... 8 more

2 原因分析

Android go  不再允许后台service直接通过startService方式去启动, 具体行为变更如下:

如果针对 Android go 的应用尝试在不允许其创建后台服务的情况下使用 startService() 函数,则该函数将引发一个 IllegalStateException。 新的 Context.startForegroundService() 函数将启动一个前台服务。现在,即使应用在后台运行, 系统也允许其调用 Context.startForegroundService()。不过,应用必须在创建服务后的五秒内调用该服务的 startForeground() 函数。

3 解决方法


图片发自简书App

注意点:要修改对应的启动的类

我们这里要修改这里。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。