android home按键拦截

home按键默认现在没法屏蔽,但是mtk由于特殊需要,需要拦截home按键,于是添加了一套机制实现。


1:在alps\frameworks\base\core\java\android\view WindowManager.java


里面添加一个属性
        /** M: Window flag:  When this flag is set, the home key can be dispatched
        * to the window.
         * {@hide} */
        public static final int FLAG_HOMEKEY_DISPATCHED = 0x80000000;

2:在 alps\frameworks\base\policy\src\com\android\internal\policy\impl PhoneWindowManager.java 
里面的
    public long interceptKeyBeforeDispatching(WindowState win, KeyEvent event, int policyFlags) {
接口里面,添加
       if (keyCode == KeyEvent.KEYCODE_HOME) {
            /// M: [ALPS00054781]Dispatch the home key to the application @{
            if (win != null && win.getAttrs() != null) {
                final int flag = win.getAttrs().flags;
                if ((flag & WindowManager.LayoutParams.FLAG_HOMEKEY_DISPATCHED) != 0) {
                    // the window wants to handle the home key, so dispatch it to it.
                    return 0;
                }
            }

通过这里来拦截home按键
3:如果需要app拦截
在 activity 的 onCreate 里面增加
getWindow().addFlags(FLAG_HOMEKEY_DISPATCHED);


即可。

你可能感兴趣的:(源码,android,home)