WakefulBroadcastReceiver is deprecated

Android WakefulBroadcastReceiver deprecated Warning (Android Studio)

'android.support.v4.content.wakefulbroadcastreceiver' is deprecated.
This Inspection reports where deprecated code is used in the specified inspection scope

WakefulBroadcastReceiver Android Oreo deprecation

The WakefulBroadcastReceiver class was deprecated in API level 26.1.0 (Android – 8.0 Oreo).
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).

Android Service Wacklock, WAKE_LOCK permission and JobIntentService

To overcome this background restriction and WAKE_LOCK permission issues. According to documentation, the new JobIntentService class will work to handle both wake locks and backward compatibility:
JobIntentService is Helper for processing work that has been enqueued for a job/service. When running on Android O or later, the work will be dispatched as a job via JobScheduler.enqueue. When running on older android versions of the platform, it will use Context.startService. Use enqueueWork(Context, Class, int, Intent) to enqueue new work to be dispatched to and handled by your service. It will be executed in onHandleWork(Intent). You do not need to use WakefulBroadcastReceiver when using this class. When running on Android Oreo, the JobScheduler will take care of wake locks for you (holding a wake lock from the time you enqueue work until the job has been dispatched and while it is running). When running on previous versions of the platform, this wake lock handling is emulated in the class here by directly calling the PowerManager; this means the application must request the WAKE_LOCK permission. So, make sure to add the WAKE_LOCK permission in your AndroidManifest.xml.
<uses-permission android:name="android.permission.WAKE_LOCK"></uses-permission>

IntentService + WakefulBroadcastReceiver replacement with JobIntentService + BroadcastReceiver

There is much better solution for while migrating to Android O as IntentService + WakefulBroadcastReceiver are deprecated with API 26 (Android 8.0 Oreo). The best alternative is to use JobIntentService+BroadcastReceiver  which can be easily converted from IntentService + WakefulBroadcastReceiver model. JobIntentService class  of support library 26 relies on JobScheduler API of android and has the exact workflow of IntentService. So, For pre-Oreo android devices, JobIntentService will use old IntentService.

Use Jobintentservice Android O and BroadcastReceiver for background processing(Services)

Use the BroadcastReceiver and JobIntentService for background processing(Services) on Android Oreo (26)
TaskFinishReceiver.java
public class TaskFinishReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            //enqueueWork will pass service/work intent in the JobIntentService class
            MyJobIntentService.enqueueWork(context, intent);
        }
    }

MyJobIntentService.java
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.JobIntentService;
import android.util.Log;


/**
 * Example implementation of a JobIntentService.
 */

public class MyJobIntentService extends JobIntentService {
 
    static final int JOB_ID = 1000;

/**
     * Convenience method for enqueuing work in to this service.
     */
    static void enqueueWork(Context context, Intent work) {
        enqueueWork(context, AlarmSchedulingService.class, JOB_ID, work);
    }

    @Override
    protected void onHandleWork(Intent intent) {
       // We have received work to do.  The system or framework is already
        // holding a wake lock for us at this point, so we can just go.
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.e("COMPLETED WORK:","All work complete");
    }
}

No comments:

Post a Comment

Popular Posts