Android:未调用的未决意图具有多个警报的打盹
Posted
技术标签:
【中文标题】Android:未调用的未决意图具有多个警报的打盹【英文标题】:Android: Pending intent not calledon snooze with multiple alarm 【发布时间】:2012-11-20 02:10:17 【问题描述】:我正在努力在我现有的应用程序中添加多个警报功能。早些时候,警报只有一个,一切正常。我已经能够设置多个警报,它也工作正常,但问题在于贪睡功能。早些时候,贪睡运行完美。但是,在多个警报实施后,它不起作用。一旦我按下贪睡按钮,我就永远不会得到关于贪睡闹钟的对话框。这是我的代码:
设置报警方式
public void setAlarm(boolean AlarmEnabled, int AlarmHour, int AlarmMin)
//Build Intent/Pending Intent for setting the alarm
Intent AlarmIntent = new Intent(MultipleAlarmActivity.this, AlarmReceiver.class);
AlarmManager AlmMgr = (AlarmManager)getSystemService(ALARM_SERVICE);
int _id = (int)System.currentTimeMillis();
AlarmIntent.putExtra("REQUEST CODE", _id);
Log.v("MultipleAlarmActivity RequestCode", ""+_id);
PendingIntent Sender = PendingIntent.getBroadcast(MultipleAlarmActivity.this, _id, AlarmIntent, 0);
//Build Calendar objects for setting alarm
Calendar curCalendar = Calendar.getInstance();
Calendar alarmCalendar = Calendar.getInstance();
//Initialize Seconds and Milliseconds to 0 for both calendars
curCalendar.set(Calendar.SECOND, 0);
curCalendar.set(Calendar.MILLISECOND, 0);
alarmCalendar.set(Calendar.SECOND, 0);
alarmCalendar.set(Calendar.MILLISECOND, 0);
//Update alarmCalendar with Alarm Hour and Minute Settings
alarmCalendar.set(Calendar.HOUR_OF_DAY, AlarmHour);
alarmCalendar.set(Calendar.MINUTE, AlarmMin);
//If Alarm Time is now or in the past, set it for tomorrow 24 hours in advance from time selected
if (alarmCalendar.getTimeInMillis() <= curCalendar.getTimeInMillis())
alarmCalendar.add(Calendar.HOUR, 24);
//Set the alarm
AlmMgr.set(AlarmManager.RTC_WAKEUP, alarmCalendar.getTimeInMillis(), Sender);
//Build the Strings for displaying the alarm time through Toast
String CalendarHourStr;
if (AlarmHour > 12)
CalendarHourStr = Integer.toString(AlarmHour - 12);
else
CalendarHourStr = Integer.toString(AlarmHour);
String CalendarMinStr = Integer.toString(AlarmMin);
if (AlarmMin < 10)
CalendarMinStr = "0" + CalendarMinStr;
String strAmPM;
if (AlarmHour < 12)
strAmPM = "AM";
else
strAmPM = "PM";
String alarmTime = Integer.toString(alarmCalendar.get(Calendar.MONTH) + 1) + "/" + Integer.toString(alarmCalendar.get(Calendar.DAY_OF_MONTH)) + "/" + Integer.toString(alarmCalendar.get(Calendar.YEAR)) + " " + CalendarHourStr + ":" + CalendarMinStr + " " + strAmPM;
Toast.makeText(this, "Alarm Set For " + alarmTime, Toast.LENGTH_LONG).show();
Log.v("MultipleAlarmActivity setAlarm", "in if loop");
Log.v("MultipleAlarmActivity setAlarm", "setAlarm called");
报警接收器
public class AlarmReceiver extends BroadcastReceiver
public static final String ALARM_ALERT_ACTION = "com.android.alarmclock.ALARM_ALERT";
public static final String ALARM_INTENT_EXTRA = "intent.extra.alarm";
@Override
public void onReceive(Context context, Intent intent)
Intent in = new Intent(Intent.ACTION_MAIN);
in.setClass(context, SnoozeActivity.class);
Log.v("AlarmReceiver RequestCode", ""+intent.getIntExtra("REQUEST CODE", 0));
in.putExtra("REQUEST CODE", intent.getIntExtra("REQUEST CODE", 0));
in.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(in);
打盹闹铃的方法
private void snooze()
//Set Calendar Value for Snooze Alarm
Calendar calendar = Calendar.getInstance();
//int snoozeTime = mMinute + SNOOZE_MIN;
calendar.add(Calendar.MINUTE, SNOOZE_MIN); //SNOOZE_MIN = 1;
long snoozeTime = calendar.getTimeInMillis();
//Build Intent and Pending Intent to Set Snooze Alarm
Intent AlarmIntent = new Intent(SnoozeActivity.this, AlarmReceiver.class);
AlarmManager AlmMgr = (AlarmManager)getSystemService(ALARM_SERVICE);
AlarmIntent.putExtra("REQUEST CODE", req_code);
PendingIntent Sender = PendingIntent.getBroadcast(SnoozeActivity.this, req_code, AlarmIntent, PendingIntent.FLAG_UPDATE_CURRENT);
AlmMgr.set(AlarmManager.RTC_WAKEUP, snoozeTime, Sender);
timer.cancel();
【问题讨论】:
【参考方案1】:我发现了问题。我使用布尔值来检查是否设置了警报。而且,我在调用 setAlarm() 时忘记将此值设置为 true。那是我的错误。
【讨论】:
以上是关于Android:未调用的未决意图具有多个警报的打盹的主要内容,如果未能解决你的问题,请参考以下文章
Android 警报管理器 setExactAndAllowWhileIdle() 在打盹模式下的 Android 7.0 Nougat 中不起作用
在 Android 12/API 31 中,地理围栏不适用于 IMMUTABLE 未决意图。为啥?