gpt4 book ai didi

android - AlarmManager 的更新时间 (Android)

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:51:16 27 4
gpt4 key购买 nike

我使用 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);
}

调用的缩进是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);
}

最佳答案

我找到了解决方案。我发现即使文档说

requestCode Private request code for the sender (currently not used).

当请求 ID 用于每个事件时,警报会更新并为每个事件创建警报。

原因是对于两个不同的请求代码,filterEquals(Intent) 将为 false。 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.

我还将 PendingIntent.FLAG_ONE_SHOT 更改为 PendingIndent.FLAG_CANCEL_CURRENT

关于android - AlarmManager 的更新时间 (Android),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16763032/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com