【问题标题】:How to cancel a repeating notification on a particular date如何取消特定日期的重复通知
【发布时间】:2016-05-30 15:44:53
【问题描述】:

好吧,我已经尽力寻找上述问题的答案,但到目前为止我的努力都是徒劳的。 我在alarmmanager 类的帮助下创建了一个警报,它将定期(大约5 天)触发通知。 下面是在按钮的 onClick() 中实现报警的代码。

AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);

    Intent notificationIntent = new Intent("android.media.action.DISPLAY_NOTIFICATION");
    notificationIntent.addCategory("android.intent.category.DEFAULT");

    PendingIntent broadcast = PendingIntent.getBroadcast(this, 100, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);

    Calendar cal = Calendar.getInstance();
    if(fromDateEtxt.getText().toString().length()>0) {
        cal.add(Calendar.HOUR_OF_DAY, 10);
        alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,cal.getTimeInMillis(), 5 * 24 * 60 * 60 * 1000, broadcast);

    }

广播接收器的代码。

public class AlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
    Intent notificationIntent = new Intent(context, NotificationActivity.class);

    TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
    stackBuilder.addParentStack(NotificationActivity.class);
    stackBuilder.addNextIntent(notificationIntent);

    PendingIntent pendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);

    NotificationCompat.Builder builder = new NotificationCompat.Builder(context);

    Notification notification = builder.setContentTitle("Demo App Notification")
            .setContentText("New Notification From Demo App..")
            .setTicker("New Message Alert!")
            .setSmallIcon(R.mipmap.ic_launcher)
            .setContentIntent(pendingIntent).build();

    NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(0, notification);
}}

onClick() 方法除了启动警报之外还会更改活动。现在我面临的问题是我真的不知道如何停止重复警报。我想在特定日期停止闹钟。其次,我很困惑是使用alarm.cancel 来取消警报还是使用另一个警报来取消先前的警报,如here 所示。除此之外,我想知道是否可以从其他活动中取消警报,或者该点似乎没有必要并且可以提前设置日期限制?

【问题讨论】:

    标签: android android-intent alarmmanager android-notifications repeatingalarm


    【解决方案1】:

    您需要在AlarmManager 上调用cancel,并使用与setRepeating 相同的PendingIntent

    示例代码 -

    Intent intent = new Intent(this, AlarmReceive.class);
    PendingIntent sender = PendingIntent.getBroadcast(this, 0, intent, 0);
    AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
    
    alarmManager.cancel(sender);
    

    如果您要求在特定日期取消,您可以使用AlarmManager 设置另一个闹钟。将PendingIntent(可能带有BroadcastReceiver)传递给它,以取消类似于示例代码的重复警报。

    【讨论】:

    • 好的,对于在特定日期取消,我应该在同一活动还是在不同活动中创建新警报,因为存在 onClick() 方法的活动不会再次打开。那么在 Onclick() 方法中创建另一个警报(用于取消的警报)是否会在创建警报之前限制警报天数??
    • 我不确定您的确切用例是什么,但如果您知道单击按钮时的取消日期,您也可以在那里创建取消它的警报。跨度>
    • 我是否需要使用不同的警报管理器变量,因为它在示例代码中不清楚?
    • 我认为您不需要不同的对象。查看 AlarmManager 文档以获得更好的理解。
    猜你喜欢
    • 2023-03-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多