Answers:
跟进发表的评论,这里是详细的解决方案。假设您注册了一个具有未决意图的重复警报,如下所示:
Intent intent = new Intent("com.my.package.MY_UNIQUE_ACTION");
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0,
intent, PendingIntent.FLAG_UPDATE_CURRENT);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.add(Calendar.MINUTE, 1);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 1000 * 60, pendingIntent);
您将检查其是否处于活动状态的方法是:
boolean alarmUp = (PendingIntent.getBroadcast(context, 0,
new Intent("com.my.package.MY_UNIQUE_ACTION"),
PendingIntent.FLAG_NO_CREATE) != null);
if (alarmUp)
{
Log.d("myTag", "Alarm is already active");
}
此处的关键是FLAG_NO_CREATE
如javadoc中所描述的:(if the described PendingIntent **does not** already exists, then simply return null
而不是创建一个新的)
alarmManager.cancel(pendingIntent)
和pendingIntent.cancel()
,此解决方案才能返回false。
对于其他可能需要这样做的人,这是一个答案。
用 adb shell dumpsys alarm
您可以知道已设置警报以及它们何时警报和间隔。此警报已被调用多少次。
adb shell dumpsys alarm | grep <e.g. package name of your app>
也可以在新的Windows系统上使用(我使用Win10)
与接收者一起工作的例子(最重要的答案就是采取行动)。
//starting
AlarmManager alarmManager = (AlarmManager) getActivity().getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(getActivity(), MyReceiver.class);
intent.setAction(MyReceiver.ACTION_ALARM_RECEIVER);//my custom string action name
PendingIntent pendingIntent = PendingIntent.getBroadcast(getActivity(), 1001, intent, PendingIntent.FLAG_CANCEL_CURRENT);//used unique ID as 1001
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), aroundInterval, pendingIntent);//first start will start asap
//and stopping
Intent intent = new Intent(getActivity(), MyReceiver.class);//the same as up
intent.setAction(MyReceiver.ACTION_ALARM_RECEIVER);//the same as up
PendingIntent pendingIntent = PendingIntent.getBroadcast(getActivity(), 1001, intent, PendingIntent.FLAG_CANCEL_CURRENT);//the same as up
alarmManager.cancel(pendingIntent);//important
pendingIntent.cancel();//important
//checking if alarm is working with pendingIntent
Intent intent = new Intent(getActivity(), MyReceiver.class);//the same as up
intent.setAction(MyReceiver.ACTION_ALARM_RECEIVER);//the same as up
boolean isWorking = (PendingIntent.getBroadcast(getActivity(), 1001, intent, PendingIntent.FLAG_NO_CREATE) != null);//just changed the flag
Log.d(TAG, "alarm is " + (isWorking ? "" : "not") + " working...");
值得一提:
如果创建的应用程序稍后(流程)重新检索相同类型的PendingIntent(相同的操作,相同的Intent的-动作,数据,类别,组件,标志),它将收到一个PendingIntent,表示相同的令牌(如果该令牌仍然有效),并且因此可以调用cancel()将其删除。
简而言之,您的PendingIntent应该具有相同的功能(操作和意图的结构)以对其进行控制。
请注意文档中有关“警报管理器”的set方法的引用:
如果已为此计划调度了一个警报(两个意图的相等性由Intent.filterEquals定义),则将其删除并替换为该警报。
如果知道要设置警报,则无需费心检查警报是否已存在。每次您的应用启动时就创建它。您将用相同的替换过去的任何警报Intent
。
如果要尝试计算以前创建的警报还有多少时间,或者是否真的需要知道这种警报是否存在,则需要使用其他方法。要回答这些问题,请在创建警报时考虑保存共享的偏好数据。您可以存储设置闹钟时的时钟时间戳,希望闹钟响起的时间以及重复周期(如果设置了重复闹钟)。
我有2个警报。我将Intent与Extras结合使用,而不是采取行动来识别事件:
Intent i = new Intent(context, AppReciever.class);
i.putExtra("timer", "timer1");
事实是,使用diff extras的意图(和警报)将不会唯一。因此,要确定哪个警报处于活动状态,我必须定义diff requestCode
-s:
boolean alarmUp = (PendingIntent.getBroadcast(context, MyApp.TIMER_1, i,
PendingIntent.FLAG_NO_CREATE) != null);
这是警报的创建方式:
public static final int TIMER_1 = 1;
public static final int TIMER_2 = 2;
PendingIntent pending = PendingIntent.getBroadcast(context, TIMER_1, i,
PendingIntent.FLAG_CANCEL_CURRENT);
setInexactRepeating(AlarmManager.RTC_WAKEUP,
cal.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pending);
pending = PendingIntent.getBroadcast(context, TIMER_2, i,
PendingIntent.FLAG_CANCEL_CURRENT);
setInexactRepeating(AlarmManager.RTC_WAKEUP,
cal.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pending);
PendingIntent.getService
刚找到另一种解决方案,它似乎对我有用
Intent myIntent = new Intent(MainActivity.this, MyReceiver.class);
boolean isWorking = (PendingIntent.getBroadcast(MainActivity.this, 0, myIntent, PendingIntent.FLAG_NO_CREATE) != null);
if (isWorking) {Log.d("alarm", "is working");} else {Log.d("alarm", "is not working");}
if(!isWorking) {
pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0, myIntent, PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
int timeNotif = 5 * 60 * 1000;//time in ms, 7*24*60*60*1000 for 1 week
Log.d("Notif", "Notification every (ms): " + timeNotif);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), timeNotif, pendingIntent);
}
尽管这里几乎每个人都给出了正确的答案,但是没有人解释“警报”的工作依据是什么
您实际上可以AlarmManager
在这里了解更多有关它的信息。但这是快速答案
您AlarmManager
基本上看到PendingIntent
了将来某个时间的时间表。因此,要取消预定的警报,您需要取消PendingIntent
。
创建广告素材时,请始终注意两件事 PendingIntent
PendingIntent.getBroadcast(context,REQUEST_CODE,intent, PendingIntent.FLAG_UPDATE_CURRENT);
PendingIntent
现在,要检查警报是否已安排或取消警报,您只需要访问该警报即可PendingIntent
。如果您使用相同的请求代码并按FLAG_NO_CREATE
如下所示使用,则可以完成此操作
PendingIntent pendingIntent=PendingIntent.getBroadcast(this,REQUEST_CODE,intent,PendingIntent.FLAG_NO_CREATE);
if (pendingIntent!=null)
alarmManager.cancel(pendingIntent);
随着FLAG_NO_CREATE
它会返回null
,如果PendingIntent
不存在。如果已经存在,则返回对现有对象的引用PendingIntent
我制作了一个简单的(无论是否愚蠢的)bash脚本,该脚本从adb shell中提取了long并将其转换为时间戳,并以红色显示。
echo "Please set a search filter"
read search
adb shell dumpsys alarm | grep $search | (while read i; do echo $i; _DT=$(echo $i | grep -Eo 'when\s+([0-9]{10})' | tr -d '[[:alpha:][:space:]]'); if [ $_DT ]; then echo -e "\e[31m$(date -d @$_DT)\e[0m"; fi; done;)
试试吧 ;)
Intent intent = new Intent("com.my.package.MY_UNIQUE_ACTION");
PendingIntent pendingIntent = PendingIntent.getBroadcast(
sqlitewraper.context, 0, intent,
PendingIntent.FLAG_NO_CREATE);
FLAG_NO_CREATE不是创建挂起的意图,因此它将布尔值设置为false。
boolean alarmUp = (PendingIntent.getBroadcast(sqlitewraper.context, 0,
new Intent("com.my.package.MY_UNIQUE_ACTION"),
PendingIntent.FLAG_NO_CREATE) != null);
if (alarmUp) {
System.out.print("k");
}
AlarmManager alarmManager = (AlarmManager) sqlitewraper.context
.getSystemService(Context.ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
System.currentTimeMillis(), 1000 * 60, pendingIntent);
在AlarmManager检查Pending Intent的值后,它为true,因为AlarmManager更新了Pending Intent的标志。
boolean alarmUp1 = (PendingIntent.getBroadcast(sqlitewraper.context, 0,
new Intent("com.my.package.MY_UNIQUE_ACTION"),
PendingIntent.FLAG_UPDATE_CURRENT) != null);
if (alarmUp1) {
System.out.print("k");
}