4

im receiving an intent in broadcast receiver and then i start service to do more work. now what if the device is sleep and this happen, do i have to get Wakelock (AlarmManger?), and why do i need it? does my service will stop running if the device goes to sleep without getting a wakelock.

Jimmy
  • 10,427
  • 18
  • 67
  • 122

3 Answers3

5

now what if the device is sleep and this happen, do i have to get Wakelock (AlarmManger?), and why do i need it?

If the device is asleep to begin with, you will not be "receiving an intent in broadcast receiver", because the device is asleep.

do i have to get Wakelock (AlarmManger?), and why do i need it?

You don't "need it", unless you want to ensure the device stays running while you complete some work.

does my service will stop running if the device goes to sleep without getting a wakelock.

Yes.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • for the first part, i meant i will receive an intent from C2DM which i guess you could receive it while the device is asleep. and then i want to run a service and i guess i have to get wakelock to keep it running. is these assumption are correct ? – Jimmy Apr 15 '11 at 19:15
  • @Fevos: "i meant i will receive an intent from C2DM which i guess you could receive it while the device is asleep." -- yes, C2DM uses a socket connection over wireless data (AFAIK), and incoming packets on that connection will wake up the device *briefly*. "and then i want to run a service and i guess i have to get wakelock to keep it running. is these assumption are correct ?" -- yes, you will want your own `WakeLock`. Just be sure to release it when you are done. – CommonsWare Apr 15 '11 at 19:20
  • "If the device is asleep to begin with, you will not be "receiving an intent in broadcast receiver", because the device is asleep." Where this is documented? – Marian Paździoch Dec 10 '14 at 08:26
  • @MarianPaździoch: "Where this is documented?" -- I doubt that it is documented specifically, in part because it is obvious. If the CPU is not executing instructions, nobody's broadcast `Intent` is going to be processed. You are welcome to read the documentation on `PowerManager` and `PowerManager.WakeLock` to learn more about CPU states. – CommonsWare Dec 10 '14 at 11:50
  • @CommonsWare I wouldn't use word OBVIOUS in context of Android :). So if it's obviuos that BroadcastReceiver keeps device PARTIAL_WAKE_LOCK then is it also obvious that IntentService also keeps device PARTIAL_WAKE_LOCK? – Marian Paździoch Dec 10 '14 at 11:57
  • @MarianPaździoch: "So if it's obviuos that BroadcastReceiver keeps device PARTIAL_WAKE_LOCK" -- it does not keep a `PARTIAL_WAKE_LOCK`. "IntentService also keeps device PARTIAL_WAKE_LOCK?" -- it too does not keep a `PARTIAL_WAKE_LOCK`. That is why the Android Support package has `WakefulBroadcastReceiver` and I wrote `WakefulIntentService`. – CommonsWare Dec 10 '14 at 12:03
  • @CommonsWare "If the device is asleep to begin with, you will not be "receiving an intent in broadcast receiver", because the device is asleep." I've found definition of contrary here: http://stackoverflow.com/questions/27396766/do-i-need-to-acquire-wake-lock-when-invoking-a-broadcast-service – Marian Paździoch Dec 10 '14 at 12:11
1

Looks like the Android's native WakefulBroadcastReceiver would be a perfect solution for you. Need to extend this rather than the regular BroadcastReceiver and start the service in the onReceive() in the "wakeful" manner:

startWakefulService(context, service);

and signal your work is done in the service's onHandleIntent(), calling

MyWakefulReceiver.completeWakefulIntent(intent);
dud3rino
  • 517
  • 4
  • 8
0
public class WakeLockManager extends BroadcastReceiver {

    private static WakeLock mWakeLock;
    private String LCLT;

    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals(Consts.WAKELOCK_INTENT)) {
            Log.v("wakelock", "GOT THE wakelock INTENT");
            boolean on = intent.getExtras().getBoolean("on");
            if (mWakeLock == null) {
                PowerManager pm = (PowerManager) context
                        .getSystemService(Context.POWER_SERVICE);
                mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
                        "Breeze WakeLock");
            }
            if (on) {
                if (!mWakeLock.isHeld()) {
                    mWakeLock.acquire();
                    Log.v("wakelock", "acquiring wakelock");
                }
            } else {
                if (mWakeLock.isHeld()) {
                    Log.v("wakelock", "releasing wakelock");
                    mWakeLock.release();
                }
                mWakeLock = null;
            }
        }
    }
}

look at the above code ..put it in a separate class file and and in your manifest define it for some custom intent .... now that this class will respond to a custom intent ...just broadcast that intent and you can turn the wakelock on or off in your entire app since the wakelock is static..like this :

public void setWakeup(boolean status) {
    Intent wakelock_Intent = new Intent(CUSTOM_INTENT);
    wakelock_Intent.putExtra("on", status);
    this.sendBroadcast(wakelock_Intent);
}

the above would be defined in your alarmmanager code so it schedules a call

Mr_and_Mrs_D
  • 32,208
  • 39
  • 178
  • 361
j2emanue
  • 60,549
  • 65
  • 286
  • 456
  • Are you sure this "static lock" won't be wiped away when the Receiver `class` is unloaded ? See : http://stackoverflow.com/questions/6299283/broadcastreciever-life-cycle-static-variables#comment7376537_6299862 – Mr_and_Mrs_D Apr 25 '13 at 15:03
  • I am assuming the object will not be garbage collected as this is not an anonymous broadcastReceiver. If i allocate this object myself and maintain a hard reference to it why would it free'd by the system ? this is a interesting topic please advise. – j2emanue Apr 26 '13 at 20:32