I would like for the VOLUME_UP and VOLUME_DOWN buttons to open and close an AlertDialog window. Below is what I have so far,
private void paused() {
View v = this.getLayoutInflater().inflate(R.layout.paused, null);
AlertDialog.Builder openPausedAlert = new AlertDialog.Builder(this);
AlertDialog alert = openPausedAlert.create();
alert.setView(v);
//alert.show(); This works...
DialogInterface.OnKeyListener keylistener = new DialogInterface.OnKeyListener() {
@Override
public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent KEvent) {
int keyaction = KEvent.getAction();
int keycode = KEvent.getKeyCode();
if (keyaction == KeyEvent.ACTION_DOWN) {
if (keycode == KeyEvent.KEYCODE_VOLUME_UP) {
alert.dismiss();
return true;
}else if (keycode == KeyEvent.KEYCODE_VOLUME_DOWN){
alert.show(); //this does not work
return true;
}
}
return false;
}
};
alert.setOnKeyListener(keylistener);
alert.show() works fine when it is outside of the DialogInterface method, but is unreachable when it is in the DialogInterface conditionals. However, alert.dismiss() is reachable and actuates fine when alert.show() is outside of the DialogInterface. What am I missing here that will make this work? Thanks in advance...
This works.
private void paused() {
View v = MainActivity.this.getLayoutInflater().inflate(R.layout.paused, null);
AlertDialog.Builder openPausedAlert = new AlertDialog.Builder(MainActivity.this);
AlertDialog alert = openPausedAlert.create();
alert.setMessage("MUTED");
alert.setView(v);
alert.show();
bPaused = true;
DialogInterface.OnKeyListener keylistener = new DialogInterface.OnKeyListener() {
@Override
public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent KEvent) {
int keyaction = KEvent.getAction();
int keycode = KEvent.getKeyCode();
if (keyaction == KeyEvent.ACTION_DOWN) {
if ((keycode == KeyEvent.KEYCODE_VOLUME_UP) || (keycode == KeyEvent.KEYCODE_VOLUME_DOWN)) {
if (bPaused == true) {
alert.dismiss();
bPaused = false;
return true;
}
}
}
return false;
}
};
alert.setOnKeyListener(keylistener);
}//End paused()