不允许启动服务意图-Android Oreo [英] Not allowed to start service Intent - Android Oreo

查看:128
本文介绍了不允许启动服务意图-Android Oreo的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我当前正在使用startWakefulService函数,该函数在Oreo中崩溃.我意识到我要么必须切换到startForegroundService()并使用前台服务,要么要切换到JobIntentService,但是基于下面的代码,我不确定该怎么做. (对不起,我是android新手).朝着正确方向的任何观点将不胜感激.

I am currently using the startWakefulService function which is crashing in Oreo. I realise I either have to switch to startForegroundService() and use a foreground service, or switch to JobIntentService but based on my code below I am not sure what to do. (Sorry I am an android novice). Any points in the right direction will be greatly appreciated.

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);
}
}

这是我在Android 8.x上运行时遇到的当前错误

This is the current error I get when run on Android 8.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 cmp = com.app.app/.gcm.GCMIntentService(有其他功能)}:应用程序位于后台uid UidRecord {f602fba u0a114 RCVR空闲过程:1 seq(0,0,0)}

Fatal Exception: java.lang.RuntimeException Unable to start receiver com.heyjude.heyjudeapp.gcm.GcmBroadcastReceiver: java.lang.IllegalStateException: Not allowed to start service Intent { act=com.google.android.c2dm.intent.RECEIVE flg=0x1000010 pkg=com.app.app cmp=com.app.app/.gcm.GCMIntentService (has extras) }: app is in background uid UidRecord{f602fba u0a114 RCVR idle procs:1 seq(0,0,0)}

推荐答案

我也遇到相同的行为.

为什么会发生此问题?

由于Android 8新增了背景执行限制你应该 无法启动服务背景.

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

我如何修复

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

Migrate your GCMIntetService to JobIntentService instead of IntentService.

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

Please follow this steps: 1) Add the BIND_JOB_SERVICE permission to your service:

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

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

2) In your GCMIntentService, instead extending the IntentService, use android.support.v4.app.JobIntentService and override onHandleWork then remove the override in you onHandleIntent.

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入队.

Then finally, in your GCMBroadcastReceiver, enqueue your 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之后,该实现对我有用,我希望它对您有用.

This implementation work for me after we update our target sdk to 27 and I hope it works for you.

这篇关于不允许启动服务意图-Android Oreo的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆