scheduling and editing an event to occur at a specific time android

1.2k views Asked by At

I am building an app that needs to perform an action at a specific date and time and put a notification an hour before this task is performed.

I have searched and found that the Alarm Manager class can be used for this purpose but I am not sure how to use it. Also if in a future point to cancel or edit it once the application is closed and reopened.

I have created a SQlite database which stores the information along with the date and time of the event to happen.

Thanks

1

There are 1 answers

1
Dazzibao On BEST ANSWER

Google provides a "Scheduler" code sample which does what you want: display a notification at a given time. This sample also shows how to cancel an alarm.

Edit : I've just watched at the sample code again and you are right, the sample doesn't explain how to get the PendingIntent after the app is closed and reopened. The following code creates the PendingIntent if it doesn't exist, and if it already exists, it returns the existing PendingIntent, thanks to FLAG_UPDATE_CURRENT. So you don't have to care if the alarm has already been started or not: just get the Pending intent, and cancel it.

    Intent intent = new Intent(context, MyAlarmReceiver.class);
    pendingAlarmIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    ....
    pendingAlarmIntent.cancel();