block Home Button in Ice cream sandwich and jelly bean - android

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.

Related

how to prevent use of HOME button in android and ios?

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.

how can I disable home button in android.I am writing an screen lock app but cannot disable the home button [duplicate]

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.

Adobe Flash Builder Flex Mobile Android: Avoid that user can close the app

I am developing an app for android. It is not always clear to me when the app will be terminated by the OS. Sometimes it happens when you press the back button and sometimes when you press the home button. What is the logic behind this?
My app must be keep activated during a long period (when you run it the app must stay resident). Also another question is if it is possible to popup when an event comes in and activate the screen and bring it to the front.
Does somebody made this already? Or is there more information about this (how to do it). Search the internet but doesn't find some useful things.
Pressing the Back button on the main activity will finish and exit the app.
Pressing the Home button will pause and leave the app in the background, it will return to the previous state when it's restored.
Override the onBackPressed() method on the main activity if you don't want it to quit when the back button is pressed.
If you want an event to occur when the screen is shown, override the onResume() method and do the checks and event required in there.
Being that your answer is with Flex the issue is that you need to listen to the stage's keyDown event.
stage.addEventListener(KeyboardEvent.Key_DOWN, yourHandler);
Then you need to response to the back button and inhibit this - if your intent is to stop the back button.
private function _onStage_keyDownHandler(event:KeyboardEvent)
{
if(event.keyCode == Keyboard.BACK)
{
event.stopImmediatePropogation();
event.preventDefault();
//your code here
}
}
And one more moment - android can close your app when it thinks that it need more memory for more recent apps.
And you may need to use native extensions or even develop your app in java if you want to implement reliable resident behavior.
Update: java service+ air ui example:
http://www.jamesward.com/2011/05/11/extending-air-for-android/
end of update
And you must know that air apps eat at least 30mb of memory(if they are empty and do nothing), normally they will start from 50-70mb. I guess that no user will wish to allow another 70mb of his memory-hungry device to be eaten by something not critical.
And for automatic maximizing of your window you may need root access.

Not able disable Home button on specific android devices

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.

Overwriting phone standard buttons

Since we´re going to use phones for public use I want an app to be launched when the phone booted.
Than by filling in a code the correct activity should be started without the user being able to get into the ´phone´ software (OS).
Is it possible to overwrite all the phonebuttons, so the user won´t go to the homescreen eg, if yes, which methods are called?
Thanks
You can override the back button default behaviour by adding these lines in your activities:
#Override
public void onBackPressed() {
// do nothing
}
(The default behaviour is to call finish() on the current activity: if you do the above, you remove this finish() call).
Overriding the home button is a little bit more complicated: look at these questions:
Can I override the 'Home' button in my application?
Android Overriding home key
The Android API documentation can also show you many things.
http://developer.android.com/reference/android/app/Activity.html
You will want to make sure you cover for every part of the activity life-cycle as well as override any methods that will cause behavior you don't want (ie. button presses).
It would also be smart to look through the intents thrown by the Android OS.
http://developer.android.com/reference/android/content/Intent.html
This way you can catch any unexpected events. Knowing the platform you are working with can also help. Some Manufacturers have phones that provide specific API's (can be download from manufacturers websites) as well as hardware buttons. You should also account for this if you are trying to make a locked system.

Categories

Resources