我有问题,取消闹铃和服务启动它。
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); } }借助 LocationPoller 意图将启动一个服务来获取设备的位置。所以我用一个alarmManager重复在一定的频率此搜索。
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.
之后,当我在一个BroadcastReceiver让我的位置,我调用另一个活动,以显示位置和火的通知。
After that when I get my location on a BroadcastReceiver, I call another Activity to show the location and fire a notification.
在这个时候,我想停止搜索的位置。所以我尽量取消alarmManager。
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.
任何线索?
感谢。
推荐答案您必须做的意图是独一无二的。
You have to make the intent unique.
做某事类似于:
locationPollerIntent.setData((Uri.parse("YOUR UNIQUE ID FOR THIS INTENT")));取回您正确的报警时
AlarmManager将检查意图的数据字段。只需设置一个唯一的URI的意图的数据。
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:
intentToStop.setData((Uri.parse("YOUR SAME UNIQUE ID FOR THIS INTENT")));报警后应取消。
更多推荐
不能停止服务,开始与alarmManager
发布评论