How to track mobile button(not widget button) click event - android

In Android application is it possible that when I press my mobile *(star) button(not widget button) then I can perform any particular events in my application? If it's possible, then how may I achieve it?

If you mean the * key from your hardware keyboard ( on the devices that have it) you can capture it using KeyCode.
Here you can find an extensive list of all the keys you can intercept.
To do it:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
switch (keyCode) {
case KeyEvent.KEYCODE_STAR: //here you check any key you want
{
//your code here
return true;
}
}
return super.onKeyDown(keyCode, event);
}
EDIT
Answering your comment, I don't believe this is possible. The KeyDown/Up events are handled on Activities. And you won't have an Activity active. Check this out!
EDIT
Yeah, according to this guy you can't.

If the button is within your own app, then yes.
If you mean a button in any other app (I think you mean the * key on the dial pad), then no.

Related

Restrict the home button Android 4.0 +

I am working in an launcher application ,In which i have to disable the home button event.
How can we do this in android 4.0 and above.
There is no way to intercept the home button on Android, unless you make your app the home screen. This is for security reasons, so that malicious apps cannot take over your device by overriding all the buttons that can exit. The home button is the one sure shot way to be able to leave any app.
If you want to handle the HOME button, implement a home screen.
For more information check the link below. check the answer by commonsware
Not able disable Home button on specific android devices
Here you have ;)
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_HOME)
{
return true;
}
return super.onKeyDown(keyCode, event);
}
You should use this in your activities.
You could use the method onPause() onPause
So if you press Home and return to you app you can run whatever you want.
Maybe it helps

Override android headset hook long press

I need to override the android heaset hook button, the long press causes the music player starts auntomatically and I need to avoid this.
Is it possible in Android?
I try:
#Override
public boolean onKeyLongPress(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_HEADSETHOOK) {
return true;
}
return false;
}
But it not works.
Thanks,
It is possible to just create your own gesture recognition to account for a long press. You can start a timer on KeyDown and then check that timer on KeyUp to see if the key up was at or lower than your long press time. Or you can use the getEventTime methods to do the same function. If your question was more specific on how to intercept the headset buttons I would recommend this article, Allowing applications to play nice(r) with each other: Handling remote control buttons.
If you need some other kind of help or some code samples let me know

How to handle KeyEvent of home button in an Android application?

I'm developing an application in Android, and I use the the following code to handle the KeyEvent for the back button:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK))
{
finish();
}
return super.onKeyDown(keyCode, event);
}
How would I go about doing this for the home button?
You can not control the behaviour of Home key. You will not get the event of Home key but you can disable it but it is highly recommended you should not do this.Before blocking the Home key refer this post.
However you can block the home key like this:
#Override
public void onAttachedToWindow() {
this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
super.onAttachedToWindow();
}
As this question suggests: Android Overriding home key this is unfortunately not possible. Perhaps there is some other way to implement your desired behavior?
Obvious answer is - handle home key press in onPause() method of your activity. This callback is called when user hits home key. Guaranted. Home key is not for you, but
for OS and user.
Read these :
Overriding the Home button - how do I get rid of the choice?
Android - How to catch that the Home button was pressed?
The home button is supposed to do one thing and one thing only and consistently. Get the user back to the the HOME screen. Even if you could override it's behavior it would be an extremely user-unfriendly thing to do. So don't do it and solve your problem differently!strong text

Home button listener

Using the setOnKeyListener I can able to listen for all physical buttons except Home and End button, is there any possibility to catch the action of Home button.
You may try this on Android 4.0+:
1. Register a BroadcastReceiver for Intent.ACTION_CLOSE_SYSTEM_DIALOGS.
2. Call Intent.getStringExtra("reason") to get the reason. Reasons are below:
"homekey" for home key pressed;
"assist" for home key long pressed;
You do not need to catch Home button. If user press Home and some other Activity comes to foreground, your app goes to background and onPause() is called in your current Activity. You may override that function to clean search string or anything you need.
UPDATE:
More clean solution is to use flag FLAG_ACTIVITY_NO_HISTORY when starting that critical activity. So, when your activity goes to background system will close it properly for you.
You want to use public boolean dispatchKeyEvent(KeyEvent event), as covered here: http://developer.android.com/reference/android/app/Activity.html#dispatchKeyEvent%28android.view.KeyEvent%29.
Use it like so:
#Override
public boolean dispatchKeyEvent(KeyEvent event)
{
// do whatever you want to do here, then return true if you handled the key code
if (event.getAction() == KeyEvent.ACTION_DOWN) {
switch (event.getKeyCode()) {
case KeyEvent.KEYCODE_BACK:
mBackDown = true;
return true;
case KeyEvent.KEYCODE_HOME:
mHomeDown = true;
return true;
}
}
return super.dispatchKeyEvent(event); // let the default handling take care of it
}
Let me know if that works for you.
EDIT: not sure why this doesn't work for you, but without looking through the rest of your code it would be hard to tell what exactly is going on. However, for your task, what I would recommend is that you use the finishOnTaskLaunch manifest attribute, as described at http://developer.android.com/guide/topics/manifest/activity-element.html#finish: properly used (set it to true) this will make sure that if your Activity is relaunched it will shutdown any existing instance.
This is only possible if you modify the main android source code. Although this is not recommended for app purposes. But more geared towards hidden menus.
public static final int KEYCODE_HOME
Since: API Level 1
Key code constant: Home key. This key is handled by the framework and is never delivered to applications.
Constant Value: 3 (0x00000003)

How to disable (or hide) home button in AVD

I am developing an application that I don't want user to touch home/back button. Trust me, I have a good reason for it.
What I need to do is to disable home/back or even the keyboard using terminal.
I've look through commands in adb shell already and I cannot find any command to fix this.
I'm not sure about AVD, but in real phone this buttons are hard buttons. So, no software can remove them. All you can do is to override the behaviour for this buttons using onKeyDown() method.
Using the following piece of a code in an Activity subclass would work for intercepting key events:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
return true;
// return super.onKeyDown(keyCode, event);
}
The back key is the constant KeyEvent.KEYCODE_BACK; the home button should be similar.

Categories

Resources