gpt4 book ai didi

Android GCM - WakefulBroadcastReceiver.startWakefulService 由于 IllegalStateException 8.0 而崩溃

转载 作者:行者123 更新时间:2023-11-29 18:46:51 25 4
gpt4 key购买 nike

我们刚刚将应用程序的目标 api 迁移到 Android API 27 (8.1),当通知到达时它一直崩溃,特别是当应用程序在后台时。

java.lang.RuntimeException: Unable to start receiver <package>.service.GCMBroadcastReceiver: java.lang.IllegalStateException: Not allowed to start service Intent { act=com.google.android.c2dm.intent.RECEIVE flg=0x1000010 pkg=<package> cmp=<package>/.service.GCMIntentService (has extras) }: app is in background uid UidRecord{b248a9d u0a85 RCVR bg:+1m4s53ms idle change:uncached procs:1 seq(0,0,0)}
at android.app.ActivityThread.handleReceiver(ActivityThread.java:3194)
at android.app.ActivityThread.-wrap17(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1672)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
Caused by: java.lang.IllegalStateException: Not allowed to start service Intent { act=com.google.android.c2dm.intent.RECEIVE flg=0x1000010 pkg=<package> cmp=<package>/.service.GCMIntentService (has extras) }: app is in background uid UidRecord{b248a9d u0a85 RCVR bg:+1m4s53ms idle change:uncached procs:1 seq(0,0,0)}
at android.app.ContextImpl.startServiceCommon(ContextImpl.java:1521)
at android.app.ContextImpl.startService(ContextImpl.java:1477)
at android.content.ContextWrapper.startService(ContextWrapper.java:650)
at android.support.v4.content.WakefulBroadcastReceiver.startWakefulService(WakefulBroadcastReceiver.java:99)
at <package>.service.GCMBroadcastReceiver.onReceive(GCMBroadcastReceiver.java:32)
at android.app.ActivityThread.handleReceiver(ActivityThread.java:3187)
at android.app.ActivityThread.-wrap17(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1672)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)

最佳答案

我是这样解决问题的:

为什么会出现这个问题?

Due to the new Background Execution Limits of Android 8 and you should not start services background.

我是如何修复它的

将您的 GCMIntetService 迁移到 JobIntentService 而不是 IntentService

请按照以下步骤操作:1) 为您的服务添加 BIND_JOB_SERVICE 权限:

<service android:name=".service.GCMIntentService"
android:exported="false"
android:permission="android.permission.BIND_JOB_SERVICE"/>

2) 在您的 GCMIntentService 中,使用 android.support.v4.app.JobIntentService 并覆盖 onHandleWork 而不是扩展 IntentService然后删除 onHandleIntent 中的 override

public class GCMIntentService extends JobIntentService {

// Service unique ID
static final int SERVICE_JOB_ID = 50;

// Enqueuing work in to this service.
public static void enqueueWork(Context context, Intent work) {
enqueueWork(context, GCMIntentService.class, SERVICE_JOB_ID, work);
}

@Override
protected void onHandleWork(@NonNull Intent intent) {
onHandleIntent(intent);
}

private void onHandleIntent(Intent intent) {
//Handling of notification goes here
}
}

最后,在您的 GCMBroadcastReceiver 中,将您的 GCMIntentService 加入队列。

public class GCMBroadcastReceiver extends WakefulBroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
// Explicitly specify that GcmIntentService will handle the intent.
ComponentName comp = new ComponentName(context.getPackageName(),
GCMIntentService.class.getName());
// Start the service, keeping the device awake while it is launching.
// startWakefulService(context, (intent.setComponent(comp)));

//setResultCode(Activity.RESULT_OK);

GCMIntentService.enqueueWork(context, (intent.setComponent(comp)));
}
}

在我们将目标 sdk 更新到 27 后,这个实现对我有用,我希望它对你有用。

关于Android GCM - WakefulBroadcastReceiver.startWakefulService 由于 IllegalStateException 8.0 而崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51532059/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com