如何使Android WakeLock正常工作?

我的WakeLock不能使我的设备保持唤醒状态。


在OnCreate()我有:


PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);

mWakeLock = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK | PowerManager.ON_AFTER_RELEASE, "My Tag");

mWakeLock.acquire();

然后:


new CountDownTimer(1320000, 200) {


    public void onTick(long millisUntilFinished) {

        // I update a progress bar here.                                         

    }


    public void onFinish() {

        // I finish updating the progress bar.

        mWakeLock.release();

    }

}.start();

在计时器结束之前屏幕关闭,如何使屏幕保持可见?


mWakeLock 是以前这样声明的字段:


private PowerManager.WakeLock mWakeLock;

我的设备使用Android 1.6。我真的很感激能解决这个问题。


慕标5832272
浏览 1169回答 3
3回答

守候你守候我

您的清单中是否设置了所需的权限?<uses-permission android:name="android.permission.WAKE_LOCK" />

慕姐8265434

您只需要编写以下代码:&nbsp;private PowerManager.WakeLock wl;&nbsp; &nbsp; protected void onCreate(Bundle savedInstanceState) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;super.onCreate(savedInstanceState);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; setContentView(R.layout.main);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK, "DoNjfdhotDimScreen");&nbsp; &nbsp; }//End of onCreate&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; protected void onPause() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; super.onPause();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; wl.release();&nbsp; &nbsp; &nbsp; &nbsp; }//End of onPause&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; protected void onResume() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; super.onResume();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; wl.acquire();&nbsp; &nbsp; &nbsp; &nbsp; }//End of onResume然后在清单文件中添加权限&nbsp;<uses-permission android:name="android.permission.WAKE_LOCK" />现在,您的活动将始终保持清醒状态。您可以根据需要执行其他操作w1.release()。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Android