Android AlarmManager 和 BroadcastReceiver 运行在后台 Service
我目前在集成 AlarmManager 和 BroadcastReceiver 时遇到问题。
在我的应用程序中,我正在运行一个后台服务,无论该应用程序是否正在运行,该服务都会运行。我想我让这部分工作得很好。后台服务保留一个根据用户位置而变化的数组。但是,每天早上 8:00,我希望应用程序将数组变量重置为默认值。在网上查了很多资料后,似乎可以通过 AlarmManager (每上午 8:00 启动任务)并使用 BroadcastReceiver (接收警报并执行任务)。
所以基本上代码是这样的:
public class BackgroundService extends Service {
private ArrayList thisArray;
private BroadcastReceiver thisReceiver = new BroadcastReceiver(){
@Override
public void onReceive(Context context, Intent intent) {
setArrayToDefault();
}
}
@Override
public void onCreate(){
super.onCreate();
Calendar cal = new GregorianCalendar();
cal.add(Calendar.MINUTE, 2); //example
this.registerReceiver(thisReceiver, new IntentFilter("BackgroundService"));
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), ONE_MINUTE, "what to put here?"); //example, repeat every minute
}
private void setArrayToDefault(){
//here, the array will be changed back to default values
}
}
我的主要问题是如何设置 AlarmManager 在每次设置时调用 thisReceiver 。有什么想法吗?我的做法正确吗?
I'm currently having a problem integrating AlarmManager and BroadcastReceiver.
Im my app, I'm running a background service that runs regardless that app is running or not. I think I get this part working fine. The background service keeps an array that changes based on user's location. However, at 8:00am everyday, I want the app to reset the array variable to default. After much looking around online, it seems like the way to do this is via AlarmManager (to initiate the task every 8:00am) and using BroadcastReceiver (to receive the alarm, and perform the task).
So basically the code goes something like this:
public class BackgroundService extends Service {
private ArrayList thisArray;
private BroadcastReceiver thisReceiver = new BroadcastReceiver(){
@Override
public void onReceive(Context context, Intent intent) {
setArrayToDefault();
}
}
@Override
public void onCreate(){
super.onCreate();
Calendar cal = new GregorianCalendar();
cal.add(Calendar.MINUTE, 2); //example
this.registerReceiver(thisReceiver, new IntentFilter("BackgroundService"));
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), ONE_MINUTE, "what to put here?"); //example, repeat every minute
}
private void setArrayToDefault(){
//here, the array will be changed back to default values
}
}
My main issue is on how to set the AlarmManager to call thisReceiver everytime it's set. Any idea? Is my approach correct?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
请不要。这就是为什么用户使用任务杀手和“设置”中“管理服务”屏幕的“强制停止”来攻击我们。
您没有注册
BroadcastReceiver
,因此AlarmManager
将无法联系它。请重新设计您的服务,使其不必一直运行。无论如何,Android 将会因为老化而终止你的服务,所以如果你想要一个可靠且稳定的应用程序,你无论如何都需要重新设计。
Please don't. This is why users attack us with task killers and the Force Stop from the Manage Services screen in Settings.
You are not registering the
BroadcastReceiver
, soAlarmManager
will not be able to contact it.Please please please please please please please redesign your service such that it does not have to run all of the time. Android will be killing off your service due to old age, anyway, so if you want a reliable and stable app, you need to do this redesign, anyway.