I am working on an app and i want it to be such that when the user presses home then nothing should happen. Is it possible to accomplish that? If that is not possible then is it possible that some other action is performed when hone is pressed. the whole idea is user should not leave the app directly from any screen.
update:can someone tell me how to define my app as launcher?
No, you cannot. Whenever Home button is pressed, the framework will always throw you back on android's home screen. Sorry, you are out of luck. :)
you cant make it work as you want, but you can disable it
#Override
public void onAttachedToWindow() {
this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
super.onAttachedToWindow();
}
and you can tell user that home button is disabled:
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_HOME) {
Context context = getApplicationContext();
Toast toast = Toast.makeText(context,"Home button is disabled",1);
toast.setGravity(Gravity.CENTER_VERTICAL,0,0);
toast.show();
return true;
}
return super.onKeyDown(keyCode, event);
}
I am working on an app and i want it to be such that when the user presses home then nothing should happen. Is it possible to accomplish that?
Make your app be the home screen. The user can still remove your app by rebooting in safe mode.
Related
I am developing an application in API level 2.2. In my application whenever I press Home button it forces my app to exit, and when I relaunch that app by clicking on its icon, it starts from where it exited. I have tried to change/ override functionality of Home button but it does not works.
I have tried as
`
public boolean onKeyDown (int keyCode, KeyEvent event){
if (keyCode == KeyEvent.KEYCODE_BACK)
{
startActivity(new Intent(CategoryFirstActivity.this,LastActivity.class));
finish();
}
else if (keyCode == KeyEvent.KEYCODE_HOME)
{
startActivity(new Intent(CategoryFirstActivity.this,LastActivity.class));
finish();
}
return false;
}`
But it works for Back button only and not for Home button.
I have also tried to get keyCode of Home key but I didn't get it.
I need solution to change Home Button functionality.
Simply put: better you do not change the behavior of your home button...
Instead of trying to reprogram your Home button, you could try using some of the Activity callbacks that Android provides. You can take a look at the Activity lifecycle here:
http://developer.android.com/reference/android/app/Activity.html
For example, you could try overriding the onPause callback in an effort to achieve the effect you're going for.
#Override
public void onPause() { /* do stuff */ }
In your case, do stuff might be returning the application to its initial state or forcing it to exit. I've never attempted a force quit myself, but there are some answers on this StackOverflow post:
How to exit from the application and show the home screen?
Best of luck!
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 my game, if a user hits the back button I pop up a dialog asking if they really want to quit. However, I can't do the same with the home button because there's no way to override it.
If the user knows the task manager trick they can hold down home and switch back to the app and not lose their place.
If they don't know the trick they'll just select the icon again which will start the application over from the main menu.
Is there any way to override this behavior so that instead of starting at the main menu it would go back to where it was if the app is currently running?
I know that I could save the state of everything when the app pauses and then programmatically reload everything and send them to where they were. I'd like to avoid having to do all that work if possible.
However, I can't do the same with the home button because there's no way to override it.
You can,actually,override the home button.
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_HOME)) {
//do your stuff
return true;
}
return super.onKeyDown(keyCode, event);
}
Is there any way to override this behavior so that instead of starting at the main menu it would go back to where it was if the app is currently running?
About this, your best bet is to override the onPause() method in every Activity. But there is no guarantee about it, because, the OS might choose to kill your application when the user navigates away from it(for resource requirements,maybe). So in such a case, your application will start off from the main menu, and you can't help it.
How to add a listener to home button? i.e I want to add some functionality when home button is pressed. How do I do it?
I did the following, but it is not working for home button. It's working only for back button.
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if (keyCode == KeyEvent.KEYCODE_BACK)
{
// some functionality
}
if (keyCode == KeyEvent.KEYCODE_HOME)
{
// some functionality
}
// // TODO Auto-generated method stub
return super.onKeyDown(keyCode, event);
}
don't do that. its not correct to do that because the only use of home button is to exit from the application at any point. you can use isFinishing() if u want to execute any code while leaving an activity. have a look into this.
You must override onAttachedToWindow() first.
#Override
public void onAttachedToWindow() {
this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
super.onAttachedToWindow();
}
In addition, you need to add the permission:
<uses-permission android:name="android.permission.DISABLE_KEYGUARD "></uses-permission>
You can't do that. The Android policy forbids that a App can overwrite or listen to the Homebutton.
Maybe you can accomplish what you want with overwriting onDetachFromWindow.
You can't but you could use onPause(). The application will be paused when the home button is clicked.
How to add a listener to home button?
You don't. The HOME button always brings up the user's chosen home screen. You can elect to make your app be a home screen, by supporting CATEGORY_HOME in an activity, but then it had better really work as a home screen.
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