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:
- Have a
BroadcastReceiverthat listens to an acceptable broadcastIntent, such asIntent.ACTION_BOOT_COMPLETED(you will to declare a permission in your manifest in order to receive that intent) - When that
BroadcastReceiverreceives that intent, have it call an alarm-settingServicethat will set upAlarmManager. - 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) - You can stop that first service - your real Service will receive Intents sent by AlarmManager.
Android Service would be the way to go.
加载中,请稍侯......
精彩评论