PLEASE DO NOT USE A WAKE LOCK

This requires that you give your app an additional permission, and it is very easy to introduce bugs where you accidentally remain holding the wake lock and thus leave the screen on.

It is far, far better to use the window flag FLAG_KEEP_SCREEN_ON, which you can enable on your activity's window in your onCreate() like this:

    @Override    protected void onCreate(Bundle icicle) {        super.onCreate(icicle);        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);    }

This will make sure that the screen stays on while your window is in the foreground, and only while it is in the foreground. It greatly simplifies this common use case, eliminating any juggling you need to do as your app transitions between states.

link | edit | flag answered Jan 25 '10 at 18:33 hackbod
6,490 1 4 12

3
Does that prevent the device from sleeping? If so, the commonness of WAKE_LOCK strikes me as a shocking mistake!– Michael Cramer Jan 25 '10 at 18:52
3
Yes it keeps the screen on and prevents the device from sleeping.– hackbod Jan 26 '10 at 0:55
Thanks!! great info– user244190 Feb 8 '10 at 16:28
I am currently using a Wake Lock to make sure the device does not turn off while tracking with the GPS. When the user stops tracking I release the wake lock. If I use your suggestion above, is it possible to remove the flag after you add it. Would I need to redraw the window?– user244190 Feb 8 '10 at 20:04
Yes you can remove the flag, with the appropriate window API. You don't need to worry about causing anything to be drawn, the framework does that if needed.– hackbod Feb 9 '10 at 3:26

更多相关文章

  1. 代码中设置drawableleft
  2. android 3.0 隐藏 系统标题栏
  3. Android开发中activity切换动画的实现
  4. Android(安卓)学习 笔记_05. 文件下载
  5. Android中直播视频技术探究之—摄像头Camera视频源数据采集解析
  6. 技术博客汇总
  7. android 2.3 wifi (一)
  8. AndRoid Notification的清空和修改
  9. Android中的Chronometer

随机推荐

  1. 关于获取手机通讯录联系人Contacts被depr
  2. Android::开机自启动C程序
  3. Android(安卓)7.0后访问文件权限:android.
  4. Android--Activity运用实验
  5. Android(安卓)Binder框架实现之Parcel详
  6. android学习日记(一) 获取某一会话的所有
  7. Android(安卓)Action Bar学习(一)--基本
  8. 关于android.support.v4.app.Fragment与a
  9. Android(安卓)Studio 项目模板修改
  10. 如何不让EditText不获得焦点