Android WakeLock
Asked Answered
A

3

6

I have a problem with acquiring a WakeLock. It seems not to work. I am trying to acquire a FULL_WAKE_LOCK but neither the display gets enabled nor is my app able to perform tasks.

I am using the following permission: android.permission.WAKE_LOCK

My acquire code looks like this:

PowerManager pm = (PowerManager) getBaseContext().getSystemService(Context.POWER_SERVICE);
wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK|PowerManager.ACQUIRE_CAUSES_WAKEUP, "My Tag");
wl.acquire();

What am i doing wrong?

Edit: Added another flag ACQUIRE_CAUSES_WAKEUP ... but no changes in behavior

Edit2: All i am trying to do is, to play music and to wake my device up upon a certain event. The music is working perfectrly but the device stays black.

Auguste answered 1/9, 2010 at 6:2 Comment(0)
E
7

WakeLock is an Inefficient way of keeping the screen on. Instead use the WindowManager to do the magic. The following one line will suffice the WakeLock. The WakeLock Permission is also needed for this to work. Also this code is efficient than the wakeLock.

getWindow().addFlags(LayoutParams.FLAG_KEEP_SCREEN_ON);

You need not relase the WakeLock Manually. This code will allow the Android System to handle the Lock Automatically. When your application is in the Foreground then WakeLock is held and else android System releases the Lock automatically.

But if you do want to release the flag, you can do it with:

getWindow().clearFlags(LayoutParams.FLAG_KEEP_SCREEN_ON);
Engud answered 5/4, 2011 at 7:26 Comment(2)
In my tests, this only works if you call it in the activity's onCreate method. E.g. if you add FLAG_KEEP_SCREEN_ON in onCreate then try to remove it later there's no effect, and vice versa. Tested on ICS 4.0.3Cordelier
you can use it anywhere. You need to use the context.Engud
H
3
private static PowerManager.WakeLock lockStatic = null;
private static String LOCK_NAME_STATIC = "MyWakeLock";

public static void acquireStaticLock(Context context) {
    getLock(context).acquire();
}

synchronized private static PowerManager.WakeLock getLock(Context context) {
    if (lockStatic == null) {
        PowerManager mgr = (PowerManager) context
                .getSystemService(Context.POWER_SERVICE);
        lockStatic = mgr.newWakeLock(PowerManager.FULL_WAKE_LOCK,
                LOCK_NAME_STATIC);
        lockStatic.setReferenceCounted(true);
    }

    return (lockStatic);
}

Usage:

Call acquireStaticLock() when you need to acuire the lock

Call getLock(this).release(); inside activity when you need to release the lock

Also add permission in minifest file:

<uses-permission android:name="android.permission.WAKE_LOCK" />
Hate answered 27/9, 2011 at 9:15 Comment(1)
You'll need to define LOCK_NAME_STATIC to use code above. E.g. private static String LOCK_NAME_STATIC = "MyWakeLock";Cordelier
P
1

Where are you acquiring the wake lock? You will need to acquire it in the receiver of the intent, not in the service/activity that your intent starts.

Patriliny answered 1/9, 2010 at 10:58 Comment(2)
i am acquiring it in an anctivity because i have an activity set as receiverAuguste
I am not sure I understand. An Activity cannot be a BroadcastReceiver as the BroadcastReceiver is an abstract class not an interface (no multiple inheritance in Java). Do you mean you have a private static BroadcastReceiver on the activity class? All I am saying is you must take the wake lock in onReceive() in the BroadcastReceiver not in onCreate() of the Activity that you start from the service.Patriliny

© 2022 - 2024 — McMap. All rights reserved.