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
Related
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.
I have an android application which kind of like a home screen of the build-in screen on android phone. I can list all the available applications and run them. One thing I don't know how to do is when user run an application from my apps, my application can trigger the hardware menu button to show the current application menus.
Is this possible ? If not, what about doing it if the device was rooted.
In activity use override onKeyDown like this :
#Override
public boolean onKeyDown(int keycode, KeyEvent e) {
switch(keycode) {
case KeyEvent.KEYCODE_MENU:
doSomething();
return true;
}
return super.onKeyDown(keycode, e);
}
In order to open up the menu from other application apart from the caller application, the device have to be rooted before the code to work.
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.
When I test my android application on my phone, the application don't want to quit and make a bug on my phone. My little app take 70MB on my phone and still alive all the day...
How can I close it?
Do I have to put a Listner for the button "return" or there is some methods made from the SDK???
Thanks !
EDIT:
My application still runing, even if I press "HOME" ... this is not normal, is it?
Application on mobile aren't meant to quit, because it's against the UX of mobile user.
Have a look at this discussion. Android: Is quitting an application frowned upon?
you can override onKeyDown function like this
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK)) {
finish();
}
return super.onKeyDown(keyCode, event);
}
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);
}