【Android】保持螢幕常亮喚醒狀態
阿新 • • 發佈:2018-12-14
在開發過程中有時會用到保持螢幕常亮,主要用電源控制來實現,具體實現如下:
第一步: 首先新增許可權:
<uses-permission android:name="android.permission.WAKE_LOCK"></uses-permission>
第二步:程式碼實現如下:
public class ScreenActivity extends Activity { PowerManager powerManager = null; WakeLock wakeLock = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); this.setContentView(R.layout.main); powerManager = (PowerManager)this.getSystemService(this.POWER_SERVICE); wakeLock = this.powerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK, "My Lock"); } @Override protected void onResume() { super.onResume(); wakeLock.acquire(); } @Override protected void onPause() { super.onPause(); wakeLock.release(); } }