我想为Volume up键添加一个按钮单击事件处理程序,并在用户连续三次单击该音量时启动一个活动。以下代码仅在我的应用程序的主要活动处于活动状态时有效。应用程序未处于活动状态时如何捕获onKeyDown?
@Override
public boolean onKeyDown (int KeyCode, KeyEvent event){
//listen if VOLUME UP BUTTON IS PRESSED
if(KeyCode == KeyEvent.KEYCODE_VOLUME_UP){
//increase the value of vBtnClicked by 1 each time it is clicked.
vBtnClicked++;
//if btn is clicked 3 times excecute code and reset the value of
//vBtnClicked
if(vBtnClicked == 3){
//reset value of variable for later use.
vBtnClicked = 0;
//MY CODE WILL BE ADDED HERE
Toast.makeText(getBaseContext(), "dummy_text" ,
Toast.LENGTH_SHORT).show();
}Log.d("log_dave","backclick value is:" + vBtnClicked);
return true;
}
//this following code is if you want the code to execute only after the
//button is pressed 3 times in a row.
//if another button is pressed in between vBtnCliced is reset
else {
vBtnClicked=0;
}
return super.onKeyDown(KeyCode, event);
}