Java – PowerManager on Android devices WakeLock

I want to implement a wakelock in my Android application I have the following code in oncreat():

pm = (PowerManager) getSy@R_419_2354@Service(Context.POWER_SERVICE);
myWakeLock = pm.newWakeLock(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON,"WakeLock for Tuner");

The second line that caused the crash It throws a fatal exception As far as I know, Android says that the first argument has no effective wake-up locking level However, on the developer website, it is recommended to use flag_ KEEP_ SCREEN_ It makes me 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?

In my opinion, how to get an Android wakelock to work? The following code recommended in is not the correct method

getWindow().addFlags(LayoutParams.FLAG_KEEP_SCREEN_ON);

I don't need to wake up for the cave app The application is the instrument's tuner and should only remain awake while the tuner is running The plan calls myWakeLock. in the startTuner () method. Acquire (), calling myWakeLock. in the stopTuner () method. release(). I don't know how to achieve this in the suggested way

This is the complete 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.sy@R_419_2354@.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

Solution

This is an alternative to using wakelock if your goal is to keep the screen open when some of your UI is in the foreground

It depends on what you want to do You must use one of these constants on PowerManager in newwakelock()

If you want to keep the screen awake, call setkeepscreen on (true) on some views in the tuner UI If you want to restore normal screen behavior, call setkeepscreen on (false) on some views in the tuner UI Between these calls, the screen does not close as long as your tuner UI is in the foreground As a reward, you don't need wake_ Lock permission

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>