当前位置:   article > 正文

Android拦截HOME键_android 屏蔽home键

android 屏蔽home键

Android拦截按键

Android拦截HOME按键

拦截HOME按键需要修改Android的framework层

  • 具体代码文件路径android/frameworks/base/services/core/java/com/android/server/policy/PhoneWindowManager.java

PhoneWindowManager的interceptKeyBeforeDispatching方法,这个方法对按键事件做了二次拦截
当返回值为-1则说明事件被拦截,返回值为0则说明事件放开执行。

举例在某个APP中屏蔽对应的HOME按键;

  • 如下代码为识别当前的APP是哪个?
public String getTopActivity(Context context){                                                                                                            
         ActivityManager am = (ActivityManager) context.getSystemService(context.ACTIVITY_SERVICE);
          ComponentName cn = am.getRunningTasks(1).get(0).topActivity;
          Log.d(TAG, "pkg:" + cn.getPackageName() + " cls:"+cn.getClassName());//包名加类名
          return cn.getClassName();
     }

public boolean isBackgroundActivity(Context context, String packageName) {
          ActivityManager am = (ActivityManager) context.getSystemService("activity");
         List<ActivityManager.RunningTaskInfo> tasks = am.getRunningTasks(1);
          if (!tasks.isEmpty()) {
              ComponentName topActivity = ((ActivityManager.RunningTaskInfo) tasks.get(0)).topActivity;
              Log.w(TAG, "topActivity packageName = " + topActivity.getPackageName());
              if (!topActivity.getPackageName().equals(packageName)) {
                  Log.w(TAG, packageName + " is background!");
                  return true;
              }
          }
          Log.w(TAG, packageName + " is foreground!");
          return false;
     }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 如下代码判断针对对应的APP进行HOME按键拦截:
/** {@inheritDoc} */
     @Override
     public long interceptKeyBeforeDispatching(WindowState win, KeyEvent event, int policyFlags) {
......
// First we always handle the home key here, so applications
         // can never break it, although if keyguard is on, we do let
         // it handle it, because that gives us the correct 5 second
         // timeout.
        if (keyCode == KeyEvent.KEYCODE_HOME) {
 
             // If we have released the home key, and didn't do anything else
             // while it was pressed, then it is time to go home!
             if (!down) {
                 //添加的判断当前前台运行APP代码
                 if (!isBackgroundActivity(mContext, PKG_NAME_xxxxx)) {
                         Log.d(TAG, "xxxx app is foreground");
                         if (!getTopActivity(mContext).equals(ACTIVITY_NAME_xxxx)) {                                                          
                                 Log.d(TAG, "xxxxx app is foreground activity");
                                 return -1;
                         }
                        return -1;
                 }
                 
 
                 cancelPreloadRecentApps();

                 mHomePressed = false;
                 if (mHomeConsumed) {
                     mHomeConsumed = false;
                     return -1;
                 }
                 ......
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32

备注:
直接在onKeyDown中判断通过keyCode == KeyEvent.KEYCODE_HOME是不能拦截的

Android拦截BACK按键

这个在Activity中自行重写回调即可,具体见如下示意代码:

@Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(keyCode == KeyEvent.KEYCODE_BACK){
        	//按键按下时返回处理逻辑可以放在这里
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        if(keyCode == KeyEvent.KEYCODE_BACK){
             //按键释放时返回处理逻辑可以放在这里
            return true;
        }
        return super.onKeyUp(keyCode, event);
    }

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

注:在onKeyDown或onKeyUp中使用return true或return false,都会拦截掉返回按键。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/代码探险家/article/detail/848431
推荐阅读
相关标签
  

闽ICP备14008679号