I wanted to get Get Key press, Key release Events out of my Activity Screen
i.e ex When i am in Android Native Home Screen and if i Press Search KEY i need to fire my API..Is it Possible??
When i am in Android Native Home Screen and if i Press Search KEY i need to fire my API..Is it Possible??
No. Key events are generally only delivered to the foreground activity. The two exceptions are the CAMERA button (on those few devices that have one) and the MEDIA button (found on headsets) -- those are sent as broadcast Intents if the foreground activity does not consume the event.
I don't know about the SEARCH button, but BACK button you can override.
As I know HOME button can't be override.
Maybe consider using MENU button to display option menu?
#Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if (keyCode == KeyEvent.KEYCODE_BACK)
{
//your method
return false;
}
return false;
}
Related
I am developing an application in API level 2.2. In my application whenever I press Home button it forces my app to exit, and when I relaunch that app by clicking on its icon, it starts from where it exited. I have tried to change/ override functionality of Home button but it does not works.
I have tried as
`
public boolean onKeyDown (int keyCode, KeyEvent event){
if (keyCode == KeyEvent.KEYCODE_BACK)
{
startActivity(new Intent(CategoryFirstActivity.this,LastActivity.class));
finish();
}
else if (keyCode == KeyEvent.KEYCODE_HOME)
{
startActivity(new Intent(CategoryFirstActivity.this,LastActivity.class));
finish();
}
return false;
}`
But it works for Back button only and not for Home button.
I have also tried to get keyCode of Home key but I didn't get it.
I need solution to change Home Button functionality.
Simply put: better you do not change the behavior of your home button...
Instead of trying to reprogram your Home button, you could try using some of the Activity callbacks that Android provides. You can take a look at the Activity lifecycle here:
http://developer.android.com/reference/android/app/Activity.html
For example, you could try overriding the onPause callback in an effort to achieve the effect you're going for.
#Override
public void onPause() { /* do stuff */ }
In your case, do stuff might be returning the application to its initial state or forcing it to exit. I've never attempted a force quit myself, but there are some answers on this StackOverflow post:
How to exit from the application and show the home screen?
Best of luck!
My application settings can be opened and closed with hardware menu button (as well as with back button when navigating out of the root screen). There are many child screens in this preferences.
(And lets say, there are two activities - main and settings).
I'd like that I could return to the last preference screen I was working with when I access application settings again.
Saving last preferenceScreen key and using setPreferenceScreen(lastPreferenceScreenKey) is not relevant as in this case I can't navigate up the hierarchy.
Setting PreferenceActivity.launchMode="singleTask" and starting main activity when user presses menu button didn't help - when I starting settings activity again Its root screen appears.
Is it possible somehow or another?
Maybe try to use a back button pressed listener:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK)) {
// Back button pressed
}
return super.onKeyDown(keyCode, event);
}
Try to use sharedPreference for storing last visited screen and use a dispatcher activity to dispatch to the correct activity based on the preference on launching the application.
Refer this link for your reference
.
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
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
I'm starting an activity from a service, which displays some text.
Now if a user presses a key - say a back key, I would like that back button to be processed by any other app which may be running (not started by my service).
Is this possible? I have tried FLAG_NOT_FOCUSABLE, however, the back key is still not processed by any other app/ignored. Android, after some time keep giving a option for "Force Close" or 'Wait".
Any pointers will be helpful.
Just FYI, "whitepages" app does something like this - it shows a dialog window, however, doesn't process the back key.
Thanks.
I'm not sure if this is correct or not, but I've seen the same behavior in a few apps myself. I think they are using Toast messages to display the text. I'm not sure if a service can display Toast or not - you'll have to try... if not then you could also try having the Activity display the toast and immediately exit.
Only the currently running activity receives in the button pressed signal. All you can do is intercept the back button press by implementing:
onBackPressed()
On older devices this won't quite work and you'll need to do something like:
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if (keyCode == KeyEvent.KEYCODE_BACK) {
//Do something here
return true;
}
return super.onKeyDown(keyCode, event);
}
As far as passing the button press to another activity, I'm not sure you can do that. What you could do is pass the activity an intent that could specify the back button was pressed. However, this assumes that the other activity is setup to deal with such an intent.