简体   繁体   English

Android设备上的PowerManager.WakeLock

[英]PowerManager.WakeLock on Android Devices

i am trying to implement an WakeLock in my Android App. 我想在我的Android应用程序中实现一个WakeLock。 I have the following code in my onCreat(): 我在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. 据我所知,Android说第一个Argument没有有效的唤醒锁定级别。 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 ) 但是在开发者网站上,建议使用FLAG_KEEP_SCREEN_ON,这样我就有点困惑了( 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 ? 我是否必须使用已弃用的PowerManager.FULL_WAKE_LOCK?

The following Code, as suggested in the Question How to get an Android WakeLock to work? 以下代码,如问题中建议的如何让Android WakeLock工作? , 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. 该计划是在startTuner()方法中调用myWakeLock.acquire(),在stopTuner()方法中调用类似myWakeLock.release()。 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 但在开发人员站点上,建议使用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. 这是指使用WakeLock替代方法 ,如果您的目标只是在您的某些UI位于前台时保持屏幕开启。

Do I have to use the deprecated PowerManager.FULL_WAKE_LOCK ? 我是否必须使用已弃用的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() . 您必须在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. 该计划是在startTuner()方法中调用myWakeLock.acquire(),在stopTuner()方法中调用类似myWakeLock.release()。 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. 如果要保持屏​​幕清醒,请在调谐器UI中的某些View上调用setKeepScreenOn(true) Call setKeepScreenOn(false) on some View in your tuner UI when you want normal screen behavior to resume. 如果希望恢复正常的屏幕行为,请在调谐器UI中的某些View上调用setKeepScreenOn(false) In between those calls, so long as your tuner UI is in the foreground, the screen will not turn off. 在这些调用之间,只要您的调谐器UI位于前台,屏幕就不会关闭。 As a bonus, you do not need the WAKE_LOCK permission. 作为奖励,您不需要WAKE_LOCK权限。

int PROXIMITY_WAKE_LOCK = 32;
PowerManager mgr=(PowerManager) getSystemService(Context.POWER_SERVICE);
proximityWakeLock = mgr.newWakeLock(PROXIMITY_WAKE_LOCK, "Beam");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM