AlarmManager and BroadcastReceiver: is this right approach? (need to repeat task every X mins)
My application should send some data to the remote database. The problem is that sometimes there is no internet connection available (for example, there are no Wi-Fi and GSM signals). In this case I save data in local storage and want to send them as far as internet connection is up.
To do this I plan to use AlarmManager and BroadcastReceiver: invoke every X minutes an alarm and check if internet available in receiver. Then, if internet available I want to send delayed data to the database and remove alarms.
In my main Activity I have use this code to setup alarms
private void setCheckAlarm(){
alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
broadcast_intent = new Intent(Context, SendData.class);
pendingIntent = PendingIntent.getBroadcast(Context, 0, broadcast_intent, 0);
// check for internet every 5 minutes, starting for the first time in 1 minute from now
// this intervals only for testing and debug purpose
Calendar now = Calendar.getInstance();
long triggerAtTime = now.getTimeInMillis()+ (1 * 60 * 1000); // 1 minute
long repeat_alarm_every = (1 * 5 * 60 * 1000); // repeat every 5 minutes
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, triggerAtTime, repeat_alarm_every, pendingIntent);
}
and here is receiver that checks for internet and send data
public class SendData extends BroadcastReceiver {
ConnectivityManager mConnectivity;
@Override
public void onReceive(Context context, Intent intent) {
// if Network connection is OK (Wifi or Mobile) then insert data ...
mConnectivity = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
开发者_Python百科Log.i("SendData", "mConnectivity.getNetworkInfo(0)="+mConnectivity.getNetworkInfo(0));
Log.i("SendData", "mConnectivity.getNetworkInfo(1)="+mConnectivity.getNetworkInfo(1));
if ((mConnectivity.getNetworkInfo(0).getState() == NetworkInfo.State.CONNECTED)||( mConnectivity.getNetworkInfo(1).getState() == NetworkInfo.State.CONNECTED)) {
Log.i("SendData","Connectivity OK ...");
// insert data from local storage into remote db
// maybe using AsyncTask?
...
// when finished remove alarms and exit
...
} else {
// else exit and wait for next alarm
Log.i("SendData", "No network connection for the moment... will try again later!");
}
};
}
As I understand, another possible approach is to use Service. So here is my
Questions
- Is this right approach?
- Is it possible to remove alarms from receiver? If yes, how I can do this?
- Should I use AsyncTask in receiver to send data or can do this from main loop?
Thanks!
I would consider using a Sync Provider. This allows the user to globally opt out of data transmission, allows you to schedule the sync but still lets the system skip it if it needs to (low power, no connection, etc).
There's a good write up on this here. The documentation generally is pretty difficult to understand and the sample wraps up some other things in with the basic sync provider example (i.e. implementing your own Auth Provider and extending Contacts, neither of which you need, it sounds like), but once you strip it down to the minimum it's reasonably straightforward.
精彩评论