使用警报管理器删除持久通知
Remove Persistent Notification with Alarm Manager
我有一个每天在特定时间显示警报的应用程序,我设置了一个 AlarmManager 来执行此操作。现在我希望我的持续通知在一小时后取消。我知道我应该制作另一个 AlarmManager 并取消第一个,但我如何指定它必须在 "An Hour" 之后取消?
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());
我想你知道你可以通过它的 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>
希望对您有所帮助!
我有一个每天在特定时间显示警报的应用程序,我设置了一个 AlarmManager 来执行此操作。现在我希望我的持续通知在一小时后取消。我知道我应该制作另一个 AlarmManager 并取消第一个,但我如何指定它必须在 "An Hour" 之后取消?
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());
我想你知道你可以通过它的 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>
希望对您有所帮助!