Not allowed to start service Intent - Android Oreo

后端 未结 2 476
轮回少年
轮回少年 2021-01-06 05:36

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,

相关标签:
2条回答
  • 2021-01-06 06:21

    Based on the documentation:

    As of Android O, background check restrictions make this class no longer generally useful. (It is generally not safe to start a service from the receipt of a broadcast, because you don't have any guarantees that your app is in the foreground at this point and thus allowed to do so.) Instead, developers should use android.app.job.JobScheduler to schedule a job, and this does not require that the app hold a wake lock while doing so (the system will take care of holding a wake lock for the job).

    Only alternative is to start a ForeGroundService or use JobScheduler/JobIntentService

    You can start foreground service as follows:

    @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.
        ContextCompat.startForegroundService(context,intent.setComponent(comp));
        ...
    }
    

    You also need to call startService() from your Service class and display notification. You can follow my answer on this SO for implementation details.

    0 讨论(0)
  • 2021-01-06 06:31

    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.

    0 讨论(0)
提交回复
热议问题