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() callmWakeLock.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);getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
}
Reference
转载于:https://blog.51cto.com/zhangyan1158/490341