不允许启动服务意图

不允许启动服务意图

本文介绍了不允许启动服务意图-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

推荐答案

我也遇到相同的行为.

为什么会发生此问题?

我如何修复

将您的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 onHandleWorkthen 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 22:38