更新AlarmManager(Android)的时间

Eir*_*ikO 6 notifications android alarmmanager

我使用AlarmManager在事件日期和时间显示事件的通知.但是,当事件更新时,如何更新AlarmManager将PendingIndent发送到我的应用程序的时间?

创建事件时,将调用以下代码:

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);
    }
Run Code Online (Sandbox Code Playgroud)

调用的缩进是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);
     }
Run Code Online (Sandbox Code Playgroud)

Eir*_*ikO 7

我找到了解决方案..我发现第二个参数可以getBroadcast(Context context, int requestCode, Intent intent, int flags)产生差异,即使文档说明了这一点

requestCode发件人的私有请求代码(当前未使用).

当每个事件使用请求ID时,将更新警报并为每个事件创建警报.

原因是使用两个不同的请求代码,filterEquals(Intent)将是错误的.文件AlarmManager set(...)说:

如果过去发生时间,将立即触发警报.如果已经安排了此Intent的警报(由两个意图定义相等 filterEquals(Intent)),则它将被删除并替换为此.

我也换PendingIntent.FLAG_ONE_SHOTPendingIndent.FLAG_CANCEL_CURRENT.