使用警报管理器删除持久性通知

时间:2016-10-14 16:47:51

标签: android android-studio alarmmanager android-notifications android-alarms

我有一个应用程序,它在每天的特定时间显示警报,我设置了一个AlarmManager来执行此操作。现在我希望我的持续通知在一小时后取消。我知道我应该制作另一个AlarmManager并取消第一个,但是如何指定它必须在“一小时”后取消?

    Calendar calender = Calendar.getInstance();
    calender.set(Calendar.HOUR_OF_DAY,01);
    calender.set(Calendar.MINUTE, 00);
    calender.set(Calendar.SECOND, 00);
    Intent intent = new Intent(getApplicationContext(), AlertReceiver.class);
    PendingIntent pendingintent = PendingIntent.getBroadcast(getApplicationContext(),100
    ,intent,PendingIntent.FLAG_UPDATE_CURRENT);
    AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calender.getTimeInMillis(),
            AlarmManager.INTERVAL_DAY, pendingintent);

这是我的接收者:

NotificationManager notificationManager = (NotificationManager) context.getSystemService(context
    .NOTIFICATION_SERVICE);
    Intent repeating_intent = new Intent(context, SurveyActivity.class);
    repeating_intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

    PendingIntent pendingIntent = PendingIntent .getActivity(context,100,repeating_intent,
            PendingIntent.FLAG_UPDATE_CURRENT);

    NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
            .setContentIntent(pendingIntent)
            .setSmallIcon(R.drawable.notiflogo)
            .setContentTitle("Alarm")
            .setContentText("This is Alarm")
            .setTicker("Hello")
            .setAutoCancel(true);

    builder.setOngoing(true); 
    notificationManager.notify(100,builder.build());

1 个答案:

答案 0 :(得分:0)

我想您知道您可以通过其ID(您在代码中将其设置为100)取消通知。要实现过期,您只需设置另一个取消通知的一次性(非重复)警报。您在显示如下通知后立即设置该警报:

notificationManager.notify(100,builder.build());

AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    Intent intent=new Intent(context,NotificationCancelReceiver.class);
    intent.putExtra("notification_id", 100);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 1, intent, 0);

    // Here's two way to fire a one-time (non-repeating) alarm in one hour
    // One way: alarmManager.set(AlarmManager.RTC,  System.currentTimeMillis() + 60 * 60 * 1000, pendingIntent); 
    // Another way:
    alarmManager.set(AlarmManager.ELAPSED_REALTIME,
                SystemClock.elapsedRealtime() + 60 * 60 * 1000, pendingIntent);
    // If you want to wake up the system with this alarm use ELAPSED_REALTIME_WAKEUP not ELAPSED_REALTIME

这是取消通知的NotificationCancelReceiver:

@Override
public void onReceive(Context context, Intent intent) {
        int id = intent.getIntExtra("notification_id", -1);
        if (id != -1) {
            NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
            notificationManager.cancel(id);
        }
}

确保您在AndroidManifest.xml

中添加了接收者
<receiver android:name=".NotificationCancelReceiver">
</receiver>

希望这有帮助!