Android: Unregister camera button

蹲街弑〆低调 提交于 2019-12-04 08:45:50

In your example you need to return true to let it know you "consumed" the event. Like this:

videoPreview.setOnKeyListener(new OnKeyListener(){
    public boolean onKey(View v, int keyCode, KeyEvent event){
        if(event.getAction() == KeyEvent.ACTION_DOWN) {
            switch(keyCode) {
                case KeyEvent.KEYCODE_CAMERA:
                    //videoPreview.onCapture(settings);
                    onCaptureButton();
                    /* ... */
                    return true;
            }
        }
        return false;
    }
});

It will also only work if the videoPreview (or a child element) has focus. So you could either set it to have focus by default:

@Override
public void onResume() {
    /* ... */
    videoPreview.requestFocus();
    super.onResume();
}

or (prefered) put the listener on the top-level element (eg. a LinearLayout, RelativeLayout, etc).

As soon as camera button is pressed a broadcast message is sent to all the applications listening to it. You need to make use of Broadcast receivers and abortBroadcast() function. You can find more details regarding this in the below link

http://suhassiddarth.blogspot.com/

a simple way to disable the camera button (or react on a click) is to add the following to your activity:

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_CAMERA) {
        return true; // do nothing on camera button
    }
    return super.onKeyDown(keyCode, event);
}

You forgot to return true in your case KeyEvent.KEYCODE_CAMERA branch. If you return true, that signals to Android that you have consumed the key event, and the Camera application should not be launched. By returning false all the time, all the key events are passed upwards to the default handlers.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!