Android: 通知取消重复闹钟
Android: Notification cancels it's repeating alarm
我希望我的通知取消它自己的重复闹钟。但是,我什至不知道如何在 pendingIntent 创建之前传递它(稍后取消它)。
闹钟设置在这里:
public class Schedule extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
PowerManager powerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "");
wakeLock.acquire();
//next is notification code. //
...
//create intent.
Intent notificationIntent = new Intent(context, MainActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(context,
NOTIFY_ID, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
...
////
wakeLock.release();
}
public void setAlarm(Context context) {
...
Intent intent = new Intent(context, Schedule.class);
intent.putExtra("DELAY", delay);
intent.putExtra("NOTIFICATION_ID", id);
intent.putExtra("TITLE_TEXT", titleText);
intent.putExtra("BIG_TEXT", bigText);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, id, intent, PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + 1000 * 60 * delay, 1000 * 60 * delay, pendingIntent);
}
}
关键问题是我想取消通知点击时的重复警报(onRecieve 方法中的意图),但取消警报需要 pendingIntent,通知是其中的一部分。有什么办法可以欺骗系统?
使用 AlarmManager.cancel()
到 cancel
特定 alarm
并使用用于创建警报的相同 PendingIntent
。当然,您应该使用之前使用的 requestCode
(NOTIFY_ID
)。
试试这个:
Intent intent = new Intent(this, Schedule.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, NOTIFY_ID, intent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.cancel(pendingIntent);
将此代码部分添加到您的 MainActivty 的 onCreate()
方法中,因为您的 notification
将在 click
从通知面板打开它时意图 MainActivity
。
希望对你有所帮助~
我希望我的通知取消它自己的重复闹钟。但是,我什至不知道如何在 pendingIntent 创建之前传递它(稍后取消它)。
闹钟设置在这里:
public class Schedule extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
PowerManager powerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "");
wakeLock.acquire();
//next is notification code. //
...
//create intent.
Intent notificationIntent = new Intent(context, MainActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(context,
NOTIFY_ID, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
...
////
wakeLock.release();
}
public void setAlarm(Context context) {
...
Intent intent = new Intent(context, Schedule.class);
intent.putExtra("DELAY", delay);
intent.putExtra("NOTIFICATION_ID", id);
intent.putExtra("TITLE_TEXT", titleText);
intent.putExtra("BIG_TEXT", bigText);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, id, intent, PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + 1000 * 60 * delay, 1000 * 60 * delay, pendingIntent);
}
}
关键问题是我想取消通知点击时的重复警报(onRecieve 方法中的意图),但取消警报需要 pendingIntent,通知是其中的一部分。有什么办法可以欺骗系统?
使用 AlarmManager.cancel()
到 cancel
特定 alarm
并使用用于创建警报的相同 PendingIntent
。当然,您应该使用之前使用的 requestCode
(NOTIFY_ID
)。
试试这个:
Intent intent = new Intent(this, Schedule.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, NOTIFY_ID, intent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.cancel(pendingIntent);
将此代码部分添加到您的 MainActivty 的 onCreate()
方法中,因为您的 notification
将在 click
从通知面板打开它时意图 MainActivity
。
希望对你有所帮助~