I'm trying to use a relatively simple solution to execute some code on boot. Basically I want to schedule/reschedule an Alarm at boot to execute a certain task in the future.
In my manifest I have:
<uses-permission android:name="com.android.alarm.permission.SET_ALARM"/>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<receiver android:name="com.cswt.lcyairport.alarm.AlarmReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
And my code:
@Override
public void onReceive(Context context, Intent intent) {
this.context = context;
String action = intent.getAction();
if (action.equals(Intent.ACTION_BOOT_COMPLETED)) {
// Setup alarm
scheduleAlarm();
//Intent pushIntent = new Intent(context, MainActivity.class);
//pushIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//context.startActivity(pushIntent);
}
}
private void scheduleAlarm() {
long interval = 10*1000;
Intent intentAlarm = new Intent(AlarmReceiver.ACTION_GO_TO_GATE);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intentAlarm, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, 10000, interval, pendingIntent);
}
If I uncomment the code to start the activity it works great, and I see BOOT_COMPLETED is captured by my receiver. However, trying to start the alarm does not work (also tried showing a notification and it doesn't work). How can I solve this?