Restore Alarm manager after phone reboot

alarmmanager, android, widget

Solution

To do something at boot you simply do following.

First in the `manifest`, this is added under application tag:

    <receiver android:name="AlarmReceiver">
    <intent-filter>
        <action android:name="packagename.ACTION"/>
        <action android:name="packagename.ACTION2"/>
    </intent-filter>
</receiver>

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

In order for this to work you need to add permission to receive the Broadcast in the manifest with following line:

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

Then you have a class BootSetter:

public class BootSetter extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        // Do your stuff
    }
}

There is a similar post, though not completly the same here. It's about running an alarm every day at noon.

Problem

i am building a small widget for learning purpose, it simply has an configuration activity where i set the update interval. it works normally and i can create multiple instance of it. but when i reboot the phone the alarm manager stops, and the widget won't update. after some search and google'ng i learned that i have to add a BOOT COMPLETE receiver but after several attempts i failed to implement so any one has an i idea about how to add that or any good source code example on widgets.

Original source

Related problems