Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Not allowed to start service Intent - Android Oreo

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

This is the current error I get when run on Android 8.x

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

like image 230
Marcus Smith Avatar asked Nov 29 '22 21:11

Marcus Smith


1 Answers

I experience the same behaviour.

Why this issue happens?

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

How I fixed it

Migrate your GCMIntetService to JobIntentService instead of IntentService.

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

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

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

like image 148
Abdulmoiz Esmail Avatar answered Dec 04 '22 13:12

Abdulmoiz Esmail