AlarmManager 和 BroadcastReceiver:这是正确的方法吗? (需要每 X 分钟重复一次任务)
我的应用程序应该将一些数据发送到远程数据库。问题是有时没有可用的互联网连接(例如,没有 Wi-Fi 和 GSM 信号)。在这种情况下,我将数据保存在本地存储中,并希望在互联网连接可用时发送它们。
为此,我计划使用 AlarmManager 和 BroadcastReceiver:每 X 分钟调用一次警报并检查接收器中是否有互联网可用。然后,如果互联网可用,我想将延迟数据发送到数据库并删除警报。
在我的主要活动中,我使用此代码来设置警报
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);
}
,这是检查互联网并发送数据的接收器
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);
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!");
}
};
}
据我了解,另一种可能的方法是使用服务。这是我的
问题
- 这是正确的方法吗?
- 是否可以从接收器中删除警报?如果是,我该怎么做?
- 我应该在接收器中使用 AsyncTask 来发送数据还是可以从主循环中执行此操作?
谢谢!
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);
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我会考虑使用同步提供程序。这允许用户全局选择退出数据传输,允许您安排同步,但仍然让系统在需要时跳过它(低功耗、无连接等)。
这里有一篇很好的文章。该文档通常很难理解,并且示例将一些其他内容包含在基本同步提供程序示例中(即实现您自己的身份验证提供程序并扩展联系人,这听起来像是您不需要的),但是一旦您剥离它降到最低限度,这相当简单。
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.