Keep the screen awake throughout my activity

后端 未结 5 1157
说谎
说谎 2020-12-04 21:03

I have three activities in my app. I want to keep the screen awake when it is in the second activity. The screen should not go off in my second activity unless the \"lock\"

相关标签:
5条回答
  • 2020-12-04 21:26

    try to use this

    getWindow().addFlags(
                            WindowManager.LayoutParams.FLAG_FULLSCREEN
                                    | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    
    0 讨论(0)
  • 2020-12-04 21:30

    As discussed in the Android tutorial Keep the Screen On, you can do this in a few ways. You can set the FLAG_KEEP_SCREEN_ON on the activity's window:

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

    An XML equivalent for that is to add the attribute android:keepScreenOn="true" to the root view of your activity's layout. The advantage of setting the flag programmatically is that you can use

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

    when you no longer need to force the screen to stay on while your activity is running.

    Another way to control the screen (and certain other resources) is to use a wake lock:

    mWakeLock = ((PowerManager) getContext().getSystemService(Context.POWER_SERVICE))
        .newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK, getClass().getName());
    mWakeLock.acquire();
    // screen stays on in this section
    mWakeLock.release();
    

    The manifest will have to include this permission:

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

    However, as discussed in the tutorial, a wake lock is more appropriate for other use cases (such as a service or background task needing the CPU to keep running while the screen is off).

    0 讨论(0)
  • 2020-12-04 21:31

    I find this solution much easier:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" // Whatever your layout is
        ...
        android:keepScreenOn="true"> // Add this line
    

    Just add this to your activity layout XML.

    0 讨论(0)
  • 2020-12-04 21:33

    This code is deprecated, use this instead:

    PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
    wl = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, "My Tag");
    wl.acquire();
    

    After you finish with usage, call (best solution is to call this method in onDestroy method of some activity):

    wl.release();
    

    More about this on this link

    0 讨论(0)
  • 2020-12-04 21:34

    As per I understand your question, I think you have to use WAKE_LOCK for it in your application.

    Something like,

    final PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
    mWakeLock = pm.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK | PowerManager.ON_AFTER_RELEASE,"");    
    mWakeLock.acquire();
    

    And in your application's manifest.xml file file add this,

    <uses-permission android:name="android.permission.WAKE_LOCK" />
    
    0 讨论(0)
提交回复
热议问题