我想从手机触发多个通知 当我启动我的应用程序时,我必须在不同时间设置通知,之后需要触发通知,例如我已经在8:30,8:40设置了通知 如果我关闭了该应用程序,则需要显示通知我尝试了一些代码,但它仅触发了我的代码
public class AlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
//Toast.makeText(context, "ALARM!! ALARM!!", Toast.LENGTH_SHORT).show();
//Stop sound service to play sound for alarm
// context.startService(new Intent(context, AlarmSoundService.class));
//This will send a notification message and show notification in
notification tray
ComponentName comp = new ComponentName(context.getPackageName(),
AlarmNotificationService.class.getName());
startWakefulService(context, (intent.setComponent(comp)));
}
}
////////
public void setalrm(List<String> times)
{
AlarmManager mgrAlarm = (AlarmManager)
this.getSystemService(Context.ALARM_SERVICE);
ArrayList<PendingIntent> intentArray = new ArrayList<PendingIntent>();
for(int i = 0; i < times.size(); i++)
{
String time="";
SimpleDateFormat sd= new SimpleDateFormat("kk:mm:ss",
Locale.getDefault());
String[] ss;
time=times.get(i);
ss=time.split(":");
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, Integer.parseInt(ss[0]));
calendar.set(Calendar.MINUTE, Integer.parseInt(ss[1]));
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
Intent intent = new Intent(this, AlarmReceiver.class);
intent.putExtra("text","set");
intent.putExtra("time",time);
// Loop counter `i` is used as a `requestCode`
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0,
intent, PendingIntent.FLAG_ONE_SHOT);
//AlarmManager manager = (AlarmManager)
getSystemService(Context.ALARM_SERVICE);//get instance of alarm
manager
//set alarm manager with entered timer by converting into
if (Build.VERSION.SDK_INT >= 23) {
mgrAlarm.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP,
calendar.getTimeInMillis(),pendingIntent);
} else if (Build.VERSION.SDK_INT >= 19) {
mgrAlarm.setExact(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(),
pendingIntent);
} else {
mgrAlarm.set(AlarmManager.RTC_WAKEUP,calendar.
getTimeInMillis(),pendingIntent);
}
intentArray.add(pendingIntent);
}
}
//////
public class AlarmNotificationService extends IntentService {
private NotificationManager alarmNotificationManager;
//Notification ID for Alarm
public static final int NOTIFICATION_ID = 1;
public AlarmNotificationService() {
super("AlarmNotificationService");
}
@Override
public void onHandleIntent(Intent intent) {
//Send notification
sendNotification("Wake Up! Wake Up! Alarm started!!");
}
//handle notification
private void sendNotification(String msg) {
alarmNotificationManager = (NotificationManager) this
.getSystemService(Context.NOTIFICATION_SERVICE);
//get pending intent
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
new Intent(this, MainActivity.class), PendingIntent.FLAG_UPDATE_CURRENT);
//Create notification
NotificationCompat.Builder alamNotificationBuilder = new NotificationCompat.Builder(
this).setContentTitle("Alarm").setSmallIcon(R.drawable.em_logo)
.setStyle(new NotificationCompat.BigTextStyle().bigText(msg))
.setContentText(msg).setAutoCancel(true);
alamNotificationBuilder.setContentIntent(contentIntent);
//notiy notification manager about new notification
alarmNotificationManager.notify(NOTIFICATION_ID,
alamNotificationBuilder.build());
}
}