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)
Related
I am building an application using Xamarin and it's Android Player for Android. Whenever I hit the back button it seems to back out of the application completely. I want to be able to change this default to go back to the previous page in my app. How do I override the back button behavior?
So the previous answer is correct, you can trap the hardware back button and do whatever you want with it. But I want to make sure you understand why this is happening. Android handles the hardware back button for you, and most of the time, letting Android handle it will work, it knows what to do.
But in your case, you're not actually navigating at all. Your click handler is removing one layout file and replacing it with another. This is the equivalent of showing/hiding a div in web development. You're not actually changing the screen(page).
Because of this, when you hit the back button, you're still on the first (and only) screen in your app, so the OS does the only thing it knows to do, it closes the app.
If you want to continue with the paradigm of showing/hiding layouts in lieu of actually navigating, I would trap the hardware back button and re-swap your layout files.
public override bool OnKeyDown(Keycode keyCode, KeyEvent e)
{
if (keyCode == Keycode.Back) {
SetContentView(Resource.Layout.Login)
return false;
}
return true;
}
But the true solution that I would recommend would be to read up on how to truly navigate in Xamarin Android. Swapping your layout files and putting all the logic for your entire app in one Activity will be very hard to maintain.
You can capture the OnKeyDown and decide whether to allow the Back button to ripple up the event chain or not:
public override bool OnKeyDown(Keycode keyCode, KeyEvent e)
{
if (keyCode == Keycode.Back) {
Toast.MakeText (this, "Back button blocked", ToastLength.Short).Show ();
return false;
}
Toast.MakeText (this, "Button press allowed", ToastLength.Short).Show ();
return true;
}
you can handle that in the OnBackPressed() event.
I want to run code only when the home button (when the app is sent to background) is pressed. I tried using the lifecycle-method but the problem is that they also get executed when and other dialog/activity is started. I only want to check if the application is sent to background. How can I achieve that?
In onOptionsItemSelected(MenuItem item);, check to see if the item clicked is the home button:
EDIT:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
finish();
return true;
}
return super.onOptionsItemSelected(item);
}
Activity.onUserLeaveHint() fires on your activity when the Activity is going to be backgrounded as a result of user action, such as pressing the home button or switching apps.
Well since you are writing the code you can set some boolean flags when you start explicitly another acitivty, and check that when your activity goes through onPause()...
if they are false, it means someone else is pausing your activity.
Maybe not the most elegant solution but it will work if that is the only problem you have.
You could have a tracking system (basically a counter) to know when one of your activities is resumed and paused, thus allowing you to know if any of your activities is open.
This way you could know if your app is "open". but this would not apply only to the home button, but to the back button (last back to close your app) or even opening another app from the notification bar.
Android OS will kill your application to free resources, it wont stay in the background all the time. Use service if you want your app keep running in the background
If you want to do when the home button is pressed and the activity is going background, you can override this on the activity:
#Override
public boolean onKeyUp(int keyCode, KeyEvent event)
{
switch(keyCode)
{
case KeyEvent.KEYCODE_HOME:
//somthing you want to do
default:
return super.onKeyUp(keyCode, event);
}
}
I am using the KeyUp event because that's when the app goes backgroud, the user can do a longPress and I don't think you want to do the method when the app still open.
this is sort of a cheat answer, but i find it to be much more reliable.
one method that's always called when you press the home button is the surfacedestroyed method for any surfaceviews contained in any of the program's activities.
Putting the code you want to execute there, having the method point to another method you want to execute, or having the method trigger a flag that points to the method you want to execute will do the job.
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.
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.
How to finish the application on HOME button click?
You don't - just let Android suspend your app and tidy it up when necessary.
You should only be finishing the Activity by detecting the click and calling finish() on the activity.
As already mentioned before you really should consider NOT using this approach to finish your application.
Anywho: Here is some code you can use to detect Home-Button pushes and call appropriate functions.
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
if (event.getAction() == KeyEvent.ACTION_DOWN) {
switch (keyCode) {
case KeyEvent.KEYCODE_HOME:
finish();
return true;
}
}
return super.onKeyDown(keyCode, event);
}
You can set the intent you used to start acitvity with flag FLAG_ACTIVITY_NO_HISTORY, and according to the doc:
public static final int FLAG_ACTIVITY_NO_HISTORY
If set, the new activity is not kept in the history stack. As soon as
the user navigates away from it, the activity is finished. This may
also be set with the noHistory attribute. Constant Value: 1073741824
(0x40000000)
This might fit the use case.
Android did't gave permission to programmers to handle home button for user convenience. when user wants sudden exit from the application he will press the home button.