Reputation: 11608
I use a broadcastReceiver to trigger alarms in an app that has an alarm clock feature. The class is currently very simple
public class MyReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Intent scheduledIntent = new Intent(context, AlarmUp.class);
scheduledIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(scheduledIntent);
}}
The problem is that it gets cancelled in the case of device reboot, so the alarm doesn't trigger. Is there any way to get the receiver to automatically continue after a reboot? Thx
Upvotes: 3
Views: 956
Reputation: 9912
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.
There may be other simpler alternative to store your alarms, like using SharedPreferences.
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.
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. :)
Upvotes: 4
Reputation: 11141
Edit your manifest file,
<receiver android:name=".RecieverName"
android:enabled="true"
android:exported="false">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
It will automatically start on device reboot.
also declare the permission for it,
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
Upvotes: 0