我需要制作在不同时间发出不同通知的应用程序 我试图制作,但只有第一个工作。我不知道你能帮助我吗? 如何在同一时间发送多个报警管理器
我的主要活动代码
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
}
}
}
应用程序仅在第一次报警时工作,这通知代码和广播是真的
答案 0 :(得分:0)
要发送多个通知,请尝试使用
替换您的行notificationManager.notify(NOTIFICATION_ID_BIG_IMAGE + (int) ((new Date().getTime() / 1000L) % Integer.MAX_VALUE), mNotifyBuilder.build());
并添加
public static final int NOTIFICATION_ID_BIG_IMAGE = 101;