Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/svg/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在android 4.0中覆盖home按钮_Android_Overriding_Android 4.0 Ice Cream Sandwich_Android Homebutton - Fatal编程技术网

如何在android 4.0中覆盖home按钮

如何在android 4.0中覆盖home按钮,android,overriding,android-4.0-ice-cream-sandwich,android-homebutton,Android,Overriding,Android 4.0 Ice Cream Sandwich,Android Homebutton,我想在我的android活动中覆盖home按钮。我尝试了一些与此相关的东西,它们适用于2.3及以下版本,但不适用于4.0以上版本 @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if(keyCode == KeyEvent.KEYCODE_HOME) { startActivity(new Intent(this, ActivityB.class)); retur

我想在我的android活动中覆盖home按钮。我尝试了一些与此相关的东西,它们适用于2.3及以下版本,但不适用于4.0以上版本

@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); }
另一个是方式

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

但这对我没有帮助。如果您对此有任何想法,请共享信息

我也遇到了这样的问题,我正在使用下面的类来收听home button click事件

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();
                        }
                    }
                }
            }
        }

    }

}
用法如下:

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

    public void onHomePressed() {

        //do your somthing...
    }

    public void onHomeLongPressed() {
    }

});

mHomeWatcher.startWatch();

请看,在Android上没有办法拦截home按钮,除非您将应用程序设置为主屏幕。如果您想处理HOME按钮,请实现HOME屏幕。您不能。但是你为什么想要to@Balu出于安全原因,使恶意应用程序无法控制