无法停止使用 AlarmManager 启动的服务
我在取消警报和由此启动的服务时遇到问题。
我在活动中设置了警报:
public class AlarmStarter extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent locationPollerIntent = new Intent(this, LocationPoller.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast
(getApplicationContext(), REQUEST_CODE, locationPollerIntent, 0);
alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
SystemClock.elapsedRealtime(), 6000, pendingIntent);
}
}
LocationPoller 意图将启动一项服务来获取警报的位置设备。 所以我使用 AlarmManager 以一定的频率重复此搜索。
之后,当我在 BroadcastReceiver 上获取位置时,我调用另一个 Activity 来显示位置并触发通知。
这个时候,我想停止寻找位置。 所以我尝试取消alarmManager。
public class Notifier extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.alarm_notifier);
findViewById(R.id.btTurnOffAlarm).
setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intentToStop = new Intent(this, LocationPoller.class);
PendingIntent pendingIntentToStop = PendingIntent.getBroadcast
(getApplicationContext(), REQUEST_CODE, intentToStop, 0);
AlarmManager alarmManager = (AlarmManager)
getSystemService(ALARM_SERVICE);
alarmManager.cancel(pendingIntentToStop);
}
});
}
}
但这不起作用,它继续搜索位置。
有什么线索吗?
谢谢。
I'm having problems to cancel an alarm and a service started with it.
I set the alarm in a activity:
public class AlarmStarter extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent locationPollerIntent = new Intent(this, LocationPoller.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast
(getApplicationContext(), REQUEST_CODE, locationPollerIntent, 0);
alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
SystemClock.elapsedRealtime(), 6000, pendingIntent);
}
}
The LocationPoller intent will start a service to obtain the location of the device.
So I use an alarmManager to repeat this search in certain frequency.
After that when I get my location on a BroadcastReceiver, I call another Activity to show the location and fire a notification.
At this time, I would like to stop searching for the location.
So I try to cancel the alarmManager.
public class Notifier extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.alarm_notifier);
findViewById(R.id.btTurnOffAlarm).
setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intentToStop = new Intent(this, LocationPoller.class);
PendingIntent pendingIntentToStop = PendingIntent.getBroadcast
(getApplicationContext(), REQUEST_CODE, intentToStop, 0);
AlarmManager alarmManager = (AlarmManager)
getSystemService(ALARM_SERVICE);
alarmManager.cancel(pendingIntentToStop);
}
});
}
}
But this is not working, it continues to search for the location.
Any clues?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
你必须使意图独一无二。
类似于:
AlarmManager 在为您返回正确的警报时将检查意图的数据字段。
只需为意图的数据设置一个唯一的 Uri 即可。
因此,当您取回闹钟时,请执行以下操作:
之后应取消闹钟。
You have to make the intent unique.
sth similar to:
AlarmManager will check the data field of an intent when getting back the right alarm for you.
just set a unique Uri to the data of the intent.
so when you get back your alarm, do sth like:
the alarm should be cancelled after.
两个地方的
REQUEST_CODE
需要相同;您没有显示代码清单中定义的位置。另外,这里不需要
getApplicationContext()
——this
就可以正常工作。REQUEST_CODE
needs to be the same in both places; you do not show where that is defined in your code listings.Also, you do not need
getApplicationContext()
here --this
will work just fine.您必须传递同一个实例。
如果每次创建 Intent 时都有两个不同的引用,则它将不起作用。
更详细的示例在这里:
http://malubu .wordpress.com/2012/06/05/take-your-time-widgets-and-alarmmanager/
You have to pass the same instance.
If you have two different references each time you create the Intent, it won't work.
More detailed example here:
http://malubu.wordpress.com/2012/06/05/take-your-time-widgets-and-alarmmanager/
我遇到了同样的问题,这与 Intent“locationPollerIntent”和“intentToStop”具有相同的实例无关,但 PendingIntent 必须具有相同的实例。
停止 AlarmManager 后,该服务也会停止。
I had the same issue, its not about having the same instance of the Intent "locationPollerIntent" and "intentToStop" but you must have the same instance for the PendingIntent.
After stoping the AlarmManager, the service stops too.