简体   繁体   English

更新AlarmManager(Android)的时间

[英]Update time for AlarmManager (Android)

I use AlarmManager to display a notification for an event at the event date and time. 我使用AlarmManager在事件日期和时间显示事件的通知。 But how can I update the time the AlarmManager sends the PendingIndent to my app, when an event is updated? 但是,当事件更新时,如何更新AlarmManager将PendingIndent发送到我的应用程序的时间?

When an event is created 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);
    }

The indent called is TimerReciver: 调用的缩进是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);
     }

I found the solution.. I turns out that the second argument to getBroadcast(Context context, int requestCode, Intent intent, int flags) makes the difference, even when the documentation says 我找到了解决方案..我发现getBroadcast(Context context, int requestCode, Intent intent, int flags)的第二个参数getBroadcast(Context context, int requestCode, Intent intent, int flags)有所不同,即使文档说

requestCode Private request code for the sender ( currently not used ). requestCode发件人的私有请求代码( 当前未使用 )。

When a request id is used for each event, the alarm is updated and alarms are created for each event. 当每个事件使用请求ID时,将更新警报并为每个事件创建警报。

The reason is that with two different request codes, the filterEquals(Intent) will be false. 原因是使用两个不同的请求代码, filterEquals(Intent)将为false。 Documentation of AlarmManager set(...) says: AlarmManager set(...)文档说:

If the time occurs in the past, the alarm will be triggered immediately. 如果过去发生时间,将立即触发警报。 If there is already an alarm for this Intent scheduled (with the equality of two intents being defined by filterEquals(Intent)) , then it will be removed and replaced by this one. 如果已经安排了此Intent的警报(两个意图的相等性由filterEquals(Intent))定义filterEquals(Intent)) ,那么它将被删除并替换为此。

I also changed PendingIntent.FLAG_ONE_SHOT to PendingIndent.FLAG_CANCEL_CURRENT . 我还将PendingIntent.FLAG_ONE_SHOT更改为PendingIndent.FLAG_CANCEL_CURRENT

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM