我需要使用音量按钮来控制应用程序中的变量参数。
我按Activity.onKeyDown
时会在按下按钮时收到通知,但媒体音量也会增加
当我按下音量键时,Android正在执行以下操作:
有没有办法避免增加系统音量并仅为我的应用程序使用音量键?
答案 0 :(得分:102)
您需要捕获所有操作:
@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_DOWN) {
//TODO
}
return true;
case KeyEvent.KEYCODE_VOLUME_DOWN:
if (action == KeyEvent.ACTION_DOWN) {
//TODO
}
return true;
default:
return super.dispatchKeyEvent(event);
}
}
答案 1 :(得分:11)
下一个会更好,因为KeyEvent.KEYCODE_VOLUME_UP
的长按不能在上一个答案中正常工作。
@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_DOWN) {
//TODO
}
return true;
case KeyEvent.KEYCODE_VOLUME_DOWN:
if (action == KeyEvent.ACTION_DOWN) {
//TODO
}
return true;
default:
return super.dispatchKeyEvent(event);
}
}
答案 2 :(得分:2)
只是为了完成之前的答案,如果你想要长按支持,你应该检查密钥关闭的时间:
event.getEventTime() - event.getDownTime() > ViewConfiguration.getLongPressTimeout()
最后代码如下所示:
@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) {
if (event.getEventTime() - event.getDownTime() > ViewConfiguration.getLongPressTimeout()) {
//TODO long click action
} else {
//TODO click action
}
}
return true;
case KeyEvent.KEYCODE_VOLUME_DOWN:
if (action == KeyEvent.ACTION_UP) {
if (event.getEventTime() - event.getDownTime() > ViewConfiguration.getLongPressTimeout()) {
//TODO long click action
} else {
//TODO click action
}
}
return true;
default:
return super.dispatchKeyEvent(event);
}
}
答案 3 :(得分:1)
我之前用过这段代码来听取音量按钮。
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_VOLUME_DOWN)){
//Do something
}
return true;
}
答案 4 :(得分:1)
如果您希望在绑定Activity的片段中实现此功能,可以使用as:
注意:fragment_words只是一个仲裁使用的布局名称
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_words, container, false);
rootView.requestFocus();
rootView.setOnKeyListener(new View.OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if( keyCode == KeyEvent.KEYCOD_VOLUME_DOWN ) {
//TODO SOMETHING
return true;
} else {
return false;
}
}
});}
答案 5 :(得分:0)
具有双击操作的音量
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) {
i++;
Handler handler= new Handler();
Runnable runnable=new Runnable() {
@Override
public void run() {
i=0;
}
};
if(i==1){
Toast.makeText(getApplicationContext(),"single click",Toast.LENGTH_SHORT).show();
handler.postDelayed(runnable,400);
}
else if(i==2){
Vibrator vb = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
vb.vibrate(100);
}
return true;
}
if (keyCode == KeyEvent.KEYCODE_VOLUME_UP) {
return true;
} else {
return super.onKeyDown(keyCode, event);
}
}
答案 6 :(得分:0)
我在AB快门(蓝牙)始终启动音量时遇到问题,而我想将蓝牙键盘事件用于其他功能。 但是我仍然希望从设备的音量调高按钮调高音量。 我发现的解决方案是处理短暂事件,并且仅在按住设备按钮一秒钟以上时才允许更改音量。 看到代码:
boolean bVolumeUpKeyDown = false;
long StartDownMS = 0;
@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) {
// my handling of keyevent
return true;
}
else if (action == KeyEvent.ACTION_DOWN) {
if(bVolumeUpKeyDown == false) {
bVolumeUpKeyDown = true;
StartDownMS = System.currentTimeMillis();
}
else if(System.currentTimeMillis() - StartDownMS > 1000)
return super.dispatchKeyEvent(event); // normal volume up handling
}
return true;
case KeyEvent.KEYCODE_ENTER:
if (action == KeyEvent.ACTION_UP) {
//my handling of second ab shutter button
}
return true;
default:
return super.dispatchKeyEvent(event);
}
}