0

I want to change the brightness of screen with volume keys. I used this code, but it doesn't work:

@Override
    public boolean onKeyDown(int keyCode,KeyEvent keyEvent){
        WindowManager.LayoutParams layout = getWindow().getAttributes();

        if(keyEvent.getAction() == KeyEvent.ACTION_DOWN){
            switch (keyCode){
                case KeyEvent.KEYCODE_VOLUME_DOWN:
                    layout.screenBrightness -= 0.1;
                    getWindow().setAttributes(layout);
                    break;
                case KeyEvent.KEYCODE_VOLUME_UP:
                    layout.screenBrightness += 0.1;
                    getWindow().setAttributes(layout);
                    break;
            }

        }
        return super.onKeyDown(keyCode , keyEvent);
    }

When I press volume key down/up ringer volume is changed?

4

2 に答える 2

1
@Override
public boolean dispatchKeyEvent(KeyEvent event) {
int action = event.getAction();
int keyCode = event.getKeyCode();
    switch (keyCode) {
    case KeyEvent.KEYCODE_VOLUME_UP:
        if (action == KeyEvent.ACTION_UP) {

                brightness+=10;
            if (brightness > 100) brightness = 100;
        }
        return true;
    case KeyEvent.KEYCODE_VOLUME_DOWN:
        if (action == KeyEvent.ACTION_DOWN) {
            brightness-=10;
            if (brightness < 0) brightness=0;
        }
        return true;
    default:
        return super.dispatchKeyEvent(event);
    }
}

関数

private void setBrightness() {
  Window w = getWindow();
  WindowManager.LayoutParams lp = w.getAttributes();
  lp.screenBrightness = (float)brightness/100;
  if (lp.screenBrightness<.01f) lp.screenBrightness=.01f;
  w.setAttributes(lp);
  updateTextLabel();
}
于 2013-07-08T08:42:19.013 に答える