正在取消 Android 个警报/通知
Canceling Android Alarms / Notifications
我有一个应用程序,如果您有任何账单要支付,它会通知您。我希望每张到期账单都收到一份通知。闹钟设置为在一天中的确切时间响起。
除了一个 方面:
,我的一切都正常
我将闹钟从通知面板中滑出后,闹钟就消失了。然后,当我进入应用程序时,我总是会检查是否重置任何警报(以防用户更改日期),但同一个警报会一遍又一遍地出现。虽然已经过时了。
这是我设置闹钟的代码。我遍历每张账单,看看是否设置了警报(每月的某天):
public static void setRepeatingAlarm(Context c) {
List<Debt> debts = datasource.getAllDebt();
int count = 0;
for (Debt d : debts) {
int i;
if (String.valueOf(d.getDay()) == null) {
i = 300;
} else {
i = Integer.valueOf(d.getDay());
}
Calendar cal = Calendar.getInstance();
cal.set(Calendar.DAY_OF_MONTH, i);
cal.set(Calendar.HOUR_OF_DAY, 12);
cal.set(Calendar.MINUTE, 22);
cal.set(Calendar.SECOND, 10);
Intent intent = new Intent(c, TimeAlarm.class);
Bundle b = new Bundle();
b.putString("keyvalue", d.getName());
b.putInt("id", (int) d.getId());
intent.putExtras(b);
pendingIntent = PendingIntent.getBroadcast(c, (int) d.getId(), intent, PendingIntent.FLAG_UPDATE_CURRENT);
am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pendingIntent);
}
}
这里是我调用通知的地方:
public class TimeAlarm extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String debtName = intent.getStringExtra("keyvalue");
int id = intent.getIntExtra("id", 0);
CharSequence message = "Click to Update Balance for " + debtName;
Intent notificationIntent = new Intent(context, MainFragmentActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(context, id, notificationIntent, 0);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_notify)
.setContentTitle("Payment Due")
.setContentText(message)
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(message));
mBuilder.setContentIntent(contentIntent);
mBuilder.setAutoCancel(true);
NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
nm.notify(0, mBuilder.build());
}
}
有人可以帮我解决这些问题吗?
您可以通过将意图传递给 alarmManager.cancel( intent );
来明确取消警报
我有一个应用程序,如果您有任何账单要支付,它会通知您。我希望每张到期账单都收到一份通知。闹钟设置为在一天中的确切时间响起。
除了一个 方面:
,我的一切都正常我将闹钟从通知面板中滑出后,闹钟就消失了。然后,当我进入应用程序时,我总是会检查是否重置任何警报(以防用户更改日期),但同一个警报会一遍又一遍地出现。虽然已经过时了。
这是我设置闹钟的代码。我遍历每张账单,看看是否设置了警报(每月的某天):
public static void setRepeatingAlarm(Context c) {
List<Debt> debts = datasource.getAllDebt();
int count = 0;
for (Debt d : debts) {
int i;
if (String.valueOf(d.getDay()) == null) {
i = 300;
} else {
i = Integer.valueOf(d.getDay());
}
Calendar cal = Calendar.getInstance();
cal.set(Calendar.DAY_OF_MONTH, i);
cal.set(Calendar.HOUR_OF_DAY, 12);
cal.set(Calendar.MINUTE, 22);
cal.set(Calendar.SECOND, 10);
Intent intent = new Intent(c, TimeAlarm.class);
Bundle b = new Bundle();
b.putString("keyvalue", d.getName());
b.putInt("id", (int) d.getId());
intent.putExtras(b);
pendingIntent = PendingIntent.getBroadcast(c, (int) d.getId(), intent, PendingIntent.FLAG_UPDATE_CURRENT);
am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pendingIntent);
}
}
这里是我调用通知的地方:
public class TimeAlarm extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String debtName = intent.getStringExtra("keyvalue");
int id = intent.getIntExtra("id", 0);
CharSequence message = "Click to Update Balance for " + debtName;
Intent notificationIntent = new Intent(context, MainFragmentActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(context, id, notificationIntent, 0);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_notify)
.setContentTitle("Payment Due")
.setContentText(message)
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(message));
mBuilder.setContentIntent(contentIntent);
mBuilder.setAutoCancel(true);
NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
nm.notify(0, mBuilder.build());
}
}
有人可以帮我解决这些问题吗?
您可以通过将意图传递给 alarmManager.cancel( intent );
来明确取消警报