一旦用户从通知栏中点击我的通知,我就很难关闭它。我知道 setAutoCancel 方法,但它似乎不起作用。不过,这是我的代码。
MondayNotificationService.class
@Override
public void onReceive(Context context, Intent intent) {
final NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
final NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
builder.setSmallIcon(R.drawable.icon);
builder.setContentTitle(context.getString(R.string.app_name));
if (intent.getAction().equalsIgnoreCase("FirstPeriod")) {
builder.setContentText("Microwave Engineering");
builder.setContentIntent(PendingIntent.getActivity(context, 100, new Intent(context, Monday.class), 0));
Notification notification = builder.build();
builder.setAutoCancel(true);
int notificationID = 100;
notificationManager.notify(notificationID, notification);
Handler handler = new Handler();
long delayInMilliseconds = 300000;
handler.postDelayed(new Runnable() {
public void run() {
notificationManager.cancel(100);
}
}, delayInMilliseconds);
} if (intent.getAction().equalsIgnoreCase("SecondPeriod")) {
builder.setContentText("Digital Signal Processing");
builder.setContentIntent(PendingIntent.getActivity(context, 101, new Intent(context, Monday.class), 0));
Notification notification = builder.build();
int notificationID = 101;
builder.setAutoCancel(true);
notificationManager.notify(notificationID, notification);
Handler handler = new Handler();
long delayInMilliseconds = 300000;
handler.postDelayed(new Runnable() {
public void run() {
notificationManager.cancel(101);
}
}, delayInMilliseconds);
}
周一.class
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_monday);
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
Intent firstIntent = new Intent(this, MondayNotificationService.class);
firstIntent.setAction("FirstPeriod");
PendingIntent firstAlarmIntent = PendingIntent.getBroadcast(this, 100, firstIntent, PendingIntent.FLAG_UPDATE_CURRENT);
calendar.set(2016, 3, 23, 20, 40, 0);
alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), firstAlarmIntent);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY * 7, firstAlarmIntent);
Intent secondIntent = new Intent(this, MondayNotificationService.class);
secondIntent.setAction("SecondPeriod");
PendingIntent secondAlarmIntent = PendingIntent.getBroadcast(this, 101, secondIntent, PendingIntent.FLAG_UPDATE_CURRENT);
calendar.set(2016, 3, 23, 20, 12, 0);
alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), secondAlarmIntent);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY * 7, secondAlarmIntent);
此外,当通知在 5 分钟后过期时,我打开应用程序并单击“星期一”按钮,然后所有通知都会立即显示在通知栏中(即使是过期的通知)。 任何有助于理解问题所在的帮助将不胜感激。
最佳答案
正如 Mike M. 指出的,您需要在调用 build()
之前配置 Builder
。
你有:
Notification notification = builder.build();
builder.setAutoCancel(true);
反转这些行:
builder.setAutoCancel(true);
Notification notification = builder.build();
关于android - 如何在用户单击时关闭通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36812679/