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 关注
- 346 浏览
添加回答
举报