Reputation: 4092
Am Using androidx Work manager API, in Work manager am using PeriodicWorkRequest to trigger the Work for every 4 hours. But it works only once after run the application.
PeriodicWorkRequest Coding:-
Constraints constraints = new Constraints.Builder()
.setRequiredNetworkType(NetworkType.CONNECTED)
.build();
PeriodicWorkRequestpendingCampaignWork = new PeriodicWorkRequest.Builder(PendingCampaignWorker.class, 4, TimeUnit.HOURS)
.setConstraints(constraints)
.build();
Work Manger Code to Enqueue the Request:-
WorkManager.getInstance().enqueueUniquePeriodicWork(LATEST_CAMPAIGN_WORK, ExistingPeriodicWorkPolicy.KEEP, pendingCampaignWork);
For testing am change System time Manually to 4 hours after run the application in emulator to trigger the Work.
Is there any issue in my code help me to solve the issue.
Update:-
Work Manager is working fine, its not working based on System time as m.hassan said in answer section. Am test to trigger the work for every 20 minutes, its working fine.
Upvotes: 5
Views: 2279
Reputation: 99
This did the trick in my case: Add your app to IGNORE_BATTERY_OPTIMIZATIONS
list.
@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");
}
}
Add to AndroidManifest.xml
:
<uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS"/>
Upvotes: 0
Reputation: 44
Work Manager Not not based on system time. You can make a periodic work request of 15 minutes. This way you can test your code.
here's an example:
My Periodic Work Request:
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);
}
My Worker Class:
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());
}
Upvotes: 2