如何确保在Android N及更高版本上重启设备时调用PeriodicWorkRequests
我最近尝试使用 WorkManager
的 PeriodicWorkRequest
s作为确保定期获取用户位置更新的可靠方法在后台。该库满足了我的要求,这个特殊的细节引起了我的注意:
I recently tried to use the WorkManager
's PeriodicWorkRequest
s as a surefire way of getting user location updates periodically in the background. The library met my requirements and this particular detail got my attention:
即使应用程序或设备重新启动,也可以保证任务执行
Guarantees task execution, even if the app or device restarts
已经实施并测试了它,我尝试重新启动设备,并注意到Log消息和App Notification从未显示。
Having implemented and tested it, I tried rebooting my device and noticed the Log message and App Notification never showed up.
自然地,我做了一些研究并偶然发现:在Android Oreo中重新启动设备后,PeriodicWorkRequest不起作用
Naturally, I did some research and stumbled upon this: PeriodicWorkRequest not working after device reboot in android oreo
所以我尝试使用 BOOT_COMPLETED
广播接收器,以在设备重新启动时触发WorkRequest,但它不起作用。我在SO上检查了许多 BOOT_COMPLETED
接收者的问题,但没有任何作用,除了这里:在Android中重启设备后,广播接收器无法正常工作,但这涉及使用户选择您的应用作为 AccessibilityService ,这在我看来是UX的障碍。
So I tried using the BOOT_COMPLETED
broadcast receiver to trigger the WorkRequest on device reboot but it didn't work. I checked numerous BOOT_COMPLETED
receivers questions on SO but none worked, other than this here: Broadcast Receiver Not Working After Device Reboot in Android but that involved making the user select your app as an AccessibilityService which is a hinderance to the UX in my opinion.
经过研究后,我发现WorkManager在幕后使用了BOOT_COMPLETED BroadcastReceiver。到目前为止,我已经在Android Nougat和Oreo设备上进行了测试,因此我在运行API 16的Samsung以及运行API 22的HTC上测试了BroadcastReceiver。Receiver正常工作,我删除了它们,并在重新启动时测试了WorkRequests。他们也一样!
After some research I found out that the WorkManager uses the BOOT_COMPLETED BroadcastReceiver under the hood. I have been testing on Android Nougat and Oreo devices so far, so I tested the BroadcastReceiver on a Samsung running API 16, and also on an HTC running API 22. The Receiver worked and I removed them and also tested the WorkRequests on reboot; they worked too!
这是我实现 PeriodicWorkRequest
PeriodicWorkRequest request =
new PeriodicWorkRequest.Builder(LocationListenerWorker.class,
12, HOURS)
.build();
mWorkManager.enqueueUniquePeriodicWork(Constants.LOCATION_TASK_ID,
ExistingPeriodicWorkPolicy.REPLACE, request);
我们将非常感谢您提供任何有关如何确保在运行API 24的设备上重启请求时也能调用请求的帮助+
Would appreciate any help on how to ensure the Requests also get called on device reboot on devices running API 24+
假设您定义了一个RebootWorker类,该类扩展了Worker并在设备重新启动后执行某些特定的工作。第一种方法:
Let Say you have defined a RebootWorker class that extends Worker and do some particular job after device get rebooted. 1st Approach :
public class RebootWorker extends Worker {
...............................
}
在那种情况下,在清单
<service
android:name=".RebootWorker"
android:process=":worker"/>
这将帮助Workmanger在设备重启后运行您的辅助服务。由于设备重启,您的应用已从任务管理器中清除。
This will help to get Workmanger run your worker service after device reboot. Because due to device reboot your app is cleared from task manager.
第二种方法:您还可以使用BroadcastReceiver监听启动完成的操作
2nd Approach : You can also use BroadcastReceiver to listen Boot completed action
public class MyReceiver extends BroadcastReceiver {
WorkManager mWorkManager;
PeriodicWorkRequest rebootRequest;
@Override
public void onReceive(Context context, Intent intent) {
Log.i(MainActivity.TAG, "intent.getAction(): "+intent.getAction());
//Reboot worker
mWorkManager = WorkManager.getInstance(context);
rebootRequest = new PeriodicWorkRequest.Builder(RebootWorker.class,
MainActivity.REPEAT_INTERVAL, MainActivity.TIME).build();
if ("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())) {
//do your work and enqueue it
mWorkManager.enqueue(rebootRequest);
}
}
}