How do I detect back/home press in an accessibility Service? - android

The title pretty much sums it up.
I want to detect back/home button press using an Accessibility Service.
It seems that onKeyEvent is not triggered when pressing these buttons.

it is possible to detect key events if you create your own accessibility service. After that, you need to implement your behavior for the onKeyEvent().
Here is an example:
public class AccessibilityKeyDetector extends AccessibilityService {
...
#Override
protected boolean onKeyEvent(KeyEvent event) {
if (event.getKeyCode() == KeyEvent.KEYCODE_BACK || event.getKeyCode() == KeyEvent.KEYCODE_HOME) {
// Do your magic
}
..
}
I am assuming that you have checked the accessibility permissions and set the correct configuration of your service.
I hope this code fits you. If you have more doubts, please contact me.

Related

android how to disable changing through apps

currently I am working on a parental control app and I want the child keep from removing the app. For this, I want to lock the device when the child wants to disable my app from device admins (in settings/security) and show an activity to enter the password. But after the activity is shown, I can still use the device by changing through apps (by double square button on the device, I don't know its name).
How can I lock the device such that the user only can see my password asking activity & can't do anything but either enter the password or cancel?
Are you talking about the kiosk mode in android ?
please follow How to implement kiosk mode in android ?
here on Back / Home / Minimize button you can open your dialog for password and if password is true then finish() your current activity.
you can also use KeyEvent for that
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0)
{
this.moveTaskToBack(true);
return true;
}
return super.onKeyDown(keyCode, event);
}
you can find all android keys here

How to receive Broadcast on PowerButton

I am making an app in which if power key is pressed twice,The app will perform some task.Please provide me some help.I have made it but its not working.
I am including my class files here,Please correct me if I am going into wrong direction.
Broadcaster
Listener
I don't know how to insert code here it is giving some error so I included links here.I am sorry for that.Please help me.
Android recommends avoiding double clicks and using the long-click besides the normal click.
For the long click use this :
#Override
public boolean onKeyLongPress(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_POWER) {
// Do something here...
return true;
}
return super.onKeyLongPress(keyCode, event);
}
You also need to add this to the manifest
<uses-permission android:name="android.permission.PREVENT_POWER_KEY" />
If this doesnt work properly onKeyDown
But I have never actually used this.
My personal opinion though is that you shouldn't hook the power key if you are planning to publish your app because unless it is doing something really an app shouldn't prevent the user from closing the screen
Solved the problem by receiving broadcast on ACTION_SCREEN_ON/OFF.
When the power button is pressed screen turns on/off depending on the fact whether screen is on or off at the time when pressing the power button.
I used this facility and put the logic in code such that if the user presses power button more than 5 times an sms will be sent to the trusted contacts.

How to handle device home button click efficiently?

I want to handle device home button click in my android application.When googled it is said that this
Link works.But I have some doubts.
Is it supported by all android versions?If not which of them are supported?
Is there any consequence exist because of using onAttachedToWindow() method?
Is there any way to handle Home button click(Except this)?
Thanks in Advance
This only works in previous version. But from os version 4.0 it is not working (although in my emulators this doesnt work after api level 11 but I am pointing ics according to many other links).
Found this on other posts.
On older Android version this is working. But Android changed this, because they say "Home Button should stay Home Button" and they don't want that anybody override the Home Button. And because of this reason your code is not working anymore.
If you want to do something when the home button is pressed, then do this in the onPause method.
yeah its work please try this code
#Override
public void onAttachedToWindow() {
super.onAttachedToWindow();
this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
}
And now handle the key event like this,
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(keyCode == KeyEvent.KEYCODE_HOME)
{
Log.i("Home Button","Clicked");
}
if(keyCode==KeyEvent.KEYCODE_BACK)
{
finish();
}
return false;
};

How to override the side volume keys outside my android application

I would like to disable the side volume buttons so the only way to control the volume will be from a dedicated activity inside my android app.
I managed to disable it for all my activities by adding the following code:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
Log.d(TAG, "onKeyDown = " + keyCode);
if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) {
return true;
}
if (keyCode == KeyEvent.KEYCODE_VOLUME_UP) {
return true;
}
return super.onKeyDown(keyCode, event);
}
But I don't know how to disable it for the activities i start from my app (for example I start the gallery app)!
I know it is possible as 'Volume Locker' app doing similar stuff.
It is not possible within public APIs to suppress the key events outside of your own Activities, if there is an app that has managed to do it what they are doing would be considered malicious by the platform designers and will get fixed at some point.
Based on the description given for that app (note: I've never used it personally)
Prevent accidental changes to your volume settings, install Volume Locker today.
This app helps prevent against accidental volume changes by confirming the change you made, by either tray notification or a pop up. If you don't approve the change, the volume will be reset within a set amount of seconds... By setting the timeout to "instant", the locked volumes will revert instantly without prompting.
I suspect what that is actually doing is listening for the volume buttons using a similar technique to the one in this answer and just reverting whatever change was made instantly(ish). That would make it seem to the user like the key press did nothing but in reality what happened is the volume changed and then quickly changed back.

Notifications opening activities and the back button? Restricting to one instance?

There's a situation I have that's related to the Activity stack but I'm new enough to Android dev to not now how to respond to it.
I have an application that works like a stopwatch. When you start it, an ongoing notification goes in the notification tray and remains ongoing until you stop the stopwatch. I've noticed that if my stopwatch is running in the foreground, and I touch the notification, it creates a new instance of my stopwatch's activity and slides the old one off screen. This creates problems with the back button (you press back and the new instance goes away to reveal the old instance). It makes sense why this happens, but I don't want it to happen like this. What can I do to prevent multiple instances of my application from running?
What can I do to prevent multiple instances of my application from running?
In your Intent you use with the PendingIntent for the Notification, add setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_SINGLE_TOP); or setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);, whichever behavior fits your needs better.
You could also disable the back button. Not as elegant as CommonsWare solution but nothing wrong with some options!
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
return true;
}
return super.onKeyDown(keyCode, event);
}

Categories

Resources