Is there any way to get the receiver to automatically continue after a
reboot?
Unfortunately no. The system destroys all pending intents when the phone is turned off.
To solve your problem, you should filter on the android.intent.action.BOOT_COMPLETED
to have a BroadcastReceiver
called on device boot up. Then you can re-schedule all the needed alarms.
Something like this in your manifest :
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<receiver
android:name=".broadcasts.InitReceiver"
android:exported="false" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.TIME_SET" />
<action android:name="android.intent.action.TIMEZONE_CHANGED" />
</intent-filter>
</receiver>
You can notice that there is also TIME_SET
and TIMEZONE_CHANGED
as you probably want your alarms to work even is the user is traveling from a timezone to another.
And something like this for the broadcast.
public class YourBroadcastReceiverName extends BroadcastReceiver {
private AlarmManagerFacade alarmManager;
@Override
public void onReceive(Context context, Intent intent) {
// Retreive data related to alarms
Cursor cursor = context.getContentResolver().query(Alarm.CONTENT_URI, null,
Alarm.COLUMN_ACTIVE + " = ? ",
new String[] { String.valueOf(1) }, "");
if (cursor.moveToFirst()) {
// Schedule all the active alarms.
alarmManager = new AlarmManagerFacade(context);
do {
// TODO : Schedule alarm according to data in cursor.
} while (cursor.moveToNext());
}
cursor.close();
}
}
( This code is coming from one of my app. Some object may not be available in the Android SDK. )
In order to be able to re schedule all the alarms, you need to have them stored somewhere.
You can write your own ContentProvider for example.
- It works well with other android components thanks to the CursorAdapter widget.
- It is not the easiest solution but it's the way to go if you want to follow android guidelines.
There may be other simpler alternative to store your alarms, like using SharedPreferences.
- It's easy to use.
- But you will need to hack around to store multiple alarms in a friendly manner.
One last alternative is that you can create an object containing the information, serialize it and store it as a file on the SD Card.
- It's ugly and not flexible.
- But it not that hard to implement ...
If you want to have a closer look to the different storage options you can read about it in the docs here : http://developer.android.com/guide/topics/data/data-storage.html
I hope all this help you. :)