Search code examples
androidservicebroadcastreceiveralarmmanager

How to attach an AlarmManager to a BootReceiver


I have the following in an activity, it basically sets an AlarmManager when the user logs into the app. The AlarmManager then periodically calls another activity that deletes transactions from the phone's DB. All this works fine.

// get a Calendar object with current time
Calendar cal = Calendar.getInstance();
// add 5 minutes to the calendar object
cal.add(Calendar.MINUTE, 1);
Intent intent = new Intent(EntryActivity.this, AlarmReceiver.class);
intent.putExtra("alarm_message", "deleting transactions");
// In reality, you would want to have a static variable for the request code instead of 192837
PendingIntent sender = PendingIntent.getBroadcast(EntryActivity.this, 192837,
                                    intent, PendingIntent.FLAG_UPDATE_CURRENT);
// Get the AlarmManager service
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
//am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), sender);
am.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), 15000, sender);

.

public class AlarmReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        try {
            Bundle bundle = intent.getExtras();
            String message = bundle.getString("alarm_message");
            Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
            Intent myIntent = new Intent(context, SendOutstandingTransactions.class);
            myIntent.setAction("com.carefreegroup.startatboot.MyService");
            context.startService(myIntent);
        } catch (Exception e) {
            Toast.makeText(context, "There was an error somewhere, but we still"
                            + " received an alarm", Toast.LENGTH_SHORT).show();
            e.printStackTrace();
        }
    }
}

.

public class SendOutstandingTransactions extends IntentService {

    private static final String TAG = SendOutstandingTransactions.class.getSimpleName();
    NfcScannerApplication nfcscannerapplication;
    Cursor c;

    @Override
    public void onCreate() {
        nfcscannerapplication = (NfcScannerApplication)getApplication();
        super.onCreate();
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        nfcscannerapplication.loginValidate.deleteTableTransactions();
    }

    public SendOutstandingTransactions() {
        super("SendOutstandingTransactions");
    }
}// end of class

The Service that deletes the transactions periodically is called only when the user first logs into app. It runs indefinitely from that point on. What if the user reboots the phone? The service will only resume the next time the user logs in.

I have a BootReceiver but i don't know how to attach the AlarmManager. I've tried the following but ALARM_SERVICE cannot be resolved. Is this on the right lines?

public class MyBootReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {

        //  Intent myIntent = new Intent(context, SendOutstandingTransactions.class);
        //  myIntent.setAction("com.carefreegroup.startatboot.MyService");
        //  context.startService(myIntent);

        // get a Calendar object with current time
        Calendar cal = Calendar.getInstance();
        // add 5 minutes to the calendar object
        cal.add(Calendar.MINUTE, 1);
        Intent intent = new Intent(MyBootReceiver.this, AlarmReceiver.class);
        intent.putExtra("alarm_message", "deleting transactions");
        // In reality, you would want to have a static variable for the request code instead of 192837
        PendingIntent sender = PendingIntent.getBroadcast(context, 192837,
                                    intent, PendingIntent.FLAG_UPDATE_CURRENT);
        // Get the AlarmManager service
        AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
        //am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), sender);
        am.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), 15000, sender);
    }
}

Solution

  • getSystemService is available on a Context. (You receive it in the onReceive)

    You should do:

    AlarmManager am = (AlarmManager) context.getSystemService(context.ALARM_SERVICE);