How to intercept the back button on an Android Service? - android

I'm displaying a layout over all the other apps with ApplicationOverlay and the WindowManager.
When I click the back button. The OnBackPressed() override of my activity doesn't fire because of the overlay.
How can I detect and cancel the back button from a Service on android so that when I press back I can make my application change its layout?

If you have the overlay attached to the WindowsManager, you just need to add a listener to the view:
view.setFocusableInTouchMode(true);
view.setOnKeyListener(new View.OnKeyListener() {
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if(keyCode == KeyEvent.KEYCODE_BACK) {
stopSelf();
return true;
}
return false;
}
});

When you create WindowManagerLayoutParams add the below flags:
layoutParams.Flags = WindowManagerFlags.NotTouchModal;
layoutParams.Flags = WindowManagerFlags.NotFocusable;
then you could fire the OnBackPressed() method in your activity.

Related

Handling Backpressed in Fragment which does not have an activity

I have a navigation drawer & on click of certain items of navigation drawer, i am opening Fragment.These fragments does not have an activity. I have an edittext in my fragment,on click of it i am setting some view to invisible and on click of back button of keyboard i want to again show the view which were invisible on click of edittext.
My fragment does not have an activity so i cant implement onBackPressed. Please help
You can apply a listener in your fragment view doing this:
view.setOnKeyListener(new View.OnKeyListener() {
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_UP && keyCode == KeyEvent.KEYCODE_BACK){
// handle back button's click listener
return true;
}
return false;
}
});
Referenced answer

android - why alertDialog showing on homescreen?

I have a alertDialog prompt during onCreate of Application Class
(i added <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />)
and I have set setCancelable to false and it did succeessfully prevent user from pressing back button.
This is what I have achieved so far and this is also what I want.
However, when I press home button, the app did "disappear" but somehow the dialog still showing on home screen, and this is what I DON'T WANT.
What I expecting is that the back button should do nothing(this is what I have achieved).
and when user click home button, the whole app Including the Dialog should disappear. But somehow when I click home button the Dialog still appearing on the home screen...
This is because when you are creating the AlertDialog, you probably pass an ApplicationContext instead of just Context.
By following the android framework guideline, you should not make any UI changes in your Application class. Do it in your Activity of Fragment.
So do this in your Activity class:
new AlertDialog.Builder(this)
or this in your Fragment:
new AlertDialog.Builder(getContext)
Then it will disappear if your application goes into the background state.
Try this,
myDialog.setOnKeyListener(new DialogInterface.OnKeyListener() {
#Override
public boolean onKey(DialogInterface dialog1, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
return true;
}
return false;
}
});
This will do the trick:-
dialog.setOnKeyListener(new Dialog.OnKeyListener() {
#Override
public boolean onKey(DialogInterface dialogs, int keyCode,
KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
return true;
}
if (keyCode == KeyEvent.KEYCODE_HOME) {
// Do your stuff here...
return true;
}
return false;
}
});

How to make an app which will not listen to touch events

How to make an app which will not listen to touch events and back/home pressing but will listen only to the power button.
I've tried this but it wasn't successful.
#Override
public boolean onKeyDown(int keyCode, android.view.KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_POWER) {
return true;
}else if ((keyCode == KeyEvent.KEYCODE_HOME)|| (keyCode == KeyEvent.KEYCODE_BACK )){
return false;
}
return false;
}
You could wrap you class with a custom View of yours and override its OnClickListener and OnTouchListener methods. Override them with blank methods.
Also, keep your code to set how your app should work when the Power Button is clicked.
For instance, if you wrap your layout with a RelativeLayout or a general View, you could use something like
relativeLayout.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
return false;
}
});
relativeLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
return false;
}
});
To block user touch event try this solution. And if you want to disable back and home try to override this method of Activity:
#Override
public void onAttachedToWindow() {
this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
super.onAttachedToWindow();
}
three weeks ago, i want also to catch the home button, but i faied. i do some research, i find the android system don't support to modify it.you can visit this
Overriding the Home button - how do I get rid of the choice?, about homebutton.
as for the touchEvent, i advice you can try this method ,override onTouchEvent (if you dont add onTouchListener, make it retrun fasle and do nothing, at the same time ,the activity also dont handle the onTounEvent or OnClick(whole layout).

How to make device back button cause onCreate?

When you press back in ActionBar, it causes onCreate on the previous activity. How to do that with device back button aswell? In preference activity, if a certain preference is changed, I want main activity to be re-created when user presses back on bottom of their device.
You can override onBackPressed() and start your Activity yourself.
You call onNavigateUp() at any time in your Activity to get the same behavior as clicking the up button.
For example:
#Override
public void onBackPressed() {
if (myPreferenceHasChanged) {
onNavigateUp();
} else {
// Use the default behavior
super.onBackPressed();
}
}
Did you try something like this?This should solve your question
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
// your code
return true;
}
return super.onKeyDown(keyCode, event);
}

How to catch event with hardware back button on android?

How to catch event with hardware back button on android ? I need to supress user to go back and I when click on back button on phone to show message and not to go on previous activity. How to do that ?
you can do by this
override the onBackPressed() method into your Activity like this way
public void onBackPressed(){
// do something here and don't write super.onBackPressed()
}
override the onKeyDown() method
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
switch(keyCode){
case KeyEvent.KEYCODE_BACK:
// do something here
return true;
}
return super.onKeyDown(keyCode, event);
}
Override the method onBackPressed() in whatever Activity you want to create a different behaviour to the back button.
These question are equal to yours (and could have been found by a simple search):
how to disable back button in android
Disable back button in android
You can suppress user "back" action with onKeyDown() in your activity like this:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if ((keyCode == KeyEvent.KEYCODE_BACK))
{
//do actions like show message
return false;
}
return super.onKeyDown(keyCode, event);
}

Categories

Resources