2

I have two activitys in my app. The first activity acquires a wakelock that is still present even when this activity is destroyed. This activity also sets an alarm whcih starts the second activity. I want the second activity to release the wakelock that was acquired by the first activty.

So bassically:

First activity acquires wakelock >> First activity is destroyed >> Wakelock still acquired >> canender (alarm) opens a new activity (Second Activity) >> Second activity releases wakelock??

The question is how do i release a wakelock in a different activity to where the wakelock has been acquired?

This is the code i am using to acquire the wakelock in the first activity:

    WakeLock wl;
    PowerManager pM = (PowerManager)getSystemService(Context.POWER_SERVICE);
    wl = pM.newWakeLock(PowerManager.FULL_WAKE_LOCK, "wakeLock");
   wl.acquire();

Is there any code i could use to release the wakelock in the second activty?

Konstantin Dinev
  • 34,219
  • 14
  • 75
  • 100
Jack Trowbridge
  • 3,175
  • 9
  • 32
  • 56

2 Answers2

11

Store the wake lock in a static variable that both activities can access it. Like this:

class Globals {
    public static WakeLock wakelock;
}

In your first activity:

PowerManager pM = (PowerManager)getSystemService(Context.POWER_SERVICE);
WakeLock wl = pM.newWakeLock(PowerManager.FULL_WAKE_LOCK, "wakeLock");
wl.acquire();
Globals.wakelock = wl; // Save in a place where it won't go

  // away when this Activity is done

In your second activity:

if (Globals.wakelock != null) {
    Globals.wakelock.release();
    Globals.wakelock = null; // We don't have a wake lock any more
}
David Wasser
  • 93,459
  • 16
  • 209
  • 274
  • You meant `if(Globals.wakelock.isHeld()) Globals.wakelock.release();` – Mr_and_Mrs_D Apr 28 '13 at 00:18
  • @Mr_and_Mrs_D Um, no. I didn't. The code you provided could cause a NullPointerException. There are, of course, other ways to do this. – David Wasser Apr 28 '13 at 08:15
  • You just checked for null one line above – Mr_and_Mrs_D Apr 28 '13 at 12:13
  • @Mr_and_Mrs_D Ah, so you want to add an **additional** `if` statement. Actually, if you call `acquire()` you don't need to check `isHeld()` before calling `release()`. – David Wasser Apr 28 '13 at 12:52
  • 1
    [bad practice](http://stackoverflow.com/questions/7182002/correct-pattern-to-acquire-a-wakelock-in-a-broadcastreceiver-and-release-it-in-a) – Mr_and_Mrs_D Apr 28 '13 at 14:04
  • Why do you think this is bad practice? – Marian Paździoch Oct 06 '15 at 09:25
  • @MarianPaździoch the "bad practice" refers to the conversation in the comments about whether or not a call to `isHeld()` is required before calling `release()`. The linked question/answer claims it is "best practice" to do so. IMHO Just because one person thinks it is a good idea doesn't automagically make this a "best practice", and I don't agree with that person's reasoning anyway. See the [linked question/answer](http://stackoverflow.com/a/7182665/769265) for more details. – David Wasser Oct 06 '15 at 14:28
1
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 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 static void setWakeup(boolean status) {
    Intent wakelock_Intent = new Intent(CUSTOM_INTENT);
    wakelock_Intent.putExtra("on", status);
    getActivityReference().sendBroadcast(wakelock_Intent);
}

the above would be defined in a service ..
then we can call this method from anywhere since its static to alter the cpu wakelock

Mr_and_Mrs_D
  • 32,208
  • 39
  • 178
  • 361
j2emanue
  • 60,549
  • 65
  • 286
  • 456