3

I am using commonsware WakefulIntentService for doing wakeful work. Is there any advantage over using the commonsware library instead of WakefulBroadcastReceiver from support library?

This is my code using the suport library

import android.support.v4.content.WakefulBroadcastReceiver;    

public class SimpleWakefulReceiver extends WakefulBroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        // This is the Intent to deliver to our service.
        Intent service = new Intent(context, SimpleWakefulService.class);

        // Start the service, keeping the device awake while it is launching.
        Log.i("SimpleWakefulReceiver", "Starting service @ " + SystemClock.elapsedRealtime());
        startWakefulService(context, service);
    }
}

public class SimpleWakefulService extends IntentService {
    public SimpleWakefulService() {
        super("SimpleWakefulService");
    }

    @Override
    protected void onHandleIntent(Intent intent) {

        Log.i("SimpleWakefulReceiver", "Completed service @ " + SystemClock.elapsedRealtime());
        SimpleWakefulReceiver.completeWakefulIntent(intent);
    }
}

This is the documentation.

  1. What are the differences between them?
  2. Where should i use the commonsware library instead of suport library?
Armand
  • 23,463
  • 20
  • 90
  • 119
Darish
  • 11,032
  • 5
  • 50
  • 70
  • 2
    They solve the same basic problem. `WakefulBroadcastReceiver` could, at least in theory, be used (carefully!) with a regular `Service`, and so it is more flexible. It is also part of the Android Support package, which you may already be using for other stuff, and therefore it saves you a dependency. OTOH, between April 2008 and August 2013, `WakefulBroadcastReceiver` did not exist, and so `WakefulIntentService` was the primary option. – CommonsWare Jan 04 '15 at 11:58
  • @CommonsWare , i saw you updated the lib some months ago. At the present, does your lib is better than WakefulBroadCastReceiver ? – CauCuKien Jan 28 '17 at 07:42
  • @CauCuKien: Rob's answer does a nice job of summarizing the differences. Beyond that, I do not know what you consider "better" to mean. – CommonsWare Jan 28 '17 at 12:27

1 Answers1

4

They are almost the same.

The support library's WakefulBroadcastReceiver takes a partial wake lock, puts the lock ID as an extra in the Intent that you are supposed to give to an IntentService where you must call completeWakefulIntent () when you're done processing. So acquiring and releasing is done in different places which is a bit of a code smell.

CommonsWare's WakefulIntentService does the acquiring and releasing of the partial wake lock itself.

You could use a regular BroadcastReceiver in combination with the WakefulIntentService if you agree that acquiring and releasing should be done in the same place.

If you don't mind that much and think it's more important to use a well known library so new developers (or you in a year's time) don't have to (re)learn something new then use the support library.

update

Also this: in the documentation for WakefulBroadcastReceiver it warns about the possibility of being interrupted and losing the wake lock. You would need to acquire your own wake lock in the IntentService to guard against that. With CommonsWare's you can just rely on it to reacquire the lock.

Rob Meeuwisse
  • 2,847
  • 1
  • 17
  • 21
  • You mentioned about the possibility of being interrupted and losing the wake lock. But I can not find where it is on official documentation. Can you please give a link or an example to detect and acquire wake lock again? – CauCuKien Jan 31 '17 at 05:58
  • 1
    @CauCuKien, See the developer docs for [WakefulBroadcastReceiver](https://developer.android.com/reference/android/support/v4/content/WakefulBroadcastReceiver.html). There is an example `SimpleWakefulService` class where the comments inside function `onHandleIntent(Intent intent)` warn for this possibility: "Note that when using this approach..." – Rob Meeuwisse Feb 01 '17 at 12:53