不能停止服务,开始与alarmManager

编程入门 行业动态 更新时间:2024-10-09 05:25:12
本文介绍了不能停止服务,开始与alarmManager的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有问题,取消闹铃和服务启动它。

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

本文发布于:2023-11-27 11:05:43,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1637813.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:alarmManager

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!