使用 androidx 工作管理器API,在工作管理器中,我使用 PeriodicWorkRequest 每隔4小时触发一次工作。但是在运行应用程序后它只能工作一次。
定期工作请求编码:-
Constraints constraints = new Constraints.Builder()
.setRequiredNetworkType(NetworkType.CONNECTED)
.build();
PeriodicWorkRequestpendingCampaignWork = new PeriodicWorkRequest.Builder(PendingCampaignWorker.class, 4, TimeUnit.HOURS)
.setConstraints(constraints)
.build();
工作管理器代码以使请求入队:-
WorkManager.getInstance().enqueueUniquePeriodicWork(LATEST_CAMPAIGN_WORK, ExistingPeriodicWorkPolicy.KEEP, pendingCampaignWork);
要进行测试,请将系统时间手动更改为在模拟器中运行应用程序以触发工作后的4个小时。
我的代码中是否有任何问题可以帮助我解决问题。
更新:-
工作管理器工作正常,如 m.hassan 在回答部分所述,它无法基于系统时间工作。经过测试,每20分钟触发一次工作,它的工作正常。
答案 0 :(得分:1)
Work Manager不是基于系统时间。您可以要求15分钟的定期工作。这样,您可以测试您的代码。
这是一个示例:
我的定期工作请求:
private static final String TAG = "PeriodicWorkTag";
private static final int PERIODIC_WORK_INTERVAL = 15;
public static void schedulePeriodicWork() {
androidx.work.PeriodicWorkRequest periodicWorkRequest = new androidx.work.PeriodicWorkRequest.Builder(PeriodicWorkRequest.class, PERIODIC_WORK_INTERVAL,
TimeUnit.MINUTES)
.addTag(TAG)
.build();
WorkManager.getInstance().enqueueUniquePeriodicWork(TAG, ExistingPeriodicWorkPolicy.KEEP, periodicWorkRequest);
}
public static void cancelPeriodicWork() {
WorkManager.getInstance().cancelAllWorkByTag(TAG);
}
我的工人班:
public static final String CHANNEL_ID = "VERBOSE_NOTIFICATION" ;
public PeriodicWorkRequest(@NonNull Context context, @NonNull WorkerParameters workerParams) {
super(context, workerParams);
}
@NonNull
@Override
public Result doWork() {
showNotification(getApplicationContext());
return Result.SUCCESS;
}
private void showNotification(Context context) {
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context, CHANNEL_ID)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("My notification")
.setContentText("ddd")
.setStyle(new NotificationCompat.BigTextStyle()
.bigText("ddd"))
.setPriority(NotificationCompat.PRIORITY_DEFAULT);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
CharSequence name = "Channel_name";
String description = "description";
int importance = NotificationManager.IMPORTANCE_DEFAULT;
NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance);
channel.setDescription(description);
NotificationManager notificationManager = context.getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(channel);
}
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);
notificationManager.notify(100, mBuilder.build());
}
答案 1 :(得分:0)
这在我的案例中起到了作用:将您的应用添加到 IGNORE_BATTERY_OPTIMIZATIONS
列表。
@SuppressLint("BatteryLife")
public void showIgnoreBatteryOpt() {
String packageName = getPackageName();
Intent intent = new Intent();
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
if (!pm.isIgnoringBatteryOptimizations(packageName)) {
Log.d(TAG, "showIgnoreBatteryOpt: NOT ignoring");
intent.setAction(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
intent.setData(Uri.parse("package:" + packageName));
startActivity(intent);
}
else {
Log.d(TAG, "showIgnoreBatteryOpt: ignoring");
}
}
添加到AndroidManifest.xml
:
<uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS"/>