I know this question has been asked many times and the answer is always "No we cant disable home button".
I have a little different query to ask.
I wrote simple code in which my activity overrides the onKeyDown() and return true for all key presses.
In theory this means whoever opens the application is stuck there and has no option to move out of the application.
When i tested this application on different devices, i made following observations :
On motorola device with OS as 2.2.2 , Home button got disabled.
On HTC device with OS as 2.3.5 , Home button got disabled.
On Sony with OS as 2.3.7 , Home button got disabled.
On Samsung with OS as 2.2.1 and 2.3.3 , Home button got disabled.
On Samsung with OS as 2.3.6 and 4.0.4, Home button remained enabled.
These observations are seems very conflicting.
Does any one have any idea , why different devices are behaving differently and what is the best way to handle such scenario.
As per my understanding till now none of the vendors have customized Android OS . Everyone is putting there UI layer on top of it but no one has touched the internals.
I know this question has been asked many times and the answer is always "No we cant disable home button".
If you want to handle the HOME button, implement a home screen.
Does any one have any idea , why different devices are behaving differently
Because they are different devices, and the vendors made changes. Also, in the case of 4.0.4, additional protections may have been added, to help prevent malware authors from hijacking the HOME button without being a home screen.
what is the best way to handle such scenario
If you want to handle the HOME button, implement a home screen.
Everyone is putting there UI layer on top of it but no one has touched the internals.
This is incorrect. Pretty much every device vendor has "touched the internals", to varying degrees. So long as they meet the compatibility requirements for the Play Store, their changes are deemed acceptable by Google.
You may want to give this a try:
#Override
public void onBackPressed() {
}
#Override
protected void onUserLeaveHint() {
super.onUserLeaveHint();
((ActivityManager) getApplicationContext().getSystemService(Context.ACTIVITY_SERVICE)).moveTaskToFront(getTaskId(), 0);
}
#Override
protected void onPause() {
super.onPause();
((ActivityManager) getApplicationContext().getSystemService(Context.ACTIVITY_SERVICE)).moveTaskToFront(getTaskId(), 0);
}
Permissions needed --
add the following to manifest
<uses-permission android:name="android.permission.REORDER_TASKS" />
As mentioned in my question itself for devices below 2.3.6 OS overriding keypress() functions work well.
Issue starts with 2.3.6 onwards. I don't know what these device vendors have done but keypress() function does not functions same on all devices.
Also from ICS onwards google has stopped using keypress() function once for all.
So how do we do it.
The way i see it, if we are trying to override home button then its not possible, but definitely we can listen to it.
In our android manifest we use <category android:name="android.intent.category.HOME" /> filter than this makes our activity as home replacement screen.
Now when you will press home button the content resolver pop up will always come up and ask which application i.e the default launcher or your application should respond to home button click. You can always choose your application there.
But this does not overrides or disables home button. whenever you will press home button same thing will be repeated again and again till you make your application default , by clicking the use as default checkbox given in the content resolver pop up.
Now once you have chosen your application as default home press will always launch your application.
Done... no. The issue which arises know is if you move out of your application the home button still launches your application. How to get rid of it once your work is done.
What we have to do is while closing or calling finish() on our activity , prior to it we should set the package setting to default by using:
paramPackageManager1.setComponentEnabledSetting(localComponentName2, PackageManager.COMPONENT_ENABLED_STATE_DEFAULT, 1);
This will de associate the home button from your activity.
Related
A previous developer for my company created an application that serves as the launcher for multiple types of android phones. These phones are given out to employees to use for business purposes so the application limits access on the phone to applications that are allowed by us.
The application works fine on all versions of Android up until Lollipop. On phones that have lollipop on them a user is able to access the settings from the notification bar (something that the app blocks on previous versions of Android) and also by long pressing on the home button, the google app is launched (that was also blocked by the application on phones that have versions less than lollipop. Package name for this application is com.google.android.googlequicksearchbox).
I have been researching this for a couple of days and I cannot figure out what has changed with lollipop to make the app stop working. I tried it on a motorolla turbo 2 and a galaxy s5 that are running lollipop and the problem exists on both.
I tried multiple solutions:
Override or disable settings menu
this link does not work for because it would basically run my application every time the user tries to access settings, which is a feature that we still give as administrative privilege to some people
Detect home button press in android
I tried this link, as well as other links to override the functionality of the long press on the Home button but I couldn't capture the press on the Home button.
Question:
Q1:
Is there a way to disable access to settings from notification bar, like hide the settings icon from the notification bar or even disable notification bar completely?
Q2:
Is there a way to intercept long presses on the Home Button or to change what the long press on the home button does?
Any help would be much appreciated. Thank you
Problem Fixed.
The application logic was to get the foreground application package using
getRunningTasks(int maxNum) which does not work after the lollipop update, so I added the code from the answer to this question: How to get recent tasks on Android "L"?
Thank you Marcin Orlowski .
I am working on an application in which user should be able to use only one application,
he should not be able to switch application using HOME button,
the app. should be closed only when user presses the close button..
I have managed to override return button, don't know how to disable HOME button.
This is not possible to disable the Home Button in android When application is in the background because it might some application always disable the home button if it's possible. So this is the reason developer can't disable the home button when app in background. But In the Activity, you can intercept the home button.
In Activity you can disable the Home key in android. It work till Android 3.x only.
#Override
public void onAttachedToWindow() {
this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
super.onAttachedToWindow();
}
TGMCians is right.
Android will not let an activity and its services run completely alone on your device.
Using Override you can capture button clicks (home, back and menu) when your activity is in the foreground. If an activity/service has full control from the background you would not be able to switch for an incoming sms, e-mails, phone calls, etc either.
Personally I think its dangerous to override the home and back button together, if the activity hangs at some point, you can not get out of the app unless you restart the phone.
Personally, I do override the back key for exit, or return to main screen events, but leave the home button alone.
This is not possible using code at all on iOS, ever.
One usage of HOME button is for emergency situation.
Any house, apartment ... they all have an emergency method, like emergency door (exit), glass breaker (axe, bat ...); it saves life.
Similarly, on Android/iOS phone, HOME button saves users' lives (well, kind of). If HOME button is dead, the phone is considered dead as well as required an instant reboot for refresh. The button was designed for such a purpose, so that developers can't messed-up with everything.
Personally, I'm kinda of being thankful for Android/iOS framework team and whoever thought of this situation on HOME button. It saves my development so many times. I suggest you should think and consider the worst case possible if HOME button is not working in your app; for example, app fails to function as normal, HOME is disabled, so how to back to HOME screen, how to switch to other apps?...
The code provided by TGMCians is not working on 4.0+.
In case, you are working on Android framework, such as building ROMs, building frameworks for manufacturers, ... grep the source code with KEY_HOME and trace inner-depth to find how it works and disable it.
actually, it is possible to block the home button using the next methods:
use of security holes, at least on old android versions. this is done on some locker apps. i think some still work even on newer android versions, but it's a risk and it might be buggy on some devices. i know that "widgetLocker" and "Picture Password Lockscreen" try out those holes. i'm not sure how well they work now with them. best solution of becoming a lockscreen is #2 .
make your app a launcher app, which will handle the home button (user must confirm it of course). an example of such an app is "MagicLocker" , and in fact any launcher app...
using a rooted device. i have no idea how to do it, but i think it's very possible.
not quite a blocking method, but you could have your app full screen and on top (using the TYPE_SYSTEM_ALERT window layout type) , so home button won't be captured, but the user won't see what's going on behind your app. the downside is that any other button won't be captured by your app, since it's not really on the foreground.
I know this question has been asked many times and the answer is always "No we cant disable home button".
I have a little different query to ask.
I wrote simple code in which my activity overrides the onKeyDown() and return true for all key presses.
In theory this means whoever opens the application is stuck there and has no option to move out of the application.
When i tested this application on different devices, i made following observations :
On motorola device with OS as 2.2.2 , Home button got disabled.
On HTC device with OS as 2.3.5 , Home button got disabled.
On Sony with OS as 2.3.7 , Home button got disabled.
On Samsung with OS as 2.2.1 and 2.3.3 , Home button got disabled.
On Samsung with OS as 2.3.6 and 4.0.4, Home button remained enabled.
These observations are seems very conflicting.
Does any one have any idea , why different devices are behaving differently and what is the best way to handle such scenario.
As per my understanding till now none of the vendors have customized Android OS . Everyone is putting there UI layer on top of it but no one has touched the internals.
I know this question has been asked many times and the answer is always "No we cant disable home button".
If you want to handle the HOME button, implement a home screen.
Does any one have any idea , why different devices are behaving differently
Because they are different devices, and the vendors made changes. Also, in the case of 4.0.4, additional protections may have been added, to help prevent malware authors from hijacking the HOME button without being a home screen.
what is the best way to handle such scenario
If you want to handle the HOME button, implement a home screen.
Everyone is putting there UI layer on top of it but no one has touched the internals.
This is incorrect. Pretty much every device vendor has "touched the internals", to varying degrees. So long as they meet the compatibility requirements for the Play Store, their changes are deemed acceptable by Google.
You may want to give this a try:
#Override
public void onBackPressed() {
}
#Override
protected void onUserLeaveHint() {
super.onUserLeaveHint();
((ActivityManager) getApplicationContext().getSystemService(Context.ACTIVITY_SERVICE)).moveTaskToFront(getTaskId(), 0);
}
#Override
protected void onPause() {
super.onPause();
((ActivityManager) getApplicationContext().getSystemService(Context.ACTIVITY_SERVICE)).moveTaskToFront(getTaskId(), 0);
}
Permissions needed --
add the following to manifest
<uses-permission android:name="android.permission.REORDER_TASKS" />
As mentioned in my question itself for devices below 2.3.6 OS overriding keypress() functions work well.
Issue starts with 2.3.6 onwards. I don't know what these device vendors have done but keypress() function does not functions same on all devices.
Also from ICS onwards google has stopped using keypress() function once for all.
So how do we do it.
The way i see it, if we are trying to override home button then its not possible, but definitely we can listen to it.
In our android manifest we use <category android:name="android.intent.category.HOME" /> filter than this makes our activity as home replacement screen.
Now when you will press home button the content resolver pop up will always come up and ask which application i.e the default launcher or your application should respond to home button click. You can always choose your application there.
But this does not overrides or disables home button. whenever you will press home button same thing will be repeated again and again till you make your application default , by clicking the use as default checkbox given in the content resolver pop up.
Now once you have chosen your application as default home press will always launch your application.
Done... no. The issue which arises know is if you move out of your application the home button still launches your application. How to get rid of it once your work is done.
What we have to do is while closing or calling finish() on our activity , prior to it we should set the package setting to default by using:
paramPackageManager1.setComponentEnabledSetting(localComponentName2, PackageManager.COMPONENT_ENABLED_STATE_DEFAULT, 1);
This will de associate the home button from your activity.
I am developing Lock screen where i want to disable Home button in ice cream sandwich and in Jelly bean , i can block it using following methods in android 2.2 , 2.3
#Override
public void onAttachedToWindow() {
// TODO Auto-generate method stub
this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
super.onAttachedToWindow();
}
also tried this
getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
Here i am also not getting event info via onPause Method or onKeyDown
But these methods done't work for me in ICS,Jelly bean if is there any method that can replace it then let me know
Post ICS i.e. Android 4+, the overriding of the HomeButton has been removed for security reasons, to enable the user exit in case the application turns out to be a malware.
Plus, it is not a really good practice to not let the user navigate away from the application. But, since you are making a lock screen application, what you can do is declare the activity as a Launcher , so that when the HomeButton is pressed it will simply restart your application and remain there itself (the users would notice nothing but a slight flicker in the screen).
EDIT #1 : Here is another workaround, more suited to your needs.
EDIT #2 : Just came across this. Haven't tested it. But looks kinda promising. Not sure if it would work, but you could give it a try.
There are few things that you can try:
You can set your activity single top, and start it over with clear to top flag when onPause() method is called, this will block the home button and opening other activities.
Listen to BOOT_COMPLETED broadcast to start your activity - this will protect you from users who will take the battery out of the device in order to reboot it.
Add Alarmmanager that will test every second if your app is alive and if it is not, then start it - This will protect you from userers that some how managed to close your app(may be with external tools).
Do this and no one be able to exit your app.
I think it is impossible to detect and/or intercept the HOME button from within an Android app. This is built into the system to prevent malicious apps that cannot be exited.
I have an app, that should be toddler safe. Meaning that, it blocks any single key touch, yet handles long pressing for exiting the app.
This is so that, the toddler will be safe from doing (too) nasty things while playing.
Up to version 2.3.4 (Gingerbread), I succeeded in achieving this.
However, now I have two problems:
On Android 3 (Honeycomb), I have the status bar notifications which can be pressed on. Also, I have the switch-windows key which can be pressed. The only thing I succeeded with it is to dim the status bar.
On Android 4.0 (Ice Cream Sandwich) (using the emulator, I still don't have a real device with this version), when calling the next code, I get an exception which cannot even be caught.
The exception:
java.lang.IllegalArgumentException: Window type can not be changed after the window is added.
The code:
#Override
public void onAttachedToWindow()
{
this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG);
super.onAttachedToWindow();
}
What can I do?
For Android version 4 (API 14 and up), it might be possible to use:
getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
However, on the emulator, it doesn't block the home button, so it's kinda useless.
I still don't know if it works fine on real devices.
Maybe it's possible to use the following workaround:
Set the app as the default home screen app.
If the home button was pressed while the application was active, capture it and do nothing.
If the home button was pressed while the application was on the background (or closed), open the previously selected default home app.
Alternatively, I could ask the user to set the default home launcher app as mine for each time it is started, and reset it again (either to the previous one, or total reset) after it is closed.
Is it possible? If so, how?
Since Android 5.0 (Lollipop) (version 5.0 which is API 21), there is a way of screen-pinning, and this can be turned on by the user or the app (link here):
There are two ways to activate screen pinning:
Manually: Users can enable screen pinning in Settings > Security >
Screen Pinning, and select the tasks they want to pin by touching the
green pin icon in the recents screen. Programmatically: To activate
screen pinning programmatically, call startLockTask() from your app.
If the requesting app is not a device owner, the user is prompted for
confirmation. A device owner app can call the setLockTaskPackages()
method to enable apps to be pinnable without the user confirmation
step.
What does it do? Read further and see:
When task locking is active, the following behavior happens:
The status bar is blank, and user notifications and status information
are hidden. The Home and Recent Apps buttons are hidden. Other apps
cannot launch new activities. The current app can start new
activities, as long as doing so does not create new tasks. When screen
pinning is invoked by a device owner, the user remains locked to your
app until the app calls stopLockTask(). If screen pinning is activity
by another app that is not a device owner or by the user directly, the
user can exit by holding both the Back and Recent buttons.
Not only that, but according to this post, you can also toggle this without user-confirmation, and exiting this special state would be under your app's logic.
It seems like the perfect thing for toddler safe app.
For me below code is hiding the home button in Android 4.0 (Ice Cream Sandwich) (version 4.4.4)
public void onCreate(Bundle savedInstanceState) {
this.getWindow().setType(WindowManager.LayoutParams.TYPE_APPLICATION);
super.onAttachedToWindow();
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}