Android Regular Task (cronjob Equivalent)
Solution 1:
For Cron like tasks you have to use AlarmManager, this is a system service, for using it in your code you need to call:
AlarmManagermyAlarmManager= Context.getSystemService(Context.ALARM_SERVICE).
Full docs about AlarmManager here.
Solution 2:
The most suitable approach is through services. I learned how to write services by looking at the source code for the stock Email app that is included with Android.
The general idea is that you override the Service class, and set up alarms to activate your service. Unlike daemons and Windows services, Android services aren't always running - they start up (usually when activated by an alarm), perform work, then shut down. In some cases, you may need to acquire a partial wake lock to keep the service going until it completes the task - otherwise, Android may kill your service prematurely.
Solution 3:
If you want to build a cronjob runner then what you want is a Service:
A Service is an application component that can perform long-running operations in the background and does not provide a user interface. Another application component can start a service and it will continue to run in the background even if the user switches to another application. Additionally, a component can bind to a service to interact with it and even perform interprocess communication (IPC). For example, a service might handle network transactions, play music, perform file I/O, or interact with a content provider, all from the background.
Post a Comment for "Android Regular Task (cronjob Equivalent)"