@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&&event.isLongPress()==false) {
//navigate up
}
if(event.isLongPress()==true)
//start new activity B
startActivity(new Intent("com.example.fittle.FISH"));
return true;
case KeyEvent.KEYCODE_VOLUME_DOWN:
if (action == KeyEvent.ACTION_DOWN) {
//navigate down
}
return true;
default:
return super.dispatchKeyEvent(event);
}
}
在活动B中
@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&&event.isLongPress()==false) {
//navigate up
}
if(event.isLongPress()==true)
//play sound
return true;
case KeyEvent.KEYCODE_VOLUME_DOWN:
if (action == KeyEvent.ACTION_DOWN) {
//navigate down
}
return true;
if(event.isLongPress()==true)
//go to activity A
default:
return super.dispatchKeyEvent(event);
}
}
我有两个活动A和B. 在活动A中>我使用音量增大/减小键浏览各种按钮,然后长按按钮开始活动B.
当活动B开始时,音量会自动上升。
我知道它正在发生,因为在新活动B中检测到长按,然后被视为短按以提高音量。
有没有办法在新活动B中杀死之前长按的检测
答案 0 :(得分:0)
在活动B中。
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// my logic when key is pressed in Activity b
}