3 回答

TA貢獻1806條經驗 獲得超5個贊
與所有答案相反..檢查一下:
@Override
public boolean dispatchKeyEvent(KeyEvent event) {
if (event.getKeyCode() == KeyEvent.KEYCODE_POWER) {
Log.i("", "Dispath event power");
Intent closeDialog = new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
sendBroadcast(closeDialog);
return true;
}
return super.dispatchKeyEvent(event);
}
它能做什么:
每當用戶按下電源按鈕時。我們取消了對話,甚至看不到它!:) 這對我有用。(在note2上測試)

TA貢獻1865條經驗 獲得超7個贊
這是我所做的:
(1)創建一個接收器類:
public class ScreenReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
switch (action) {
case Intent.ACTION_SCREEN_OFF:
BaseActivity.unlockScreen();
break;
case Intent.ACTION_SCREEN_ON:
// and do whatever you need to do here
BaseActivity.clearScreen();
}
}
}
(2)上面調用的兩個方法如下所示:
public static void unlockScreen () {
if (current == null) return;
Log.i( Constants.TAG, "Turning on screen ... " );
Window window = current.getWindow();
window.addFlags( WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD );
window.addFlags( WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED );
window.addFlags( WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON );
}
public static void clearScreen () {
if (current == null) return;
Log.i( Constants.TAG, "Clearing screen flag when on ... " );
Window window = current.getWindow();
window.clearFlags( WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD );
window.clearFlags( WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED );
window.clearFlags( WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON );
}
private static BaseActivity current;
@Override
protected void onResume () {
current = this;
}
(3)在主要活動類onCreate()方法中注冊接收者:
IntentFilter filter = new IntentFilter( Intent.ACTION_SCREEN_ON );
filter.addAction( Intent.ACTION_SCREEN_OFF );
registerReceiver(new ScreenReceiver(), filter);
- 3 回答
- 0 關注
- 366 瀏覽
添加回答
舉報