xml文件中的頂層佈局添加屬性:android
android:keepScreenOn="true"
在Window設置flag:ide
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
讓CPU保持運轉的方式,因此須要權限佈局
<uses-permission android:name="android.permission.WAKE_LOCK" />
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); PowerManager powerManager = (PowerManager)getSystemService(POWER_SERVICE); if (powerManager != null) { mWakeLock = powerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK, "WakeLock"); } } @Override protected void onResume() { super.onResume(); if (mWakeLock != null) { mWakeLock.acquire(); } } @Override protected void onPause() { super.onPause(); if (mWakeLock != null) { mWakeLock.release(); } }