P. Savrov P. Savrov - 3 months ago 26
Android Question

How to use Resultreceiver and AlarmManager together?

According to this article, if I need to implement auto deleting certain rows (older than current time) from my database only when certain activity is open, I have to use ResultReceiver:


ResultReceiver - Generic callback interface for sending results between service and activity. If your service only needs to connect with its parent application in a single place, use this approach.


and AlarmManager:


Trigger at a specified time in the future or at a recurring interval


But i faced with problem that i dont know how to use AlarmManager with ResultReceiver. I mean i dont understand how to call my IntentService from Activity.

In my Intent service i have something like that:

@Override
protected void onHandleIntent(Intent intent) {
Log.d(LOG_TAG, "Inside onHandleIntent");
ResultReceiver rec = intent.getParcelableExtra(IntentConstants.UPDATE_REMINDERS_SERVICE_RECEIVER);
int rowsDeleted = notificationService.deleteAllOld(Calendar.getInstance().getTimeInMillis());

Bundle bundle = new Bundle();
bundle.putString(IntentConstants.UPDATE_REMINDERS_SERVICE_NOTIFICATIONS_DELETED, "Rows deleted: " + rowsDeleted);
// Here we call send passing a resultCode and the bundle of extras
rec.send(Activity.RESULT_OK, bundle);

}


And in Activity:

// Starts the IntentService
public void onStartService() {
Log.d("UpdateRemindersLog", "Inside onStartService");
final Intent i = new Intent(this, UpdateRemindersService.class);
i.putExtra(IntentConstants.UPDATE_REMINDERS_SERVICE_RECEIVER, remindersReceiver);
startService(i);
}


I've tried to implement AlarmManager inside onStartService method but it doesn't work. I thought it's gonna be like this:

Intent intent = new Intent(getApplicationContext(), UpdateRemindersReceiver.class);
intent.putExtra(IntentConstants.UPDATE_REMINDERS_SERVICE_RECEIVER, remindersReceiver);
// Create a PendingIntent to be triggered when the alarm goes off
final PendingIntent pendingIntent = PendingIntent.getBroadcast(this, UpdateRemindersService.REQUEST_CODE, intent, PendingIntent.FLAG_UPDATE_CURRENT);

long firstMillis = System.currentTimeMillis(); // alarm is set right away
AlarmManager alarm = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
alarm.setInexactRepeating(AlarmManager.RTC_WAKEUP, firstMillis, 10*1000, pendingIntent);


So, can someone to explain me how i can implement AlarmManager with IntentService?
Thank you.

Answer

The target of the PendingIntent.getBroadcast() call needs to be a broadcast receiver, as the method suggests. The AlarmManager only works with Intents for those.

So you need to add a broadcast receiver that handles the AlarmManager's signal and then starts your service:

public class AlarmReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        // Start your service here
    }
}

Also, remember to register your receiver in your AndroidManifest.xml file, like this (inside your application tag):

<receiver
    android:name=".AlarmReceiver"
    android:enabled="true" />

And when you create your Intent you should now target your new receiver class (AlarmReceiver.class).

For more information on the AlarmManager class your can always check the StackOverflow documentation.

Comments