protected void onPause()
{
super.onPause();
// If the screen is off then the device has been locked
PowerManager powerManager = (PowerManager) getSystemService(POWER_SERVICE);
boolean isScreenOn = powerManager.isScreenOn();
//screen locked
if (!isScreenOn) {
boolean pressed = onKeyDown(26, null);
//power button pressed
if(pressed){
//remove keyguard
getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
//start intent
Intent i = new Intent(this, VoiceRecognitionActivity.class);
startActivity(i);
}
}
}
上面的代碼確實是當按下電源按鈕時,鍵盤鎖將被解除,並且重新開始閒置的活動。跳過屏幕鎖
但是,當我按下電源按鈕時,鍵盤不會被暗淡,我必須手動解鎖。
當我按下電源按鈕時,我的活動窗口閃爍一秒鐘,顯示鍵盤鎖窗口。
喜inazaruk,感謝您的回覆。不過,我並不是想阻止手機進入睡眠狀態。 我想要做一些事情,比如當我按下電源按鈕來喚醒手機時,它會自動進入一個voiceRecognition活動,它會記錄一個語音命令。如果語音命令匹配,它將解鎖手機。 – star