Android 我从谷歌开发者的仪表板上看到了这个崩溃,但我不明白是什么导致了它
这就是我在google play开发者仪表板中遇到的崩溃。Rebootreceiver扩展broadcastreceiver类,其中onReceive()我调用另一个扩展服务的类,在该类中我调用一个函数来启动报警。这个崩溃可能是因为用户禁用了我的应用程序的谷歌警报或类似的东西造成的吗?有什么想法吗Android 我从谷歌开发者的仪表板上看到了这个崩溃,但我不明白是什么导致了它,android,Android,这就是我在google play开发者仪表板中遇到的崩溃。Rebootreceiver扩展broadcastreceiver类,其中onReceive()我调用另一个扩展服务的类,在该类中我调用一个函数来启动报警。这个崩溃可能是因为用户禁用了我的应用程序的谷歌警报或类似的东西造成的吗?有什么想法吗 java.lang.RuntimeException: at android.app.ActivityThread.handleReceiver (ActivityThread.java:423
java.lang.RuntimeException:
at android.app.ActivityThread.handleReceiver (ActivityThread.java:4238)
at android.app.ActivityThread.access$1700 (ActivityThread.java:274)
at android.app.ActivityThread$H.handleMessage (ActivityThread.java:2113)
at android.os.Handler.dispatchMessage (Handler.java:107)
at android.os.Looper.loop (Looper.java:237)
at android.app.ActivityThread.main (ActivityThread.java:8167)
at java.lang.reflect.Method.invoke (Method.java)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run (RuntimeInit.java:496)
at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:1100)
Caused by: java.lang.IllegalStateException:
at android.app.ContextImpl.startServiceCommon (ContextImpl.java:1688)
at android.app.ContextImpl.startService (ContextImpl.java:1633)
at android.content.ContextWrapper.startService (ContextWrapper.java:683)
at android.content.ContextWrapper.startService (ContextWrapper.java:683)
at com.example.myapp.RebootReceiver.onReceive (RebootReceiver.java:31)
at android.app.ActivityThread.handleReceiver (ActivityThread.java:4229)
at android.app.ActivityThread.access$1700 (ActivityThread.java:274)
at android.app.ActivityThread$H.handleMessage (ActivityThread.java:2113)
at android.os.Handler.dispatchMessage (Handler.java:107)
at android.os.Looper.loop (Looper.java:237)
at android.app.ActivityThread.main (ActivityThread.java:8167)
at java.lang.reflect.Method.invoke (Method.java)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run (RuntimeInit.java:496)
at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:1100)
从堆栈跟踪中,我发现当设备启动时,您的应用程序处于后台时,您的接收器是
RebootReceiver
。如果您的应用程序目标API为26(Android 8)或更高,并且它在后台尝试启动服务,它将因IllegalStateException
而崩溃,因此如果Android为8或更高,您需要调用startForgroundService
,而不是startService
:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
context.startForegroundService(new Intent(context, ServedService.class));
} else {
context.startService(new Intent(context, ServedService.class));
}
在服务的onHandleIntent
中,您需要调用startForeground
,否则您将再次获得IllegalStateException
如果您已将targetSdkVersion=28
(Android 9/Pie)或更高版本设置为启动前台服务,则需要声明使用前台服务的权限:
<manifest ...>
...
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
...
<application ...>
...
</manifest>
...
...
...
是否可以在emulator上检查此问题?因为我什么都没有crashes@stefanosn是的,在模拟器上检查它的Android是8或以上8谢谢你的建议…我在服务意图中有这段代码。。。公共类MyCustomRebootService扩展服务{Override public void onCreate(){MainActivity.StartArm(this);}Override public int onStartCommand(Intent Intent,int flags,int startId){return START_NOT_STICKY;}Override public IBinder onBind(意图){returnnull;}}我猜startForeground不需要,因为我调用了mainactivity函数?我在emulator上测试了它,但似乎没有在startService和android 10上崩溃…有什么想法吗?当应用程序在后台时,startService和startForeground服务似乎都在emulator上工作。可能是这一行吗?如果(intent.getAction().equals(“android.intent.action.BOOT_COMPLETED”))我在RebootReceiver中检查启动是否完成顺便说一下,应用程序在启动时关闭,即使在后台也不关闭。。。