I’ve this class (starts after boot of android) that executes AlarmReceiver at fixed time. I added sharedpreferences to disable and enable alarms. If I disable and re-enable alarm does not run. I need reboot emulator. Can I avoid emulator reboot? thanks!
public class AutoStart extends BroadcastReceiver {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
CheckboxPreference = prefs.getBoolean("checkboxPref", true);
Calendar cal4 = Calendar.getInstance(TimeZone.getDefault(), Locale.getDefault());
cal4.set(Calendar.DATE,31);
cal4.set(Calendar.MONTH,Calendar.DECEMBER);
cal4.set(Calendar.YEAR,2012);//year...
cal4.set(Calendar.HOUR_OF_DAY, 23);
cal4.set(Calendar.MINUTE, 59);
cal4.set(Calendar.SECOND, 00);
Intent intent2 = new Intent(context, AlarmReceiver.class);
PendingIntent Alarm1 = PendingIntent.getBroadcast(context, 0, intent2, 0);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, cal4.getTimeInMillis(), DateUtils.DAY_IN_MILLIS, Alarm1);
Manifest:
<receiver android:name="AutoStart">
<intent-filter>
<action
android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.HOME" />
</intent-filter>
</receiver>
First thing you’re going to want to do is make a static method that makes the Alarm put it in
AutoStart:Call it in
AutoStart‘sonReceive()like this:Now, your Preference must be part of some
Activity. To keep things simple, we’ll take care of handling the Alarm in that Activity’sonPause();Now when the user cancels your Alarm or re-enables it, your code takes care of that. If you call cancel on a PendingIntent, it will cancel it if it is valid or do nothing if it is not, so we do not have to worry about checking to see if the Alarm already exists.