WakefulBroadcastReceiver is deprecated
Asked Answered
K

2

29

For creating a receiver I'm extended WakefulBroadcastReceiver in my old project. But now it's deprecated. Instead of WakefulBroadcastReceiver which Receiver I should use now and how to convert below code with new method?

Here is my code:

 public class TaskFinishReceiver extends WakefulBroadcastReceiver {
    private PowerManager mPowerManager;
    private PowerManager.WakeLock mWakeLock;
    @Override
    public void onReceive(Context context, Intent intent) {
        mPowerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
        turnOnScreen();
        Intent wakeIntent = new Intent();

        wakeIntent.setClassName("com.packagename", "com.packagename.activity.TaskFinished");
        wakeIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(wakeIntent);
    }


    public void turnOnScreen(){
        mWakeLock = mPowerManager.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, "tag");
        mWakeLock.acquire();
    }
}
Kairouan answered 10/11, 2017 at 7:3 Comment(0)
P
15

You can rewrite your code like this:

    public class TaskFinishReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            //do your stuff in the JobIntentService class
            MyJobIntentService.enqueueWork(context, intent);
        }
    }

This will work since, according to documentation, the new JobIntentService class will handle both wake locks and backward compatibility:

You do not need to use WakefulBroadcastReceiver when using this class. When running on Android O, 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.

Primulaceous answered 27/6, 2018 at 8:7 Comment(3)
This is a much better solution for me while I am doing migration for Android O as I was using WakefulBroadcastReceiver->IntententService model which cab be easily converted to BroadcastReceiver->JobIntentService !Tercel
@Tercel JobIntentService now deprecated.Taverner
JobIntentService is deprecated. Use the new JobIntentService from androidx. androidx.core.app.JobIntentServiceKizzee
F
7

WakefulBroadcastReceiver Deprecated in API level 26.1.0.

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

public class JobSchedulerService extends JobService {

    @Override
    public boolean onStartJob(JobParameters params) {

        return false;
    }

    @Override
    public boolean onStopJob(JobParameters params) {

        return false;
    }

}

For demo case, Check

Florio answered 10/11, 2017 at 7:7 Comment(4)
@Yeahia420 Read article vogella.com/tutorials/AndroidTaskScheduling/article.htmlFlorio
if will be helpful for others if you provide any detail answer about job scheduler here.Kairouan
JobService still has limitation for Android O and pre O. So please use JobIntentService instead. more detail please check here: medium.com/til-kotlin/…Featherhead
@Featherhead JobIntentService now deprecated.Taverner

© 2022 - 2024 — McMap. All rights reserved.