WakefulBroadcastReceiver is deprecated

前端 未结 2 1943
面向向阳花
面向向阳花 2021-02-05 06:57

For creating a receiver I\'m extended WakefulBroadcastReceiver in my old project. But now it\'s deprecated. Instead of WakefulBroadcastReceiver

2条回答
  •  一向
    一向 (楼主)
    2021-02-05 07:17

    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.

提交回复
热议问题