Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PowerManager.WakeLock on Android Devices

i am trying to implement an WakeLock in my Android App. I have the following code in my onCreat():

pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
myWakeLock = pm.newWakeLock(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON,"WakeLock for Tuner");

The second line leading to a crash. It throws a Fatal Exception. As far as I can see Android says that the first Argument is no valid wake lock level. But on the developer Site it is recommended to use FLAG_KEEP_SCREEN_ON so i am a litte bit confused (http://developer.android.com/reference/android/os/PowerManager.html#newWakeLock%28int,%20java.lang.String%29)

Do I have to use the deprecated PowerManager.FULL_WAKE_LOCK ?

The following Code, as suggested in the Question How to get an Android WakeLock to work? , isn't the right way in my opinion.

getWindow().addFlags(LayoutParams.FLAG_KEEP_SCREEN_ON);

I don't need a Wakeup for the hole App. The App is a tuner for instruments and should only stay awake when the tuner is running. The plan ist to call myWakeLock.acquire() in the startTuner() Method and analogical myWakeLock.release() in the stopTuner() Method. I can't the how to realise that with the suggested way.

Here is the full Exception Message:

04-13 19:21:14.815: E/AndroidRuntime(9452): FATAL EXCEPTION: main
04-13 19:21:14.815: E/AndroidRuntime(9452): java.lang.RuntimeException: Unable to start activity ComponentInfo{de.benediktbock.ffttest/de.benediktbock.ffttest.MainActivity}: java.lang.IllegalArgumentException: Must specify a valid wake lock level.
04-13 19:21:14.815: E/AndroidRuntime(9452):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2249)
04-13 19:21:14.815: E/AndroidRuntime(9452):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2299)
04-13 19:21:14.815: E/AndroidRuntime(9452):     at android.app.ActivityThread.access$700(ActivityThread.java:154)
04-13 19:21:14.815: E/AndroidRuntime(9452):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1284)
04-13 19:21:14.815: E/AndroidRuntime(9452):     at android.os.Handler.dispatchMessage(Handler.java:99)
04-13 19:21:14.815: E/AndroidRuntime(9452):     at android.os.Looper.loop(Looper.java:137)
04-13 19:21:14.815: E/AndroidRuntime(9452):     at android.app.ActivityThread.main(ActivityThread.java:5306)
04-13 19:21:14.815: E/AndroidRuntime(9452):     at java.lang.reflect.Method.invokeNative(Native Method)
04-13 19:21:14.815: E/AndroidRuntime(9452):     at java.lang.reflect.Method.invoke(Method.java:511)
04-13 19:21:14.815: E/AndroidRuntime(9452):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
04-13 19:21:14.815: E/AndroidRuntime(9452):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
04-13 19:21:14.815: E/AndroidRuntime(9452):     at dalvik.system.NativeStart.main(Native Method)
04-13 19:21:14.815: E/AndroidRuntime(9452): Caused by: java.lang.IllegalArgumentException: Must specify a valid wake lock level.
04-13 19:21:14.815: E/AndroidRuntime(9452):     at android.os.PowerManager.validateWakeLockParameters(PowerManager.java:488)
04-13 19:21:14.815: E/AndroidRuntime(9452):     at android.os.PowerManager.newWakeLock(PowerManager.java:474)
04-13 19:21:14.815: E/AndroidRuntime(9452):     at de.benediktbock.ffttest.MainActivity.onCreate(MainActivity.java:62)
04-13 19:21:14.815: E/AndroidRuntime(9452):     at android.app.Activity.performCreate(Activity.java:5255)
04-13 19:21:14.815: E/AndroidRuntime(9452):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1097)
04-13 19:21:14.815: E/AndroidRuntime(9452):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2213)
04-13 19:21:14.815: E/AndroidRuntime(9452):     ... 11 more
like image 467
Benedikt Bock Avatar asked Apr 13 '14 17:04

Benedikt Bock


People also ask

How do I use Wakelock on Android?

To release the wake lock, call wakelock. release() . This releases your claim to the CPU. It's important to release a wake lock as soon as your app is finished using it to avoid draining the battery.

What is Wakelock permission Android?

A wakelock is a powerful concept in Android that allows the developer to modify the default power state of their device. The danger of using a wakelock in an application is that it will reduce the battery life of a device.

What does it mean when an app holds a Wakelock?

A wakelock, in layman's terms, is just a way for an app to keep the CPU/screen/other things awake when the phone is idle in order to perform a specific background task.


2 Answers

But on the developer Site it is recommended to use FLAG_KEEP_SCREEN_ON

That is referring to an alternative to using WakeLock, if your objective is simply to keep the screen on while some of your UI is in the foreground.

Do I have to use the deprecated PowerManager.FULL_WAKE_LOCK ?

That would depend upon what you are trying to do. You have to use one of those constants on PowerManager in newWakeLock().

The App is a tuner for instruments and should only stay awake when the tuner is running. The plan ist to call myWakeLock.acquire() in the startTuner() Method and analogical myWakeLock.release() in the stopTuner() Method. I can't the how to realise that with the suggested way.

Call setKeepScreenOn(true) on some View in your tuner UI when you want to keep the screen awake. Call setKeepScreenOn(false) on some View in your tuner UI when you want normal screen behavior to resume. In between those calls, so long as your tuner UI is in the foreground, the screen will not turn off. As a bonus, you do not need the WAKE_LOCK permission.

like image 156
CommonsWare Avatar answered Oct 01 '22 15:10

CommonsWare


int PROXIMITY_WAKE_LOCK = 32;
PowerManager mgr=(PowerManager) getSystemService(Context.POWER_SERVICE);
proximityWakeLock = mgr.newWakeLock(PROXIMITY_WAKE_LOCK, "Beam");
like image 31
xoxol_89 Avatar answered Oct 01 '22 15:10

xoxol_89