does Alarm Manager persist even after reboot?
Asked Answered
A

3

57

I am really new to android, I have been researching about alarms. I want to alarm if there is a birthday on that day. I've have used alarm manager. I was confused because i have read that it clears after reboot. I don't have an android phone so I'm just using the emulator.

Here's my code :

public void schedAlarm() {
    AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
    Intent intent = new Intent(this, AlarmService.class);
    pendingIntent = PendingIntent.getBroadcast(this, contact.id, intent, PendingIntent.FLAG_ONE_SHOT);
    am.setRepeating(AlarmManager.RTC, timetoAlarm, nextalarm, pendingIntent);
}

I made this BroadcastRecever in replace for AlarmSerivce Here :

public void onReceive(Context context, Intent intent) {
    nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    CharSequence from = "It Birthday!";
    CharSequence message =" Greet your friend.";
    PendingIntent contentIntent = PendingIntent.getActivity(context, 0, new Intent(), 0);
    Notification notif = new Notification(R.drawable.ic_launcher, "Birthday", System.currentTimeMillis());
    notif.setLatestEventInfo(context, from, message, contentIntent);
    nm.notify(1, notif);
 }

is this enough??

Am‚lie answered 20/8, 2012 at 8:22 Comment(1)
By default, all alarms are canceled when a device shuts down.developer.android.com/training/scheduling/alarms#bootBotchy
A
103

A simple answer would be NO. But yes you can achieve this by creating a BroadCastReceiver which will start the Alarm while booting completes of the device.

Use <action android:name="android.intent.action.BOOT_COMPLETED" /> for trapping boot activity in BroadCastReceiver class.

You need to add above line in AndroidManifest.xml as follows,

<receiver android:name=".AutoStartUp" android:enabled="true" android:exported="false" android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
     <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED" />
    </intent-filter>
    </receiver>
Additory answered 20/8, 2012 at 8:26 Comment(8)
Can i just change AlarmService into a BroadCastRecever?Am‚lie
I dont know your AlarmService file's code, so i can't say yes, but i suggest you to create new one, so that if anything goes wrong, you have atleast your previous work ready :)Additory
Yes, and you need to make changes in AndroidManifest.xml , as i said in my answer.Additory
@Additory - Does Android clear all Alarms that we have set, after rebooting? If yes, we should set them after rebooting again!?Platino
@Behzad, yes if the Alarms are set by code than it will surely get clear on reboot.Additory
Please note that you also need to request the RECEIVE_BOOT_COMPLETED permission in your manifest.Rocket
Do I need to 're-register' all my alarms again in this receiver after I get a boot event?Landside
I tried with same thing but not work for me toast is not display after reboot phone.Owens
D
13

Yes , you can make AlarmManager to work even after rebooting. Perhaps this is the easiest way : add the below code in your AndroidManifest.xml:

<receiver android:name=".AlarmReceiver">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
            <action android:name="android.intent.action.QUICKBOOT_POWERON" />

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </receiver>

don't forget to include user-permission to the AndroidManifest.xml as:

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
Dibri answered 28/6, 2017 at 11:40 Comment(2)
it fires automatically just after reboot even if alarm is set an hour later.Snip
Yes, Typically this would be called after a PendingIntent which would call .AlarmReceiver after a set period of time. By adding BOOT_COMPLETED and QUICKBOOT_POWERON. In the above answer .AlarmReceiver is called on boot (incorrect action), and the PendingIntent is still lost after boot. Instead an additional receiver should be called from BOOT_COMPLETED and QUICKBOOT_POWERON that re initializes the PendingIntentHeadlong
L
3

in some phones only adding

<action android:name="android.intent.action.Boot_COMPLETED" />

does not work you also have to add

<action android:name="android.intent.action.QUICKBOOT_POWERON" />

along with previous one

Licha answered 2/8, 2017 at 13:49 Comment(1)
Documentation of the latter one? Nothing found on developer.android.com...Medlock

© 2022 - 2024 — McMap. All rights reserved.