停止的Andr​​oid alarmmanager的setrepeatoid、Andr、setrepeat、alarmmanager

2023-09-06 02:22:01 作者:情深已故

我已经创建了警报,如下图所示。

I have created the alarm as shown below

Intent intent = new Intent(this, Areceiver.class);
PendingIntent sender = PendingIntent.getBroadcast(this, 1234567, intent, 0);

AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
am.setRepeating(AlarmManager.RTC_WAKEUP, timenow, 3000, sender);

我创建了一个按钮来停止报警。在onclick方法,我写了下面的code

I created a button to stop the alarm. In the onclick method i have written the following code

Intent intentstop = new Intent(this, Areceiver.class);
PendingIntent senderstop = PendingIntent.getBroadcast(this,
            0, intentstop, 0);
AlarmManager alarmManagerstop = (AlarmManager) getSystemService(ALARM_SERVICE);

alarmManagerstop.cancel(senderstop);

但作业不会停止。可能是什么问题?

But the job is not stopping. What might be the issue?

推荐答案

您给您的待定意图REQUEST_ code = 1234567,当您启动报警。但是,您使用的是0,当您试图阻止它。试图阻止,并应工作时,使用此

You gave your Pending intent a request_code = 1234567 when you start the alarm. But you are using 0 when you try to stop it. Use this when trying to stop and should work

Intent intentstop = new Intent(this, Areceiver.class);
PendingIntent senderstop = PendingIntent.getBroadcast(this,
            1234567, intentstop, 0);
AlarmManager alarmManagerstop = (AlarmManager) getSystemService(ALARM_SERVICE);

alarmManagerstop.cancel(senderstop);
 
精彩推荐
图片推荐