I am creating an app with Accessibility Service and I want to do some task when the back button or home button is pressed.For that purpose I added the following attribute in xml file:-
I added the flagRequestFilterKeyEvents flag.
Also set android:canRequestFilterKeyEvents="true".
The service is responding in other devices except the nexus device.
I am not able to understand that, why its not responding in nexus phones.
please share your knowledge with me.
Back button and Home Button are not keys. The actions may be implemented this way on some platforms (for example the back button could send the "escape" KeyEvent). But, this is NOT required.
In this case "KeyEvent" refers to a hardware keyboard (ex: a bluetooth keyboard). Note: software keyboards can be implemented to send these events as well, but they are not required to, and it should not be relied on.
On Nexus Devices, the back and home buttons are actually on screen software buttons. Thus, like the difference between a hardware and software keyboard, those events are not passed through the system in the same way as devices where there are physical back buttons to press.
Related
I want to create an app that is not usable by the user but performs some functionality outside the app, the app just run in the foreground and listen to the press of android hardware/software buttons, the likes of Power button, home button, volume up and down. It triggers a call when for instance, the volume up button is pressed down for 5 secs and notification appears.
I have checked solutions like this
Intercepting the back button
How to override the behavior of the volume buttons in an Android application?
But this solutions have something to do with the user accessing the app directly, i mean to perform this functionalities you have to be on the app which i don't want.
The app was suppose to be run on a smart watch which runs android OS. I was thinking if i can perform this on android phone, it should be easier on the smart watch.
Note:
The App is for targeted users, who will be trained to understand that behavior.
I'm making an Android app using Xamarin and C#. The app will always be running in Kiosk-mode in which the bottom navigation bar of the device will be removed and navigation outside of the app is blocked.
However, when the user connects a bluetooth keyboard they can hit the physical home button and return to the android home screen which should not be accessible.
I'm aware that there are ways to disable the home button, but I haven't been able to get any of them to work properly as they're generally hacks since Android doesn't directly allow developers to intercept the home button.
Is there anyway I can remap the home key of bluetooth keyboards connected? Are there any other solutions to this that I maybe overlooking?
You can't intercept Android Home Button due to security of device, however if you have rooted device you can experiment with Android Key Layout Files https://source.android.com/devices/input/key-layout-files.html
But that requires root on device
Another method with root is to find the correct /dev/input/eventX device, get exclusive access to it, and then use the /dev/uinput interface to create a new device. Then write all events from the keyboard other than home key to the new interface. I haven't tested this, but in theory it should work.
You could perhaps create a dummy launcher that simply exits as soon as it starts. But it would disable the home button altogether, not just on the BT keyboard.
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 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.