问题描述
我想实现我的Android应用程序的激活锁定。我在我的onCreat()以下的code:
PM =(电源管理)getSystemService(Context.POWER_SERVICE);
myWakeLock = pm.newWakeLock(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON,激活锁定为调谐器);
第二行导致崩溃。它抛出一个致命异常。至于我可以看到Android的说,第一个参数是没有有效的唤醒锁级别。但在开发者网站建议使用FLAG_KEEP_SCREEN_ON所以我很困惑豆蔻位(http://developer.android.com/reference/android/os/PowerManager.html#newWakeLock%28int,%20java.lang.String%29)
我必须使用pcated去preowerManager.FULL_WAKE_LOCK?
以下code,所建议的问题,是不是在我看来,正确的方式。
getWindow()addFlags(LayoutParams.FLAG_KEEP_SCREEN_ON)。
我不需要为孔应用唤醒。该应用程序是为仪器调谐器和调谐器运行时,只应保持清醒。该计划IST调用myWakeLock.acquire()在stopTuner()方法startTuner()方法和类比myWakeLock.release()。我不能如何实现与所建议的方法。
下面是完整的异常消息:
04-13 19:21:14.815:E / AndroidRuntime(9452):致命异常:主要
04-13 19:21:14.815:E / AndroidRuntime(9452):了java.lang.RuntimeException:无法启动活动ComponentInfo {de.benediktbock.ffttest / de.benediktbock.ffttest.MainActivity}:java.lang.IllegalArgumentException异常:必须指定一个有效的唤醒锁级别。
04-13 19:21:14.815:E / AndroidRuntime(9452):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2249)
04-13 19:21:14.815:E / AndroidRuntime(9452):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2299)
04-13 19:21:14.815:E / AndroidRuntime(9452):在android.app.ActivityThread.access $ 700(ActivityThread.java:154)
04-13 19:21:14.815:E / AndroidRuntime(9452):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1284)
04-13 19:21:14.815:E / AndroidRuntime(9452):在android.os.Handler.dispatchMessage(Handler.java:99)
04-13 19:21:14.815:E / AndroidRuntime(9452):在android.os.Looper.loop(Looper.java:137)
04-13 19:21:14.815:E / AndroidRuntime(9452):在android.app.ActivityThread.main(ActivityThread.java:5306)
04-13 19:21:14.815:E / AndroidRuntime(9452):在java.lang.reflect.Method.invokeNative(本机方法)
04-13 19:21:14.815:E / AndroidRuntime(9452):在java.lang.reflect.Method.invoke(Method.java:511)
04-13 19:21:14.815:E / AndroidRuntime(9452):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1102)
04-13 19:21:14.815:E / AndroidRuntime(9452):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
04-13 19:21:14.815:E / AndroidRuntime(9452):在dalvik.system.NativeStart.main(本机方法)
04-13 19:21:14.815:E / AndroidRuntime(9452):java.lang.IllegalArgumentException异常:产生的原因必须指定一个有效唤醒锁定级别。
04-13 19:21:14.815:E / AndroidRuntime(9452):在android.os.PowerManager.validateWakeLockParameters(PowerManager.java:488)
04-13 19:21:14.815:E / AndroidRuntime(9452):在android.os.PowerManager.newWakeLock(PowerManager.java:474)
04-13 19:21:14.815:E / AndroidRuntime(9452):在de.benediktbock.ffttest.MainActivity.onCreate(MainActivity.java:62)
04-13 19:21:14.815:E / AndroidRuntime(9452):在android.app.Activity.performCreate(Activity.java:5255)
04-13 19:21:14.815:E / AndroidRuntime(9452):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1097)
04-13 19:21:14.815:E / AndroidRuntime(9452):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2213)
04-13 19:21:14.815:E / AndroidRuntime(9452):11 ...更多
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.
That would depend upon what you are trying to do. You have to use one of those constants on PowerManager
in newWakeLock()
.
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.
这篇关于PowerManager.WakeLock在Android设备上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!