Answer the question
In order to leave comments, you need to log in
AlarmReceiver how to cancel notification?
Dates and task names are taken from the database, and notifications are set by time. But if the task is deleted or the status is changed, etc., i.e. in situations where the alert should be canceled, it still fires.
I know about the cancel () method, but I don’t understand where to shove it) Please help ...
public void remind() {
/* pendingIntent.cancel();
alarmManager.cancel(pendingIntent);*/
Calendar cal = Calendar.getInstance();
Cursor cursorRemind = database.query(DBHelper.TABLE_TASKS, new String[]{DBHelper.KEY_ID, DBHelper.NAME,
DBHelper.TIME_REMIND_LONG, DBHelper.IMAGE}, DBHelper.STATUS + "=?" + " and " + DBHelper.TIME_REMIND_LONG
+ "!=?" + " and " + DBHelper.TIME_REMIND_LONG + ">?",
new String[]{String.valueOf(RepresTask.statusOn), String.valueOf(""), String.valueOf(cal.getTimeInMillis())},
null, null, DBHelper.TIME_REMIND_LONG);
Calendar time = Calendar.getInstance();
if (cursorRemind.getCount() > 0) {
while (cursorRemind.moveToNext()) {
Intent intent = new Intent(getContext(), AlarmReceiver.class);
intent.putExtra("name", cursorRemind.getString(1));
intent.putExtra("image", cursorRemind.getInt(3));
intent.setData(Uri.parse("timer:" + cursorRemind.getInt(0)));
PendingIntent pendingIntent = PendingIntent.getBroadcast(getContext(), 0, intent,
PendingIntent.FLAG_CANCEL_CURRENT);
//Intent.FLAG_GRANT_READ_URI_PERMISSION
time.setTimeInMillis(cursorRemind.getLong(2));
AlarmManager alarmManager = (AlarmManager) getActivity().getSystemService(ALARM_SERVICE);
alarmManager.cancel(pendingIntent);//
alarmManager.set(AlarmManager.RTC_WAKEUP, time.getTimeInMillis(), pendingIntent);
/*Toast mToast = Toast.makeText(
getContext(), "Reminders added to the calendar successfully for "
+ android.text.format.DateFormat.format(
"MM/dd/yy h:mm:aa",
time.getTimeInMillis()),
Toast.LENGTH_LONG);
mToast.show();*/
}
}
cursorRemind.close();
}
public class AlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Resources res = context.getResources();
String name = intent.getStringExtra("name");
int icon = intent.getIntExtra("image",0);
Intent notificationIntent = new Intent(context, MainActivity.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
notificationIntent, 0);
Notification.Builder builder = new Notification.Builder(context);
builder.setContentIntent(contentIntent)
.setSmallIcon(icon)
.setContentTitle("TimeManager")
.setContentText(name)
.setLargeIcon(BitmapFactory.decodeResource(res, icon))
.setDefaults(Notification.DEFAULT_ALL);
Notification notification = builder.build();
notification.flags |= Notification.FLAG_AUTO_CANCEL;//Отмена после нажатия на увед
NotificationManager notificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(Integer.parseInt(intent.getData().getSchemeSpecificPart()), notification);
}
}
Answer the question
In order to leave comments, you need to log in
Here's an Overflow
example
in the doc , cancel removes all alarms that match the filterEquals(Intent) filter.
In the description of filterEquals(Intent)
those. to cancel, you need to create an intent the same as for installation. push it into pendingintent and run cancel
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question