Androidアラームマネージャーで、繰り返さず、繰り返す間隔が決まっていない複数のアラームをスケジュールするにはどうすればよいですか?アラームには繰り返しパターンがないため、「setRepeating」関数を使用できません。
Sqliteデータベーステーブルにアラーム時刻を保存しています。アクティビティはこのテーブルから日付と時刻を選択し、アラームを設定する必要があります。
ループ内に異なるアラームを設定すると、最後のアラームのみが保持されます。投稿から読みました:複数のアラームを作成するにはどうすればよいですか?
一意のIDをインテントに添付してから、個々のアラームイベントを設定するように指示します。しかし、それは私にはうまくいきませんでした。
この一意のIDを処理するためにマニフェストファイルに追加する必要があるものはありますか?
アクティビティ「RegularSchedule」のコードはであり、アラームイベントを1つだけ作成します。
while (notifCursor.moveToNext()) {
Intent intent = new Intent(RegularSchedule.this,
RepeatingAlarm.class);
// The cursor returns first column as unique ID
intent.setData(Uri.parse("timer:" + notifCursor.getInt(0)));
PendingIntent sender = PendingIntent.getBroadcast(
RegularSchedule.this, 0, intent, 0);
// Setting time in milliseconds taken from database table
cal.setTimeInMillis(notifCursor.getLong(1));
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), sender);
}
詳細やコードスニペットが必要な場合はお知らせください。
マニフェストファイル(ここではRepeatingAlarmはBroadcastReceiverを拡張します):
<receiver android:name=".user_alerts.RepeatingAlarm" android:process=":remote" />
<activity android:name=".user_alerts.RegularSchedule"
android:label="@string/reg_schedule_title" android:theme="@android:style/Theme.Light">
</activity>
RepeatingAlarm:
public class RepeatingAlarm extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
.......
// The PendingIntent to launch our activity if the user selects this notification
Intent notificationIntent = new Intent (context, DisplayReminder.class);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
// Set the info for the views that show in the notification panel.
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
notification.defaults |= Notification.DEFAULT_SOUND;
notification.defaults |= Notification.DEFAULT_VIBRATE;
notification.defaults |= Notification.DEFAULT_LIGHTS;
mNotificationManager.notify(2425, notification);