Rui
Rui

Reputation: 5920

Android BroadcastReceiver on BOOT_COMPLETED does not start alarm

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?

Upvotes: 3

Views: 4721

Answers (3)

ayon
ayon

Reputation: 2180

You have to use different request Code and save them somewhere to use again after device reboots.You have to use the same request Code that you have used to set your Alarm with PendingIntent. Then it will work after reboot. And also you have to make sure that request Code isn't used again because Say you set an Alarm with a request Code 0 wit.h PendingIntent, then if you set another Alarm request Code 0 then your previous Alarm will be replaced by the newer one. So, you'll not get the first Alarm.

Edit: Set alarm with the following snippet

public void SetAlarm(Calendar calendar, int reqCode) { 

 String dateName = idea.getText().toString(); String dateNote = 
 note.getText().toString(); Log.d("SetAlarm Texts", "Date : " + dateName + " 
 Note: " + dateNote);

Intent myIntent = new Intent(mActivity, AlarmReceiver.class);

myIntent.putExtra("title", "Her : " + dateName);

myIntent.putExtra("notes", dateNote);

myIntent.putExtra("code", reqCode);

PendingIntent pendingIntent = PendingIntent.getBroadcast(mActi vity, reqCode, myIntent, 0); 

AlarmManager alarmManager = (AlarmManager) mActivity.getSystemService(Context.ALARM_ SERVICE);

alarmManager.set(AlarmManager.RT C, calendar.getTimeInMillis(), pendingIntent);

}

Upvotes: 2

chandra sekhar
chandra sekhar

Reputation: 31

You have to use flag FLAG_UPDATE_CURRENT while creating pending intent using either getService, getActivity or getBroadCast.so that it keeps the current pending intent.it wont replace it.please reffer this link http://developer.android.com/reference/android/app/PendingIntent.html#FLAG_UPDATE_CURRENT?

Intent in = new Intent(arg0, Myservice.class);
PendingIntent pin = PendingIntent.getService(arg0, 0, in,PendingIntent.FLAG_UPDATE_CURRENT);
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(System.currentTimeMillis() + 1000 * 60);
AlarmManager am = (AlarmManager) arg0.getApplicationContext().getSystemService(arg0.ALARM_SERVICE);
am.setRepeating(AlarmManager.RTC, cal.getTimeInMillis(), 30 * 1000, pin);

Upvotes: 1

Suau
Suau

Reputation: 4738

have you added the necessary permission to your manifest ? ?

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

Upvotes: 0

Related Questions