编程语言
首页 > 编程语言> > java – Android设备上的PowerManager.WakeLock

java – Android设备上的PowerManager.WakeLock

作者:互联网

我想在我的Android应用程序中实现一个WakeLock.我在onCreat()中有以下代码:

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

导致崩溃的第二行.它抛出一个致命的例外.据我所知,Android说第一个Argument没有有效的唤醒锁定级别.但是在开发者网站上,建议使用FLAG_KEEP_SCREEN_ON让我感到困惑(http://developer.android.com/reference/android/os/PowerManager.html#newWakeLock%28int,%20java.lang.String%29)

我是否必须使用已弃用的PowerManager.FULL_WAKE_LOCK?

在我看来,问题How to get an Android WakeLock to work?中建议的以下代码不是正确的方法.

getWindow().addFlags(LayoutParams.FLAG_KEEP_SCREEN_ON);

我不需要为洞穴应用程序唤醒.该应用程序是仪器的调谐器,只应在调谐器运行时保持清醒状态.该计划是在startTuner()方法中调用myWakeLock.acquire(),在stopTuner()方法中调用类似myWakeLock.release().我不知道如何以建议的方式实现这一点.

这是完整的异常消息:

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

这是指使用WakeLock的替代方法,如果您的目标只是在您的某些UI位于前台时保持屏幕开启.

Do I have to use the deprecated PowerManager.FULL_WAKE_LOCK ?

这取决于你想要做什么.您必须在newWakeLock()中的PowerManager上使用其中一个常量.

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.

如果要保持屏​​幕清醒,请在调谐器UI中的某些视图上调用setKeepScreenOn(true).如果希望恢复正常的屏幕行为,请在调谐器UI中的某些视图上调用setKeepScreenOn(false).在这些调用之间,只要您的调谐器UI位于前台,屏幕就不会关闭.作为奖励,您不需要WAKE_LOCK权限.

标签:java,android,wakelock,powermanager
来源: https://codeday.me/bug/20190715/1467277.html