当前位置: 首页 > 知识库问答 >
问题:

不允许启动服务意图-Android Oreo

房时铭
2023-03-14
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);
}
}

这是在Android8.x上运行时出现的当前错误

致命异常:java.lang.RuntimeException无法启动接收方com.heyjude.heyjudeapp.gcm.gcmbroadcastreceiver:java.lang.illegalstateException:不允许启动服务意图{act=com.google.android.c2dm.Intent.receive flg=0x1000010 pkg=com.app.app/.gcm.gcm.gcminTentService(has extras)}:应用程序在后台uid UidRecord{f602fba u0a114 RCVR空闲进程:1 seq(0,0,0)}

共有1个答案

齐迪
2023-03-14

我也经历过同样的行为。

为什么会发生这个问题?

由于Android8新的后台执行限制,您不应该启动后台服务。

<service android:name=".service.GCMIntentService"
        android:exported="false"
        android:permission="android.permission.BIND_JOB_SERVICE"/>
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
    }
}
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)));
    }
}
 类似资料: