Basically, I'm trying to recreate the functionality of the MediaController in the native Music app (that I see on 2.2), where the back button immediately backs out of screen, instead of hiding the MediaController. There doesn't seem to be any good way to set a keylistener or override a method to intercept these keyevents though.
Any ideas?
You can try something like
mMediaController = new MediaController(this) {
#Override
public boolean dispatchKeyEvent(KeyEvent event) {
// TODO Auto-generated method stub
if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
finish();
}
return super.dispatchKeyEvent(event);
}
}
or the dispatchKeyEvent like
#Override
public boolean dispatchKeyEvent(KeyEvent event) {
ContentActivity.this.dispatchKeyEvent(event);
}
Related
I have created a window and i am showing it on screen through Broadcast receiver.But the problem is that it appears on the screen and i want to dismiss it once the back button is pressed.I am unable to get the event of button press on this view.My code for back press looks as follows-
view.setOnKeyListener(new View.OnKeyListener() {
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (event.getKeyCode() == KEYCODE_BACK) {
Log.d("LOG", "back button is pressed");
}
return true;
}
});
But Nothing is happening.I tried to do the same through DISPATCHKEY but it was also of no use.Please help me what i am not figuring out.Wouldn't this work on the view.?
Maintain global reference for Window and override onBackPressed()
Try this:
#Override
public void onBackPressed() {
if (view != null && view.isShowing()) {
view.dismiss();
} else {
super.onBackPressed();
}
}
I want to know which key has been pressed in android keyboard. For example, if a is pressed {a}, I want to show value {a} in screen with toast ?
i want using broadcastreciever or background service
Try using dispatchKeyEvent(KeyEvent event) in your Activity:
#Override
public boolean dispatchKeyEvent(KeyEvent event) {
Log.i("key pressed", String.valueOf(event.getKeyCode()));
return super.dispatchKeyEvent(event);
}
public class MainActivity extends Activity {
KeyEvent event;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dispatchKeyEvent(event);
}
#Override
public boolean dispatchKeyEvent(KeyEvent event) {
Log.i("key pressed", String.valueOf(event.getKeyCode()));
return super.dispatchKeyEvent(event);
}
}
When I run this code, the keyboard app crashes. A dialog with message posted below is displayed.
Unfortunately,keyboard has stopped
I have an application. I tried to disable home button. Many people said that it doesn't possible to do in android 4.0 and above. So i decided to reload the same activity when press home button. I followed the below code.
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
super.onKeyDown(keyCode, event);
if (keyCode == KeyEvent.KEYCODE_HOME) {
System.out.println("==============================");
Intent i = new Intent(getBaseContext().getPackageManager()
.getLaunchIntentForPackage(
getBaseContext().getPackageName()));
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(i);
return true;
}
return false;
}
I can't get any response when press home button. Can you tell me what's my wrong?
This key cannot be intercepted, so KEYCODE_HOME won't be send to you.
Its impossible to override the home button.
public static final int KEYCODE_HOME
Added in API level 1
Key code constant: Home key. This key is handled by the framework and is never delivered to applications.
Source:
http://developer.android.com/reference/android/view/KeyEvent.html
This is a refresh button method, but it works well in my application. in finish() you kill the instances
refresh = (Button)findViewById(R.id.refresh);
refresh.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
onRestart();
}
});
#Override
protected void onRestart() {
// TODO Auto-generated method stub
super.onRestart();
Intent i = new Intent(lala.this, lala.class); //your class
startActivity(i);
finish();
}
How can I specifically override just the back button while within a dialog to finish the entire activity and not just the dialog.
Using setOnCancelListener and setOnDismissListener do not work because there are other times that I simply close the dialog without closing the whole activity behind it.
Edit
Thanks Shubayu that may work!
I was also able to access just the back button in a dialog through this function.
dialog.setOnKeyListener(new DialogInterface.OnKeyListener() {
#Override
public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
if (event.getKeyCode() == KeyEvent.KEYCODE_BACK){
finish();
}
return false;
}
});
Override
public void onBackPressed ()
of the activity and put in the way you want the behavior in it. Also set a boolean from your dialog which you use inside onBackPressed() of the Activity. if the boolean is true, run the disabling part of the onBackPressed() code else don't.
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK){
// your logic goes here
return true;
}
return super.onKeyDown(keyCode, event);
}
use the above code::
You can use : dialog.setOnCancelListener(.....)
first set dialog.setCancelable(true);
than you can place below code :
dialog.setOnCancelListener(new DialogInterface.OnCancelListener()
{
#Override
public void onCancel(DialogInterface dialog)
{
// add code backpress
}
});
I want to disable the global search key when display the alertDialog. So I extents the class and rewrite the method dispatchKeyEvent to catch the key message. But when I press search key in the AlertDialog window, it cannot catch the key event. Why?
Here is the code in the new dispatchKeyEvent method:
#Override
public boolean dispatchKeyEvent(KeyEvent event) {
if(event.getKeyCode() == KeyEvent.KEYCODE_SEARCH)
return true;
return super.dispatchKeyEvent(event);
}
I found the way to solve this. Not to extend the AlertDialog but extend the Builder instead. And in the constructor write the code below:
setOnKeyListener(new OnKeyListener() {
#Override
public boolean onKey(DialogInterface dialog, int keyCode,
KeyEvent event) {
if(event.getKeyCode() == KeyEvent.KEYCODE_SEARCH)
return true;
return false;
}
});
And this can catch the global search key and drop it.