Run a task in my app once in every month - android

In my android app I have created a function that updates some fields in SQLite database, now I run this function manually and it is doing the job, but I want to run this function automatically once in a month. How it is possible.

To run your task periodically i recommend you to take a look at it JobScheduler api which is latest and more powerful for scheduling the job take a look at this https://developer.android.com/reference/android/app/job/JobScheduler.html
if your app targets api >21 you must use jobscheduler if u want to support the lower version take a look at firebase job dispatcher:https://github.com/firebase/firebase-jobdispatcher-android

Related

.NET MAUI Recurrent Background Job that runs few times a day

I would like to have a background job to run like 2-3 times a day, but even less it's ok. It's just a quick api call to my server, it doesn't need to update the UI, infact I prefer that it runs when the app is not in the foreground, but that's another topic.
I've read that latests versions of Android and iOS or even manifacturers restrict the app but I don't need these task to be at certain time precisely.
I'm not sure if I should use the JobScheduler or WorkManager for Android and the BackgroundFetch or BackgroundTask for iOS.
Let's say the task should run 3 times a day so every 8 hours roughly.
The thing is that a user might not open the app again so the task should be scheduled to be recurrent, maybe every time it runs it could schedule another one, but if one fails that line could never be reached.
I don't need to support old operating system version, only iOS 15+ and Android api 30+.
Thank you for the help!
Apple is strict about hardware resources occupied by App, not to mention App in the background state. When App enter the background state, it will soon be suspended by the OS, unless you apply for permission from OS. Only several modes could be allowed to run in the background, see the following picture
You want a background cron job which is not allowed in iOS.
For Andriod, you know we could use WorkManager or JobSchedule. Here are some documents that you could refer to:
Getting Started With WorkManager
Android Job Scheduler
Hope it works for you.

Workmanager with multiple-processes

Is there a way to enqueue work in work manager from multiple processes? Right now I’m getting this message when I’m trying to do it from the "non-ui" process
I/WM-GreedyScheduler: Ignoring schedule request in non-main process.
Work manager version I'm using is 2.4.0. I know there is 2.5.0-alpha2 right now, but I cannot use that in production
WorkManager 2.5.0-alpha02 is the first version of WorkManager that has multi-process support.
There is no mechanism for using WorkManager across multiple processes in any previous version of WorkManager (hence why these new APIs were added in the first place) so you'll need to either upgrade to that version of WorkManager or wait for it to become beta/RC/stable.

No way to make Work persistent across reboots?

So I know WorkManager utilizes JobScheduler on supported APIs but there doesnt seem to be any way to make WorkManager work persistent across reboots? Is the only option to request the BOOT_COMPLETED permission and reschedule jobs?
To answer your question: You don't need to do anything if the device is rebooted. WorkManager keeps scheduling your Work without any additional requirement from your side.
WorkManager persists Work requests in its internal Room database. This allows it to guarantee Work execution across device restart.
The documentation covers this as this blog "Introducing WorkManager" that the team wrote last year.
WorkManager is actually used to persist deferrable tasks even after your app exits and the device restarts, please refer to the docs. It uses JobScheduler for api 23 and above and broadcastReceiver and AlarmManger on api 14 to 22. You can use constraints to check battery status, network coverage ...etc depending your particular usecase. You just have to be careful not to remove or rename your existing classes after they are added in the queue because WorkManager uses it's internal database to store those classes and your app will crash if you remove them or rename them.

What's the proper way to schedule tasks and reminders in Oreo?

Background
Ever since Android O came out I had trouble with what classes and methods I should use when I want to schedule background sync tasks and notifications, more specifically, I want to do these two separate things:
Background sync task to download data from the server, and notify the user about important information (that was just fetched from the server). These syncs should occur at an almost precise time of the day (e.g. 21:30 ±10min).
Notify the user about an upcoming event, for example, one week before an exam.
Because Android O has placed some restrictions on AlarmManager, I cannot set a background service that runs at a specific time of the day, unless I use getForegroundService(), which, as the docs say, should only be used for services that are noticeable to the user.
What I tried / considered
I have been using JobService that runs periodically every so and so hours, but I would prefer for it to run at a more specific time of the day.
I have looked into CalendarProvider, and also considered a push notification service, but it seems to me like an overkill for simple tasks like these.
Question
My final question is what methods I could, or I should use to implement the above features?
Use WorkManager this is the best way to Run Task in Background in Android Oreo and Pie versions and it also works in older versions of Android.
See the Documentation
https://developer.android.com/topic/libraries/architecture/workmanager
https://developer.android.com/topic/libraries/architecture/workmanager/basics
https://developer.android.com/topic/libraries/architecture/workmanager/advanced
GitHub Example
https://github.com/krunalpatel3/WorkManager-Example-Andorid
Reference
http://thetechnocafe.com/how-to-use-workmanager-in-android/
https://www.youtube.com/watch?v=fAQKvBHeg_w
https://www.youtube.com/watch?v=1VVir3-4hII
https://www.youtube.com/watch?v=0jgkQYebYvQ
https://www.youtube.com/watch?v=ooP8kkhvRQI

Android Job Scheduler

I was learning about JobScheduler with a Tutorial from 2015, and when I was creating a class of this service,it complains asking me to put the annotation #RequiresApi(api = Build.VERSION_CODES.LOLLIPOP), which means, doesn't work for old versions? And if it is, GCMNetworkManager would be the one ?
which means, doesn't work for old versions?
Yes annotation specifies that API wont work under LOLLIPOP version
And if it is, GCMNetworkManager would be the one ?
Alternatives to JobScheduler are
AlarmManager
GCMNetworkManager
SyncAdapter
You should choose one that suits your need with minimal device resource utilisation.
Note: Avoid using services that run perpetually or perform periodic work, since they continue to use device resources even when they are not performing useful tasks.

Categories

Resources