I am developing an application that I don't want user to touch home/back button. Trust me, I have a good reason for it.
What I need to do is to disable home/back or even the keyboard using terminal.
I've look through commands in adb shell already and I cannot find any command to fix this.
I'm not sure about AVD, but in real phone this buttons are hard buttons. So, no software can remove them. All you can do is to override the behaviour for this buttons using onKeyDown() method.
Using the following piece of a code in an Activity subclass would work for intercepting key events:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
return true;
// return super.onKeyDown(keyCode, event);
}
The back key is the constant KeyEvent.KEYCODE_BACK; the home button should be similar.
Related
I am working in an launcher application ,In which i have to disable the home button event.
How can we do this in android 4.0 and above.
There is no way to intercept the home button on Android, unless you make your app the home screen. This is for security reasons, so that malicious apps cannot take over your device by overriding all the buttons that can exit. The home button is the one sure shot way to be able to leave any app.
If you want to handle the HOME button, implement a home screen.
For more information check the link below. check the answer by commonsware
Not able disable Home button on specific android devices
Here you have ;)
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_HOME)
{
return true;
}
return super.onKeyDown(keyCode, event);
}
You should use this in your activities.
You could use the method onPause() onPause
So if you press Home and return to you app you can run whatever you want.
Maybe it helps
In Android application is it possible that when I press my mobile *(star) button(not widget button) then I can perform any particular events in my application? If it's possible, then how may I achieve it?
If you mean the * key from your hardware keyboard ( on the devices that have it) you can capture it using KeyCode.
Here you can find an extensive list of all the keys you can intercept.
To do it:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
switch (keyCode) {
case KeyEvent.KEYCODE_STAR: //here you check any key you want
{
//your code here
return true;
}
}
return super.onKeyDown(keyCode, event);
}
EDIT
Answering your comment, I don't believe this is possible. The KeyDown/Up events are handled on Activities. And you won't have an Activity active. Check this out!
EDIT
Yeah, according to this guy you can't.
If the button is within your own app, then yes.
If you mean a button in any other app (I think you mean the * key on the dial pad), then no.
How can I start a progress dialog when the default Home Key button of Android is pressed? If it's possible, then how?
I've tried using the onKeyDown method, but it doesn't work.
Not really, no. You cant override the home button as you would the back or search.Take a look at this discussion:
Overriding the Home button - how do I get rid of the choice?
Duplicates this: Android - capture/suppress Home and EndCall buttons events?
The short answer: you can not handle Home button, but you can write a replacement Home Screen application.
Because the 'Home' button is such an essential feature to android (It provides the user with a quick one-touch button to get back to their home screen) that they made it so we can't run custom code to modify what it does. We do however, somewhat have control over what happens when the user presses the menu, back, or search buttons.
It's always best to conform to Android's UI design guidelines, this article on application design is a great read on how exactly we should go about things.
The prevailing wisdom says it cannot be done.
Checkout this conversation:
Home key press Event Listener
Maybe the below code would work the way you want it to. But I don't think you can trap the Home key absolutely.
Override below method in your activity.
#Override
public void onAttachedToWindow() {
super.onAttachedToWindow();
this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
}
Then after you can listen home key in your activity.
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(keyCode == KeyEvent.KEYCODE_HOME)
{}
});
Hope this may helpful to you.
I'm developing an application in Android, and I use the the following code to handle the KeyEvent for the back button:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK))
{
finish();
}
return super.onKeyDown(keyCode, event);
}
How would I go about doing this for the home button?
You can not control the behaviour of Home key. You will not get the event of Home key but you can disable it but it is highly recommended you should not do this.Before blocking the Home key refer this post.
However you can block the home key like this:
#Override
public void onAttachedToWindow() {
this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
super.onAttachedToWindow();
}
As this question suggests: Android Overriding home key this is unfortunately not possible. Perhaps there is some other way to implement your desired behavior?
Obvious answer is - handle home key press in onPause() method of your activity. This callback is called when user hits home key. Guaranted. Home key is not for you, but
for OS and user.
Read these :
Overriding the Home button - how do I get rid of the choice?
Android - How to catch that the Home button was pressed?
The home button is supposed to do one thing and one thing only and consistently. Get the user back to the the HOME screen. Even if you could override it's behavior it would be an extremely user-unfriendly thing to do. So don't do it and solve your problem differently!strong text
I'm writing an Android game that runs in fullscreen landscape mode, and has buttons placed at the bottom left and bottom right of the window. The problem is that one of these buttons is (on many phones) right next to the Menu button, so the player might accidentally press Menu instead.
If it is pressed briefly, I simply pause the game and show the in-game menu. No problem there.
But if the button is held down longer, Android opens up the soft keyboard on the bottom half of the screen. Since it gets in the way, and is completely useless in this Activity, I would like to disable it.
I tried the following approaches.
Via InputMethodManager
From: Hide soft keyboard on activity without any keyboard operations
Since I have only one view (a GLSurfaceView) I tried this in my Activity.onCreate():
InputMethodManager imm = ((InputMethodManager)getSystemService(INPUT_METHOD_SERVICE));
imm.hideSoftInputFromInputMethod(glSurfaceView.getApplicationWindowToken(), 0);
It doesn't work: the soft keyboard still comes up on Menu long-press.
Via the AndroidManifest.xml
From: How to stop the android soft keyboard from ever coming up in my entire application
I added this to my manifest:
<activity
android:windowSoftInputMode="stateAlwaysHidden"
>
Does a great deal of nothing as well.
So... is there even a way? How?
Here is, at least, a solution to my immediate problem. It shows the in-game menu, no matter how long the button was pressed.
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_MENU) {
event.startTracking();
return true;
}
return super.onKeyDown(keyCode, event);
}
#Override
public boolean onKeyLongPress(int keyCode, KeyEvent event) {
// From the docs:
// "Note that in order to receive this callback, someone in the event [chain]
// must return true from onKeyDown(int, KeyEvent) and call startTracking() on the event."
if (keyCode == KeyEvent.KEYCODE_MENU) {
// Override default handling, and don't pop up the soft keyboard.
return true;
}
return super.onKeyDown(keyCode, event);
}
#Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_MENU) {
openOptionsMenu();
return true;
}
return super.onKeyUp(keyCode, event);
}
But it feels like a hack, so I'm hoping that someone comes up with a better solution.
But if the button is held down longer,
Android opens up the soft keyboard on
the bottom half of the screen.
What phone do you have? Are you sure? I've never once seen that happen and I just tried it and it doesn't work on my phone.
Also, that sounds like a user problem. Don't try to subvert the user. If the user REALLY wants to open a keyboard in your app, you should let them and if it's useless, they'll hit back and it will go away.
A more concerning issue should be that your buttons are so close to the menu buttons.
Try using hideSoftInputFromWindow() instead. According to the documentation:
request to hide the soft input window from the context of the window that is currently accepting input.
use android:windowSoftInputMode="adjustPan" in android manifest.
I think this is best choice to prevent the view goes up.