Bootstrap

alarmmanagerservice.java_如何检查AlarmManager是否已经设置了警报?

与接收器一起工作的例子(最上面的答案只是与行动)。

//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(相同)操作,相同意图-动作、数据、类别、组件、标志),如果仍然有效,它将接收表示相同令牌的PendingIntent,因此可以调用Cancel()来删除它。

简而言之,PendingIntent应该具有相同的特性(操作和意图的结构)来控制它。

;