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.
Related
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
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;
};
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.
I would like to know when the user pressed the home button while he was running my app.
BUT:
The problem is that I don't want to edite the existing code.
meaning I don't want to add logic to the existing activities onPause() method.
The only solution I found was to add a service to the application which listens to the Log detecting if there was an intent to run the
HOME: Starting: Intent { act=android.intent.action.MAIN
cat=[android.intent.category.HOME]
Is there any other way to do it or is it really impossible?
The optimal solution would have been to catch an intent in the Manifest.xml, like:
action android:name="android.intent.category.HOME"
and implement a new class to catch it. (But it doesn't seem to catch it).
I would like to know when the user pressed the home button while he was running my app.
What specifically are you trying to achieve?
BUT: The problem is that I don't want to edite the existing code.
By definition, that is impossible.
The only solution I found was to add a service to the application which listens to the Log detecting if there was an intent to run the
That is modifying the code, violating your own requirement. Moreover, it requires a permission that really you should not be asking for.
Is there any other way to do it or is it really impossible?
That depends on what specifically you are trying to achieve.
The optimal solution would have been to catch an intent in the Manifest.xml...and implement a new class to catch it. (But it doesn't seem to catch it).
HOME is a category. It is not an action. Home screens are activities that respond to the MAIN action in the HOME category. However, it is modifying the code, violating your own requirement.
You can use key press event handler
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_HOME) {
// Home key pressed
} else {
return super.onKeyDown(keyCode, event);
}
return false;
}
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);
}