How to override home button in android 4.0
Asked Answered
I

1

7

I want to override the home button in my android activity. I have tried out few things related to this which are working for 2.3 and below but not for 4.0 above

@Override public boolean onKeyDown(int keyCode, KeyEvent event) {

    if(keyCode == KeyEvent.KEYCODE_HOME)
    {
        startActivity(new Intent(this, ActivityB.class));
        return true;
    }
    return super.onKeyDown(keyCode, event); }

and other is way

 @Override public void onAttachedToWindow() { 
     this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
     super.onAttachedToWindow(); 
 }

But its not helping me. any have idea about it please share information

Inoperable answered 9/4, 2014 at 4:26 Comment(7)
See #10026160Extracanonical
<category android:name="android.intent.category.HOME" />Query
There is no way to intercept the home button on Android , unless you make your app the home screen. If you want to handle the HOME button, implement a home screen.Bashan
you can't. but why do you want toJillian
@Raghunandan: That what the requirement says. and i want know if it not possible then why its is so?Inoperable
@Inoperable for security reasons so that malacious apps d not take controlJillian
possible duplicate of Disable Home Button in Android ICS (4.0)Smash
M
4

I also encountered such a problem and I am using the following class to listen to the home button click event.

public class HomeWatcher {

    static final String TAG = "HomeWatcher";

    private Context mContext;

    private IntentFilter mFilter;

    private OnHomePressedListener mListener;

    private InnerRecevier mRecevier;

    public HomeWatcher(Context context) {
        mContext = context;
        mFilter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
    }

    /**
     * set the home pressed listener, if set will callback the home pressed
     * listener's method when home pressed.
     * 
     * @param listener
     */
    public void setOnHomePressedListener(OnHomePressedListener listener) {
        mListener = listener;
        mRecevier = new InnerRecevier();
    }

    /**
     * start watch
     */
    public void startWatch() {
        if (mRecevier != null) {
            mContext.registerReceiver(mRecevier, mFilter);
        }
    }

    /**
     * stop watch
     */
    public void stopWatch() {
        if (mRecevier != null) {
            mContext.unregisterReceiver(mRecevier);
        }
    }

    class InnerRecevier 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.i(TAG, "receive action:" + action + ",reason:" + reason);
                    if (mListener != null) {
                        if (reason.equals(SYSTEM_DIALOG_REASON_HOME_KEY)) {

                            // home?
                            mListener.onHomePressed();
                        } else if (reason
                                .equals(SYSTEM_DIALOG_REASON_RECENT_APPS)) {

                            // ??home?
                            mListener.onHomeLongPressed();
                        }
                    }
                }
            }
        }

    }

}

Usage is as follows:

HomeWatcher mHomeWatcher = new HomeWatcher(Context);
mHomeWatcher.setOnHomePressedListener(new OnHomePressedListener() {

    public void onHomePressed() {

        //do your somthing...
    }

    public void onHomeLongPressed() {
    }

});

mHomeWatcher.startWatch();
Murrah answered 10/4, 2014 at 9:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.