无法取消 kotlin 中定义的警报
Unable to Cancel the Alarm defined in kotlin
我正在通过下面定义的方法创建警报
Intent intent = new Intent(context, AlarmClockReceiver.class);
intent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
Bundle bundle = new Bundle();
bundle.putSerializable(ALARM_CLOCK, dosesReminder);
intent.putExtra(ALARM_CLOCK_BUNDLE, bundle);
intent.putExtra("MedicineName", dosesReminder.getMedicine_name());
String MedicineTime = "";
if (dosesReminder.getMorning_dosed().equals("1")) {
MedicineTime = "1 Morning ";
}
if (dosesReminder.getAfter_noon_dosed().equals("1")) {
MedicineTime = MedicineTime + "1 Afternoon ";
}
if (dosesReminder.getEvening_dosed().equals("1")) {
MedicineTime = MedicineTime + "1 Evening ";
}
intent.putExtra("MedicineTime", MedicineTime);
Log.e("Alarm Id---", "" + alarm_id);
PendingIntent pi = PendingIntent.getBroadcast(context, Integer.parseInt(alarm_id), intent, PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
long nextTime = calculateNextTime(alarm.hour,
alarm.minute, getWeeks(alarm));
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, nextTime, pi);
else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
alarmManager.setExact(AlarmManager.RTC_WAKEUP, nextTime, pi);
} else {
alarmManager.set(AlarmManager.RTC_WAKEUP, nextTime, pi);
}
Alarm works well and also able to fire Notification with above code.
我使用下面的代码来取消警报
try {
Log.e("Cancel Alarm Id---", "" + alarmClockCode);
Intent intent = new Intent(context, AlarmClockReceiver.class);
intent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
Bundle bundle = new Bundle();
bundle.putSerializable(ALARM_CLOCK, dosesReminder);
intent.putExtra(ALARM_CLOCK_BUNDLE, bundle);
intent.putExtra("MedicineName", dosesReminder.getMedicine_name());
String MedicineTime = "";
if (dosesReminder.getMorning_dosed().equals("1")) {
MedicineTime = "1 Morning ";
}
if (dosesReminder.getAfter_noon_dosed().equals("1")) {
MedicineTime = MedicineTime + "1 Afternoon ";
}
if (dosesReminder.getEvening_dosed().equals("1")) {
MedicineTime = MedicineTime + "1 Evening ";
}
intent.putExtra("MedicineTime", MedicineTime);
PendingIntent pi = PendingIntent.getActivity(context, alarmClockCode,
intent, PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager am = (AlarmManager) context
.getSystemService(Activity.ALARM_SERVICE);
am.cancel(pi);
} catch (Exception e) {
e.printStackTrace();
}
i also checked the id are same when set and cancel the Alarm but i am unable to cancel the Alarm.
您的待处理意向应与请求相同。
PendingIntent pi = PendingIntent.getBroadcast(context, Integer.parseInt(alarm_id), intent, PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE)
alarmManager.cancel(pi)
当我检查我的代码时,在取消警报时出现了一个错误,我犯了如下错误:
PendingIntent pi = PendingIntent.getActivity(context, alarmClockCode,
intent, PendingIntent.FLAG_CANCEL_CURRENT);
I used the getActivity method to fetch the Pending Intent object but while creating the Pending Intent Object i used the getBroadcast method.
问题现已解决
我正在通过下面定义的方法创建警报
Intent intent = new Intent(context, AlarmClockReceiver.class);
intent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
Bundle bundle = new Bundle();
bundle.putSerializable(ALARM_CLOCK, dosesReminder);
intent.putExtra(ALARM_CLOCK_BUNDLE, bundle);
intent.putExtra("MedicineName", dosesReminder.getMedicine_name());
String MedicineTime = "";
if (dosesReminder.getMorning_dosed().equals("1")) {
MedicineTime = "1 Morning ";
}
if (dosesReminder.getAfter_noon_dosed().equals("1")) {
MedicineTime = MedicineTime + "1 Afternoon ";
}
if (dosesReminder.getEvening_dosed().equals("1")) {
MedicineTime = MedicineTime + "1 Evening ";
}
intent.putExtra("MedicineTime", MedicineTime);
Log.e("Alarm Id---", "" + alarm_id);
PendingIntent pi = PendingIntent.getBroadcast(context, Integer.parseInt(alarm_id), intent, PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
long nextTime = calculateNextTime(alarm.hour,
alarm.minute, getWeeks(alarm));
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, nextTime, pi);
else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
alarmManager.setExact(AlarmManager.RTC_WAKEUP, nextTime, pi);
} else {
alarmManager.set(AlarmManager.RTC_WAKEUP, nextTime, pi);
}
Alarm works well and also able to fire Notification with above code.
我使用下面的代码来取消警报
try {
Log.e("Cancel Alarm Id---", "" + alarmClockCode);
Intent intent = new Intent(context, AlarmClockReceiver.class);
intent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
Bundle bundle = new Bundle();
bundle.putSerializable(ALARM_CLOCK, dosesReminder);
intent.putExtra(ALARM_CLOCK_BUNDLE, bundle);
intent.putExtra("MedicineName", dosesReminder.getMedicine_name());
String MedicineTime = "";
if (dosesReminder.getMorning_dosed().equals("1")) {
MedicineTime = "1 Morning ";
}
if (dosesReminder.getAfter_noon_dosed().equals("1")) {
MedicineTime = MedicineTime + "1 Afternoon ";
}
if (dosesReminder.getEvening_dosed().equals("1")) {
MedicineTime = MedicineTime + "1 Evening ";
}
intent.putExtra("MedicineTime", MedicineTime);
PendingIntent pi = PendingIntent.getActivity(context, alarmClockCode,
intent, PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager am = (AlarmManager) context
.getSystemService(Activity.ALARM_SERVICE);
am.cancel(pi);
} catch (Exception e) {
e.printStackTrace();
}
i also checked the id are same when set and cancel the Alarm but i am unable to cancel the Alarm.
您的待处理意向应与请求相同。
PendingIntent pi = PendingIntent.getBroadcast(context, Integer.parseInt(alarm_id), intent, PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE)
alarmManager.cancel(pi)
当我检查我的代码时,在取消警报时出现了一个错误,我犯了如下错误:
PendingIntent pi = PendingIntent.getActivity(context, alarmClockCode,
intent, PendingIntent.FLAG_CANCEL_CURRENT);
I used the getActivity method to fetch the Pending Intent object but while creating the Pending Intent Object i used the getBroadcast method.
问题现已解决