Quando ero in procinto di pubblicare la mia rispondo ho scoperto che qualcuno ha già una soluzione ...
Ma qui è mio, semplice e funziona come un fascino. Un solo flag;)
Questo codice rileva le operazioni di compressione e pressione prolungata, in caso di pressione prolungata non viene eseguito alcun intervento di stampa!
Nota: se si desidera che il volume normale e comportamento giù cambiano il ritorno vero nel metodo OnKeyPress alla chiamata super come questo:
event.startTracking();
if(event.getRepeatCount() == 0){
shortPress = true;
}
//return true;
return super.onKeyDown(keyCode, event);
codice senza il super chiamata:
private boolean shortPress = false;
@Override
public boolean onKeyLongPress(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) {
shortPress = false;
Toast.makeText(this, "longPress", Toast.LENGTH_LONG).show();
return true;
}
//Just return false because the super call does always the same (returning false)
return false;
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) {
if(event.getAction() == KeyEvent.ACTION_DOWN){
event.startTracking();
if(event.getRepeatCount() == 0){
shortPress = true;
}
return true;
}
}
return super.onKeyDown(keyCode, event);
}
@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) {
if(shortPress){
Toast.makeText(this, "shortPress", Toast.LENGTH_LONG).show();
} else {
//Don't handle longpress here, because the user will have to get his finger back up first
}
shortPress = false;
return true;
}
return super.onKeyUp(keyCode, event);
}
codice qui è con il tasto volume su aggiunto, basta scegliere il vostro lato;)
private boolean shortPress = false;
@Override
public boolean onKeyLongPress(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) {
shortPress = false;
Toast.makeText(this, "longPress Volume Down", Toast.LENGTH_LONG).show();
return true;
} else if(keyCode == KeyEvent.KEYCODE_VOLUME_UP){
shortPress = false;
Toast.makeText(this, "longPress Volume Up", Toast.LENGTH_LONG).show();
return true;
}
//Just return false because the super call does always the same (returning false)
return false;
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN || keyCode == KeyEvent.KEYCODE_VOLUME_UP) {
if(event.getAction() == KeyEvent.ACTION_DOWN){
event.startTracking();
if(event.getRepeatCount() == 0){
shortPress = true;
}
return true;
}
}
return super.onKeyDown(keyCode, event);
}
@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) {
if(shortPress){
Toast.makeText(this, "shortPress Volume Down", Toast.LENGTH_LONG).show();
} else {
//Don't handle longpress here, because the user will have to get his finger back up first
}
shortPress = false;
return true;
} else if(keyCode == KeyEvent.KEYCODE_VOLUME_UP){
if(shortPress){
Toast.makeText(this, "shortPress Volume up", Toast.LENGTH_LONG).show();
} else {
//Don't handle longpress here, because the user will have to get his finger back up first
}
shortPress = false;
return true;
}
return super.onKeyUp(keyCode, event);
}
quindi provare con key up (KeyEvent.KEYCODE_VOLUME_UP) anziché keydown; se premere a lungo chiamerà prima della chiave, se premuto a lungo chiamato interromperà l'azione in keyup usando la flag – Sandy09
Questa è la chiave di aumento del volume, non un indicatore che indica che la chiave è premuto o qualcosa del genere. –
Sto provando le cose al momento –