无法停止使用 AlarmManager 启动的服务

发布于 2024-12-07 02:00:54 字数 1855 浏览 1 评论 0原文

我在取消警报和由此启动的服务时遇到问题。

我在活动中设置了警报:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

花之痕靓丽 2024-12-14 02:00:54

你必须使意图独一无二。

类似于:

locationPollerIntent.setData((Uri.parse("YOUR UNIQUE ID FOR THIS INTENT")));

AlarmManager 在为您返回正确的警报时将检查意图的数据字段。
只需为意图的数据设置一个唯一的 Uri 即可。

因此,当您取回闹钟时,请执行以下操作:

intentToStop.setData((Uri.parse("YOUR SAME UNIQUE ID FOR THIS INTENT")));

之后应取消闹钟。

You have to make the intent unique.

sth similar to:

locationPollerIntent.setData((Uri.parse("YOUR UNIQUE ID FOR THIS INTENT")));

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")));

the alarm should be cancelled after.

裸钻 2024-12-14 02:00:54

两个地方的 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.

情话难免假 2024-12-14 02:00:54

您必须传递同一个实例。

public static final Uri MYURI = Uri.parse("Some_Uri");
[...]
locationPollerIntent.setData(MyClass.MYURI);
[...]
intentToStop.setData(MyClass.MYURI);

如果每次创建 Intent 时都有两个不同的引用,则它将不起作用。

更详细的示例在这里:

http://malubu .wordpress.com/2012/06/05/take-your-time-widgets-and-alarmmanager/

You have to pass the same instance.

public static final Uri MYURI = Uri.parse("Some_Uri");
[...]
locationPollerIntent.setData(MyClass.MYURI);
[...]
intentToStop.setData(MyClass.MYURI);

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/

固执像三岁 2024-12-14 02:00:54

我遇到了同样的问题,这与 Intent“locationPollerIntent”和“intentToStop”具有相同的实例无关,但 PendingIntent 必须具有相同的实例。

停止 AlarmManager 后,该服务也会停止。

    static PendingIntent instance;

public static PendingIntent getInstance(Context context) {

    if (instance == null)
    {
        Intent intent = new Intent(context, LocationPoller.class); 
        instance = PendingIntent.getService(context, REQUEST_CODE, intent, 0);
    }

    return instance;
}

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.

    static PendingIntent instance;

public static PendingIntent getInstance(Context context) {

    if (instance == null)
    {
        Intent intent = new Intent(context, LocationPoller.class); 
        instance = PendingIntent.getService(context, REQUEST_CODE, intent, 0);
    }

    return instance;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文