Android: prevent display from turning off

前端 未结 4 1451
囚心锁ツ
囚心锁ツ 2021-01-02 10:05

I\'ve written a small game, that is only controlled via some sensors. There is no touchscreninput or something similar.

The problem is, that after a few seconds of g

相关标签:
4条回答
  • 2021-01-02 10:36

    use WakeLock

    PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
    WakeLock mWakeLock = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK | PowerManager.ON_AFTER_RELEASE, "My Tag");
    mWakeLock.acquire();
    

    and put the permission in your manifest file

    <uses-permission android:name="android.permission.WAKE_LOCK" />
    

    dont forget to release that lock in your onStop()

    0 讨论(0)
  • 2021-01-02 10:48

    use this code in the your game's Activity as first line in the onCreate() after the super call:

    this.getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    

    This will result in the System handling the screen for you.

    Best wishes, Tim

    0 讨论(0)
  • 2021-01-02 10:48

    You can request a WakeLock as specified here: http://developer.android.com/reference/android/os/PowerManager.html

    0 讨论(0)
  • The simplest would be adding android:keepScreenOn="true" to the layout in your xml.

    0 讨论(0)
提交回复
热议问题