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.
Related
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 have 2 different applications A and B and I want to create a special animation from B to A, that is when A is opened after B was visible. This means that I need to somehow know the previous app after which my app was opened. I can have different scenarios of going from B to A - using Recent Apps (multitasking) button, using Back button, using Home button (application A is a custom home screen). Are there any ideas how to do this? Some functions in ActivityManager might help, but they have comments in documentation saying not to use them for implementing logic and control flow.
Not sure if this will work across different applications, but how about getCallingActivity() or getCallingPackage()?
If that doesn't work, could you pass along some 'extra' data in the bundle when you launch the intent that indicates the launching application?
I managed to figure out how to implement this, its working for me.
I used this answer, but replaced the ActivityManager.getRunningTasks() with ActivityManager.getRecentTasks() supplying RECENT_WITH_EXCLUDED | RECENT_IGNORE_UNAVAILABLE, and took the component name from baseIntent member of the result. The info at index 1 is the one that was running before the current app was opened, irrelevant of the fact how you get back to your app - back button, home button, recents button or opened from another app.
NOTE: This works when your app is started, like in onResume() but doesn't work when your app is closed (when called in onPause()) because the new task is not yet loaded into the activity manager. So if you need to also know to which app are you going then it might be a bit more complex.
NOTE2: Although the documentation tells not to use the API above for any kind of logic and control flow, I saw that the multitasking/recent app's code is doing exactly the same, so in my opinion it should not be as risky as they write it docs.
NOTE3: Don't forget to follow all the steps in the answer I mentioned above, like adding needed permissions, otherwise you will get exceptions. Being part of the system in my case makes it much easier for me.
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 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.
Looked at android documentation and it appears that we don't have the ability to know when an app shuts down. Whether it was explicitly by the user or automatically by the operating system.
Below is the onTerminate() documentation which is only available in the emulated scenario.
public void onTerminate()
Since: API Level 1
This method is for use in emulated process
environments. It will never be called on a production Android device,
where processes are removed by simply killing them; no user code
(including this callback) is executed when doing so.
Does anyone have any other approaches to report back when the user closes the application?
We need to know from a pilot/usability standpoint if we need to incorporate additional functionality into our future production app.
Not sure whether this is going to help you...
In my app, I'm using Activity.onDestroy() to do the cleanup that I need. I have a couple of activities - and have onDestroy() in each of them.
This is the closest I got to doing what I needed - and it actually works quite well.
Looked at android documentation and it appears that we don't have the ability to know when an app shuts down. Whether it was explicitly by the user or automatically by the operating system.
Users do not close applications on Android.
Does anyone have any other approaches to report back when the user closes the application.
Users do not close applications on Android.
Android, in this respect, behaves much like a Web browser. Users do not close Web applications in Web browsers. They might close the browser. They might close a tab. They might press the home button and navigate to a different site/app. They might choose a bookmark and navigate to a different site/app. They might drag a document into the browser and view it. They might double-click on a desktop icon and view it. And they might click some "logout" link in the currently-viewed Web site/app. Any of these cause the user to leave whatever Web site/app they are in, but none of them would be construed as "closing" the Web site/app in the same manner that clicking the close button on a desktop OS window might be construed as "closing" the desktop app.
As #Aleks G notes, there are various lifecycle methods you can override to find out what the user is doing with respect to an activity. onStop() indicates something else took over foreground input and your current activity is no longer visible. onUserLeaveHint() indicates that the user pressed HOME. And so on. But those are at the activity level, not the application level.
Make a BaseActivity extending Activity in your Application and extends this BaseActivity instead of Activity.
In this BaseActivity override the onDestroy() method.
Like
#Override
protected void onDestroy() {
super.onDestroy();
//Write the code that you want to do if the application terminates
}