先说结论:所有修改只适用定制系统有系统源码的场景,否则上层App只能监听到HOME按键的广播(而且由于Home键的特殊性,完全可靠的监听同样需要在framework层修改源码),无办法拦截,下面的方法亲自验证过,参考https://blog.csdn.net/u013286571/article/details/50012901了,修改了其中顺序有误的地方

1. 源码修改 com.android.server.wm.WindowManagerService

            if (attrs != null) {                //add by sg , 注释掉对参数类型的判断/*                if (win.mAttrs.type != attrs.type) {                    throw new IllegalArgumentException(                            "Window type can not be changed after the window is added.");                }*/                flagChanges = win.mAttrs.flags ^= attrs.flags;                attrChanges = win.mAttrs.copyFrom(attrs);                if ((attrChanges & (WindowManager.LayoutParams.LAYOUT_CHANGED                        | WindowManager.LayoutParams.SYSTEM_UI_VISIBILITY_CHANGED)) != 0) {                    win.mLayoutNeeded = true;                }            }

2. 源码修改 com.android.server.policy.PhoneWindowManager

public long interceptKeyBeforeDispatching(WindowState win, KeyEvent event, int policyFlags)方法
方法内部添加一个局部变量标志位isGohome,默认为true

   final boolean down = event.getAction() == KeyEvent.ACTION_DOWN;   final boolean canceled = event.isCanceled();+ boolean isGohome = true;

找到Home键事件,增加从WindowManager获取参数,这个参数我们会在activity的onAttach方法进行设置,当接受到该参数时,则将标志位isGohome设为false

        if (keyCode == KeyEvent.KEYCODE_HOME) {+            WindowManager.LayoutParams attrs1 = win != null ? win.getAttrs() : null;+           if (attrs1 != null) {+                int type1 = attrs1.type;+               if (type1 == WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG) {+                    isGohome = false;+                }+            }

调用返回桌面方法前根据标志位进行判断,默认没有设置参数的activity就直接返回桌面,配置了WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG的activity就不响应

                if(isGohome == true){                    handleShortPressOnHome();                }

3. 在app需要拦截的activity

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

更多相关文章

  1. [android]获取各应用的启动次数和运行时间
  2. 【Android】安卓AVD无法上网解决方案
  3. Android(安卓)从入门到资深学习路线
  4. Android(安卓)Camera API2中采用CameraMetadata用于从APP到HAL的
  5. android事件拦截处理机制图解
  6. android TIPS小结
  7. Android(安卓)webview 微信分享图片
  8. 浅谈Android程序与JavaScript脚本的交互
  9. 斐波那契函数的优化

随机推荐

  1. Android iOS测试区别
  2. Android中sqlite的基本增删查改用法
  3. Android Alert Dialog解决点击按钮对话框
  4. Android之Adapter:连接后端数据和前端显
  5. Android : GestureDetector手势检测
  6. Android性能优化(二)——context 引起的
  7. android中随手指拖动滑屏
  8. Android:最全面的 Webview 详解
  9. android点滴(29) android中设置用户自定
  10. 从Android到React Native开发(二、通信与