开发者

Android - implement something like a Unix cronjob

I would like to perform a REST API request every X mins to update the database my application uses.

That should happen whether my application is running or not. That also means, if the user restarts their mobile, the polling should start straightaway, without the need to run the application first.

开发者_JS百科

Basically I want to implement kind of this Unix cronjob

*/5 * * * * /usr/local/bin/update_something.sh

Is that possible via the Android Services? If so, can you please give me some basic directions?

Thanks.


Yes you can use a service but a service needs to be triggered/started at some relevant point of time. You can try using classes like AlarmManager that can trigger your service every x mins.

http://developer.android.com/reference/android/app/AlarmManager.html

Edit-

To start your service at device boot you can use a broadcast receiver

<action android:name="android.intent.action.BOOT_COMPLETED" />

Chk this link out

http://www.androidcompetencycenter.com/2009/06/start-service-at-boot/


So, here is how I would do it:

  1. Have a BroadcastReceiver that listens to an acceptable broadcast Intent, such as Intent.ACTION_BOOT_COMPLETED (you will to declare a permission in your manifest in order to receive that intent)
  2. When that BroadcastReceiver receives that intent, have it call an alarm-setting Service that will set up AlarmManager.
  3. In that Service, Set up AlarmManager so that it will send an Intent to the real Service every five minutes. (I don't know the details about howAlarmManager` works)
  4. You can stop that first service - your real Service will receive Intents sent by AlarmManager.


Android Service would be the way to go.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜