Correct method for setKeepScreenOn / FLAG_KEEP_SCREEN_ON

后端 未结 5 1888
灰色年华
灰色年华 2020-12-02 16:10

I am using the method setKeepScreenOn(true) and haven\'t been able to figure out how to call this in relation to the current Activity (which has a content view set). I\'ve

相关标签:
5条回答
  • 2020-12-02 16:21

    Set android:keepScreenOn in XML

    0 讨论(0)
  • 2020-12-02 16:24

    If you are doing it on a class extends View. You can simple:

    this.setKeepScreenOn(true);
    
    0 讨论(0)
  • 2020-12-02 16:30

    Try this answer:

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

    getWindow is a method defined for activities, and won't require you to find a View first.

    0 讨论(0)
  • 2020-12-02 16:34

    According to Google Docs for android Developers you've two ways to do this :

    First way :

    public class MainActivity extends Activity {
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    }
    

    Second way is to add in your xml file layout this attribute: android:keepScreenOn="true"

    0 讨论(0)
  • 2020-12-02 16:42

    As Hawk said but poorly explained.

    You can also use FLAG_KEEP_SCREEN_ON in your XML layout file.

    Note the android:keepScreenOn="true"

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:keepScreenOn="true"
        android:orientation="vertical" >
    
        <!-- whatever is in your layout -->
    
    </LinearLayout>
    

    I've now written all the choices for keeping the screen on up into a blog post:
    http://blog.blundellapps.com/tut-keep-screen-onawake-3-possible-ways/

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