单击主页按钮关闭屏幕

时间:2014-01-15 10:41:45

标签: android screen

我想在按下HOME_BUTTON时关闭屏幕。

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {

    if (keyCode == KeyEvent.KEYCODE_HOME) {
        System.out.println("home button");
        finish();
                WindowManager.LayoutParams params = getWindow().getAttributes();
                params.flags |= LayoutParams.FLAG_KEEP_SCREEN_ON;
                params.screenBrightness = 0;
                getWindow().setAttributes(params);

        return true;
    }

    return super.onKeyDown(keyCode, event);
}

任何人都可以帮助我 谢谢

2 个答案:

答案 0 :(得分:1)

最终我得到了解决方案

@Override
protected void onResume() {
    super.onResume();
    if (PreferenceHelper.getScreenLock(Recognition.this)) {
        wakeLock.acquire();
    }
    WindowManager.LayoutParams params = getWindow().getAttributes();
    params.screenBrightness = 1;
    getWindow().setAttributes(params);

}

@Override
protected void onPause() {
    super.onPause();
    if (PreferenceHelper.getScreenLock(Recognition.this)) {
        wakeLock.release();
    }
    WindowManager.LayoutParams params = getWindow().getAttributes();
    params.screenBrightness = 0;
    getWindow().setAttributes(params);

}

答案 1 :(得分:0)

public boolean onKeyDown(int keyCode,KeyEvent event){

if (keyCode == KeyEvent.KEYCODE_HOME) {
    System.out.println("home button");
    DevicePolicyManager mDPM = (DevicePolicyManager)getSystemService(Context.DEVICE_POLICY_SERVICE);
    mDPM.lockNow();
    return true;
}
return super.onKeyDown(keyCode, event);

}

这会在按下主页按钮时锁定屏幕。