following this question, I looked into Android Job Scheduling and it seems like Work Manager is the latest tool if I want to do something once every month. I built a Worker class and activated it with this snippet in my activity:
PeriodicWorkRequest myWorkRequest =
new PeriodicWorkRequest.Builder(MonthlyWorker.class, 20, TimeUnit.MINUTES)
.build();
WorkManager.getInstance().enqueueUniquePeriodicWork("my fancy test",ExistingPeriodicWorkPolicy.KEEP, myWorkRequest);
While i tested the 20 minutes period successfully, there is a problem: There are no TimeUnits for months available, only days. Doing the job every 30 days wont work for obvious reasons, since I need the task to be executed on the first day of each month. So what should i do? Do I have to use a different API? Thank you.
I could do something like if (dayOfMonth==1) do Function
, but I think that undermines the purpose of a scheduler.
You can do by minutes itself:
Just convert days to minutes:
Here : 30 days = 43200 minutes = 1 month
UPDATE:
As per my requirement, I wanted to perform task every 30 minutes in between 8 am to 6 pm
With help of that worker I made for you as you required 1st of every month in between 12 am to 2 am below:
You can start this test work for X hour interval so it will check every X hour (for example, In your case you can give 2 hours so it will check every two hours)
I do not know whether it will be affected to performance or not, but I think there is no other solution.
You can start worker as below:
Hope it will help.