Back Pressed Events With System Alert Window
Solution 1:
Since it's a service that hosting an overlay window, It's a bit tricky solution but it is possible.
You should handle these 2 cases separately (overriding home button press, and back button press).
1. Overriding home button press:
Create this HomeWatcher class which contains a BroadcastReceiver that will notify when home button was pressed. Register this receiver only when your window comes up.
Android: associate a method to home button of smartphone
Inside your service onCreate method use this:
HomeWatcher mHomeWatcher = newHomeWatcher(this);
mHomeWatcher.setOnHomePressedListener(newOnHomePressedListener() {
@OverridepublicvoidonHomePressed() {
yourWindow.hide() //means: windowManager.removeView(view);
}
@OverridepublicvoidonHomeLongPressed() {
}
});
mHomeWatcher.startWatch();
2. Overriding back button press:
The idea is creating an empty layout as a data member of your window class, and attach your view to it (even if its an inflated XML layout).
For example, this is gonna be your window class:
publicclassMyWindow
{
private WindowManager windowManager;
private WindowManager.LayoutParams params;
private View view;
// Add this empty layout:private MyLayout myLayout;
publicMyWindow()
{
windowManager = (WindowManager) context.getSystemService(context.WINDOW_SERVICE);
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.your_original_window_layout, null);
// Add your original view to the new empty layout:
myLayout = new MyLayout(this);
myLayout.addView(view, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
}
// And show this layout instead of your original view:publicvoidshow()
{
windowManager.addView(myLayout, params);
}
publicvoidhide()
{
windowManager.removeView(myLayout);
}
}
And now create the MyLayout class to override the back button press:
publicclassMyLayoutextendsLinearLayout
{
private MyWindow myWindow;
publicMyLayout(MyWindow myWindow)
{
super(myWindow.context);
this.myWindow = myWindow;
}
@Override public boolean dispatchKeyEvent(KeyEvent event)
{
if (event.getKeyCode() == KeyEvent.KEYCODE_BACK)
{
if (event.getAction() == KeyEvent.ACTION_DOWN && event.getRepeatCount() == 0)
{
getKeyDispatcherState().startTracking(event, this);
returntrue;
}
elseif (event.getAction() == KeyEvent.ACTION_UP)
{
getKeyDispatcherState().handleUpEvent(event);
if (event.isTracking() && !event.isCanceled())
{
// dismiss your window:
myWindow.hide();
returntrue;
}
}
}
return super.dispatchKeyEvent(event);
}
}
I know it's a bit complicated as I said since it's a system alert window hosted by a service, BUT it's working. I have the same issue as well and it has been solved exactly like that. Good luck.
Solution 2:
use below method to handle back button pressed.
publicvoidonBackPressed()
{
super.onBackPressed();
}
Solution 3:
You need to overwrite the onBackPressed
method.
@OverridepublicvoidonBackPressed() {
super.onBackPressed(); // remove this if u want to handle this event
}
Solution 4:
Use the code below
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
exitByBackKey();
//moveTaskToBack(false);returntrue;
}
return super.onKeyDown(keyCode, event);
}
protectedvoidexitByBackKey() {
AlertDialog alertbox = new AlertDialog.Builder(this)
.setMessage("Do you want to exit application?")
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
// do something when the button is clickedpublicvoidonClick(DialogInterface arg0, int arg1) {
finish();
//close();
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
// do something when the button is clickedpublicvoidonClick(DialogInterface arg0, int arg1) {
}
})
.show();
}
Solution 5:
@OverridepublicvoidonBackPressed()
{
super.onBackPressed();
}
Declare this on your activity. super.OnBackPressed automatically calls back method in android. it will surely cancel your dialog.
in addition, your dialog must look like this.
AlertDialog.Builderbuilder=newAlertDialog.Builder(this);
builder1.setMessage("TEST DIALOG.\n");
builder1.setPositiveButton("Ok",
newDialogInterface.OnClickListener() {
@OverridepublicvoidonClick(DialogInterface dialog, int id) {
Toast.makeText(MainActivity.this, "This Is test Dialog", Toast.LENGTH_SHORT).show();
}
});
AlertDialogalert11= builder1.create();
alert11.show();
or you can set Negative button..
Hope this helps!
Post a Comment for "Back Pressed Events With System Alert Window"