2

In my application I need to send log-out request to server when ever user goes out of application via clicking on log-out button or closing application by pressing home button key.

There is no problem with button and result is as I expect. The problem is how to get home button. Based on my research it is not possible to use onKeyDown(int keyCode, KeyEvent event) as we can use for getting back button.

The solution that I'm thinking about is registering a receiver and sending a broadcast whenever Home button clicked. So through receiver I can launch a service to send log-out request to server.

My current problem is I cannot get home button whenever i click it. This the code that I have written:

manifest.xml

<application ...

        <receiver android:name="HomeButtonReceiver" >
            <intent-filter>
                <category android:name="android.intent.category.HOME" />
            </intent-filter>
        </receiver>

</application>

HomeButtonReceiver

public class HomeButtonReceiver extends BroadcastReceiver {

    private final String TAG = "HomeButtonReceiver";

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.i(TAG, "inside onReceive()...");
    }
}

Any comments/suggestions would be appreciated. Thanks

Kara
  • 6,115
  • 16
  • 50
  • 57
Hesam
  • 52,260
  • 74
  • 224
  • 365
  • With [this](http://stackoverflow.com/questions/10025660/override-home-and-back-button-is-case-a-boolean-is-true/10025904#10025904) you can handle Home button, so just send broadcast on it. – Mohammed Azharuddin Shaikh Nov 09 '12 at 04:39
  • Thanks hotveryspicy, I tested your way but I was not successful. It could capture volume up/down but i couldn't get home press. – Hesam Nov 09 '12 at 06:08

3 Answers3

1

Why dont you use dispatchKeyEvent function. It can intercept the HOME button pressing event.

@Override
    public boolean dispatchKeyEvent(KeyEvent keyevent) {

        if (keyevent.getKeyCode() == KeyEvent.KEYCODE_HOME) {
            //Do here what you want
            return true;
        }
        else
            return super.dispatchKeyEvent(event);
   }
Sahil Mahajan Mj
  • 11,033
  • 8
  • 53
  • 100
  • Thanks SahilMahajanMj, i modified your code to if (event.getKeyCode() == KeyEvent.KEYCODE_HOME) { Log.i(TAG, "inside dispatchKeyEvent()..."); return true; } else Log.i(TAG, "..."); return super.dispatchKeyEvent(event); This code could capture volume up/down but when i presses home button nothing happened. – Hesam Nov 09 '12 at 06:13
0

What you can do is like this

Create your Home button event catcher

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if(keyCode==KeyEvent.KEYCODE_HOME){
       //Send your own custom broadcast message
    }
    return super.onKeyDown(keyCode, event);
}

Now you can create a broadcast receiver so that you can receive your message

You can refer here and here for how to send a broadcast messsage

Girish Nair
  • 5,148
  • 5
  • 40
  • 61
0

If you just need to find out when ever the user goes out of application , why don't you use the onPause(..) method of the Activity?

thechaoticpanda
  • 396
  • 2
  • 18
Binoy Babu
  • 16,699
  • 17
  • 91
  • 134
  • Thanks Binoy Babu, It is because when user closes the app then I need to send a request to server to cancel session id. If I do your suggested way then user cannot browse application. Therefore, I need to know when user closes application then I send that request. – Hesam Nov 09 '12 at 06:05