既存のアプリに複数のアラーム機能を追加する作業を行っています。以前は、アラームは 1 回だけで、すべて正常に機能していました。複数のアラームを設定できました。問題なく動作していますが、スヌーズ機能に問題があります。以前は、スヌーズは完璧に機能していました。ただし、複数のアラームを実装した後は機能しません。スヌーズ ボタンを押すと、スヌーズ アラームのダイアログが表示されません。これが私のコードです:
アラーム方法の設定
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();
}