[AndroidTips]Keep Screen On in Android

Solution 1: use PowerManager and WakeLock

In AndroidManifest.xml:

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

In your Activity:

    PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
    PowerManager.WakeLock mWakeLock = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, "My Tag");
     
    // in onResume() call
    mWakeLock.acquire();
     
    ...

    // in onPause() call
    mWakeLock.release();

 

Solution 2: use the window flag FLAG_KEEP_SCREEN_ON

Put the following code in your Activity’s onCreate method:

    @Override
    protected void onCreate(Bundle icicle) {
        super.onCreate(icicle);

        // Set keep screen on
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    }

 

Reference

你可能感兴趣的:(android)