问题
I have the below code to set up an alarm from my app.
Intent intent = new Intent("MY_ALARM_NOTIFICATION");
intent.setClass(myActivity.this, OnAlarmReceive.class);
intent.putExtra("id", id);
PendingIntent pendingIntent = PendingIntent.getBroadcast(
myActivity.this, Integer.parseInt(id),
intent, PendingIntent.FLAG_UPDATE_CURRENT);
Calendar timeCal = Calendar.getInstance();
timeCal.set(Calendar.HOUR_OF_DAY, hour);
timeCal.set(Calendar.MINUTE, minutes);
timeCal.set(Calendar.DAY_OF_MONTH, day);
timeCal.set(Calendar.MONTH, month - 1);
timeCal.set(Calendar.YEAR, year);
Date date = timeCal.getTime();
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, timeCal.getTimeInMillis(), pendingIntent);
What happens when I remove my application from settings ? Do the alarms remain ?
回答1:
The events you schedule via AlarmManager
are removed when the app that scheduled them is uninstalled.
回答2:
If you would like the reminders to persist after uninstall, you will need to add them to the Google Calendar of the user (or create a calendar) with an intent
Intent intent = new Intent(Intent.ACTION_INSERT)
.setData(CalendarContract.Events.CONTENT_URI)
.putExtra(CalendarContract.EXTRA_EVENT_BEGIN_TIME, reminderDateTime.getMillis())
.putExtra(CalendarContract.EXTRA_EVENT_END_TIME, reminderDateTime.getMillis())
.putExtra(CalendarContract.Events.TITLE, "TITLE"
.putExtra(CalendarContract.Events.DESCRIPTION, eventDescription)
.putExtra(CalendarContract.Events.HAS_ALARM, true)
.putExtra(CalendarContract.Events.ALLOWED_REMINDERS, new int[]{CalendarContract.Reminders.METHOD_ALARM, CalendarContract.Reminders.METHOD_ALERT})
.putExtra(CalendarContract.Events.AVAILABILITY, CalendarContract.Events.AVAILABILITY_BUSY);
来源:https://stackoverflow.com/questions/19465314/what-happens-to-the-alarms-set-via-alarm-manager-by-an-app-if-the-app-is-uninst