0
別々の通知をするアプリケーションを別々の時間に作成する必要があります 最初に作成したのは1つだけです。私はあなたが同じ時間に複数のアラームマネージャを送ることができますどのように私に を助けることができる原因を知らない複数のAlarmManagerで複数の通知を送信する方法
私mainactivityコード
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.SECOND, 3);
PendingIntent pending_intent;
calendar.set(Calendar.YEAR,2017);
calendar.set(Calendar.MONTH,8);
calendar.set(Calendar.HOUR_OF_DAY, 10);
calendar.set(Calendar.DAY_OF_MONTH, 1);
calendar.set(Calendar.MINUTE, 1);
final Intent myIntent = new Intent(MainActivity.this, AlarmReceiver.class);
myIntent.putExtra("extra", "yes");
pending_intent = PendingIntent.getBroadcast(MainActivity.this, 1, myIntent, PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pending_intent);
AlarmManager alarmManager2 = (AlarmManager) getSystemService(ALARM_SERVICE);
Calendar calendar2 = Calendar.getInstance();
calendar2.add(Calendar.SECOND, 3);
PendingIntent pending_intent2;
calendar2.set(Calendar.YEAR,2017);
calendar2.set(Calendar.MONTH,8);
calendar2.set(Calendar.HOUR_OF_DAY, 12);
calendar2.set(Calendar.DAY_OF_MONTH, 3);
calendar2.set(Calendar.MINUTE, 1);
Intent myIntent2 = new Intent(MainActivity.this, AlarmReceiver.class);
myIntent2.putExtra("extra", "yes");
pending_intent2 = PendingIntent.getBroadcast(MainActivity.this, 2, myIntent2, PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager2.set(AlarmManager.RTC_WAKEUP, calendar2.getTimeInMillis(), pending_intent2);
AlarmManager alarmManager3 = (AlarmManager) getSystemService(ALARM_SERVICE);
Calendar calendar3 = Calendar.getInstance();
calendar3.add(Calendar.SECOND, 3);
PendingIntent pending_intent3;
calendar3.set(Calendar.YEAR,2017);
calendar3.set(Calendar.MONTH,8);
calendar3.set(Calendar.HOUR_OF_DAY, 17);
calendar3.set(Calendar.DAY_OF_MONTH, 3);
calendar3.set(Calendar.MINUTE, 1);
final Intent myIntent3 = new Intent(MainActivity.this, AlarmReceiver.class);
myIntent3.putExtra("extra", "yes");
pending_intent3 = PendingIntent.getBroadcast(MainActivity.this, 3, myIntent3, PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager3.set(AlarmManager.RTC_WAKEUP, calendar3.getTimeInMillis(), pending_intent3);
AlarmManager alarmManager4 = (AlarmManager) getSystemService(ALARM_SERVICE);
Calendar calendar4 = Calendar.getInstance();
calendar4.add(Calendar.SECOND, 3);
PendingIntent pending_intent4;
calendar4.set(Calendar.YEAR,2017);
calendar4.set(Calendar.MONTH,8);
calendar4.set(Calendar.HOUR_OF_DAY, 20);
calendar4.set(Calendar.DAY_OF_MONTH, 3);
calendar4.set(Calendar.MINUTE, 1);
final Intent myIntent4 = new Intent(MainActivity.this, AlarmReceiver.class);
myIntent4.putExtra("extra", "yes");
pending_intent4 = PendingIntent.getBroadcast(MainActivity.this, 4, myIntent4, PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager4.set(AlarmManager.RTC_WAKEUP, calendar4.getTimeInMillis(), pending_intent4);
私AlarmReceiver BroadcastReceiver
public void onReceive(Context context, Intent intent) {
Calendar calendar = Calendar.getInstance();
int day = calendar.get(Calendar.DAY_OF_MONTH);
int hour = calendar.get(Calendar.HOUR);
int year = calendar.get(Calendar.YEAR);
int min = calendar.get(Calendar.MINUTE);
int monthsss = calendar.get(Calendar.MONTH);
SharedPreferences pref = context.getSharedPreferences("MyAccount", 0); // 0 - for private mode
SharedPreferences.Editor editor = pref.edit();
String first = pref.getString("first", null); // getting String
String second = pref.getString("second", null); // getting String
String third = pref.getString("third", null); // getting String
String fourth = pref.getString("fourth", null); // getting String
if (first !="done"){
if (day== 1 && hour == 10 &&year ==2017 && min >0 && min <2 && monthsss ==8 ){
// TODO Auto-generated method stub
long when = System.currentTimeMillis();
NotificationManager notificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
Intent notificationIntent = new Intent(context, d1.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,
notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Uri sound = Uri.parse("android.resource://" +"abanob.elkomos.girgisengagement"+ "/" + R.raw.noti1);
NotificationCompat.Builder mNotifyBuilder = new NotificationCompat.Builder(
context)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(context.getResources().getString(R.string.td1))
.setContentText(context.getString(R.string.d1)).setSound(sound)
.setAutoCancel(true).setWhen(when)
.setContentIntent(pendingIntent)
.setVibrate(new long[]{1000, 1000, 1000, 1000, 1000});
notificationManager.notify(100, mNotifyBuilder.build());
editor.putString("first", "done"); // Storing string
editor.commit(); // commit changes
}
}
if (second !="done"){
if (day== 3 && hour == 12 &&year ==2017 && min >0 && min <2 && monthsss ==8 ){
// TODO Auto-generated method stub
long when = System.currentTimeMillis();
NotificationManager notificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
Intent notificationIntent = new Intent(context, d2.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,
notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Uri sound = Uri.parse("android.resource://" +"abanob.elkomos.girgisengagement"+ "/" + R.raw.noti1);
NotificationCompat.Builder mNotifyBuilder = new NotificationCompat.Builder(
context)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(context.getResources().getString(R.string.td2))
.setContentText(context.getString(R.string.d2)).setSound(sound)
.setAutoCancel(true).setWhen(when)
.setContentIntent(pendingIntent)
.setVibrate(new long[]{1000, 1000, 1000, 1000, 1000});
notificationManager.notify(100, mNotifyBuilder.build());
editor.putString("second", "done"); // Storing string
editor.commit(); // commit changes
}
}
if (third !="done"){
if (day== 3 && hour == 17 &&year ==2017 && min >0 && min <2 && monthsss ==8){
// TODO Auto-generated method stub
long when = System.currentTimeMillis();
NotificationManager notificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
Intent notificationIntent = new Intent(context, d3.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,
notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Uri sound = Uri.parse("android.resource://" +"abanob.elkomos.girgisengagement"+ "/" + R.raw.noti1);
NotificationCompat.Builder mNotifyBuilder = new NotificationCompat.Builder(
context)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(context.getResources().getString(R.string.td3))
.setContentText(context.getString(R.string.d3)).setSound(sound)
.setAutoCancel(true).setWhen(when)
.setContentIntent(pendingIntent)
.setVibrate(new long[]{1000, 1000, 1000, 1000, 1000});
notificationManager.notify(100, mNotifyBuilder.build());
editor.putString("third", "done"); // Storing string
editor.commit(); // commit changes
}
} if (fourth !="done"){
if (day== 1 && hour == 20 &&year ==2017 && min >0 && min <2 && monthsss ==8 ){
// TODO Auto-generated method stub
long when = System.currentTimeMillis();
NotificationManager notificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
Intent notificationIntent = new Intent(context, d4.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,
notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Uri sound = Uri.parse("android.resource://" +"abanob.elkomos.girgisengagement"+ "/" + R.raw.noti1);
NotificationCompat.Builder mNotifyBuilder = new NotificationCompat.Builder(
context)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(context.getResources().getString(R.string.td4))
.setContentText(context.getString(R.string.d4)).setSound(sound)
.setAutoCancel(true).setWhen(when)
.setContentIntent(pendingIntent)
.setVibrate(new long[]{1000, 1000, 1000, 1000, 1000});
notificationManager.notify(100, mNotifyBuilder.build());
editor.putString("fourth", "done"); // Storing string
editor.commit(); // commit changes
}
}
}
アプリケーション作業のみ最初のアラームでは、この通知と広範なキャストのコードは真実です
を追加しますか?つまり、「Calendar.AUGUST」は「8」ではなく「7」です。これらのアラームはすべて次の月の初めに設定されています。あなたのデバイス/エミュレータの日付/時刻を変更していますか? –
デバイスの日付と時刻を変更...最初のアラームと通知の作業が正常に完了しました –
日時の更新の間に再起動していますか? –