I use AlarmManager to display event notification at the date and time of the event. But how can I update the time when the AlarmManager sends a PendingIndent to my application when the event is updated?
When an event is raised, the following code is called:
public void setOneTimeAlarm() { Intent intent = new Intent(this, TimerReceiver.class); PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_ONE_SHOT); Calendar c = Calendar.getInstance(); c.set(Calendar.YEAR, year); c.set(Calendar.MONTH, month); c.set(Calendar.DAY_OF_MONTH, day-1); c.set(Calendar.HOUR_OF_DAY, 18); c.set(Calendar.MINUTE, 00); long date = c.getTimeInMillis(); mAlarmManager.set(AlarmManager.RTC_WAKEUP, date, pendingIntent); }
Called Indent - TimerReciver:
@Override public void onReceive(Context context, Intent intent) { Log.v("TimerReceiver", "onReceive called!"); Intent notificationIntent = new Intent(context, ListTests.class); PendingIntent contentIntent = PendingIntent.getActivity(context, 123, notificationIntent, PendingIntent.FLAG_CANCEL_CURRENT); NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); Resources res = context.getResources(); NotificationCompat.Builder builder = new NotificationCompat.Builder(context); long[] pattern = {0,300}; builder.setContentIntent(contentIntent) .setSmallIcon(R.drawable.ic_launcher) .setLargeIcon(BitmapFactory.decodeResource(res, R.drawable.ic_launcher)) .setTicker(res.getString(R.string.app_name)) .setWhen(System.currentTimeMillis()) .setAutoCancel(true) .setVibrate(pattern) .setContentTitle(res.getString(R.string.notification_title)) .setContentText(res.getString(R.string.notification_text)); Notification n = builder.build(); nm.notify(789, n); }
source share