Detect home button press in android Detect home button press in android android android

Detect home button press in android


Following code works for me :)

HomeWatcher mHomeWatcher = new HomeWatcher(this);mHomeWatcher.setOnHomePressedListener(new OnHomePressedListener() {    @Override    public void onHomePressed() {        // do something here...    }    @Override    public void onHomeLongPressed() {    }});mHomeWatcher.startWatch();
import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.content.IntentFilter;import android.util.Log;public class HomeWatcher {    static final String TAG = "hg";    private Context mContext;    private IntentFilter mFilter;    private OnHomePressedListener mListener;    private InnerReceiver mReceiver;    public HomeWatcher(Context context) {        mContext = context;        mFilter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);    }    public void setOnHomePressedListener(OnHomePressedListener listener) {        mListener = listener;        mReceiver = new InnerReceiver();    }    public void startWatch() {        if (mReceiver != null) {            mContext.registerReceiver(mReceiver, mFilter);        }    }    public void stopWatch() {        if (mReceiver != null) {            mContext.unregisterReceiver(mReceiver);        }    }    class InnerReceiver extends BroadcastReceiver {        final String SYSTEM_DIALOG_REASON_KEY = "reason";        final String SYSTEM_DIALOG_REASON_GLOBAL_ACTIONS = "globalactions";        final String SYSTEM_DIALOG_REASON_RECENT_APPS = "recentapps";        final String SYSTEM_DIALOG_REASON_HOME_KEY = "homekey";        @Override        public void onReceive(Context context, Intent intent) {            String action = intent.getAction();            if (action.equals(Intent.ACTION_CLOSE_SYSTEM_DIALOGS)) {                String reason = intent.getStringExtra(SYSTEM_DIALOG_REASON_KEY);                if (reason != null) {                    Log.e(TAG, "action:" + action + ",reason:" + reason);                    if (mListener != null) {                        if (reason.equals(SYSTEM_DIALOG_REASON_HOME_KEY)) {                            mListener.onHomePressed();                        } else if (reason.equals(SYSTEM_DIALOG_REASON_RECENT_APPS)) {                            mListener.onHomeLongPressed();                        }                    }                }            }        }    }}
public interface OnHomePressedListener {    void onHomePressed();    void onHomeLongPressed();}


This is an old question but it might help someone.

@Overrideprotected void onUserLeaveHint(){    Log.d("onUserLeaveHint","Home button pressed");    super.onUserLeaveHint();}

According to the documentation, the onUserLeaveHint() method is called when the user clicks the home button OR when something interrupts your application (like an incoming phone call).

This works for me.. :)


It is impossible to detect and/or intercept the HOME button from within an Android app. This is built into the system to prevent malicious apps that cannot be exited.