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
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.