Android拦截HOME键

Posted 天残神功

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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;
     
  • 如下代码判断针对对应的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;
                 
                 ......

备注:
直接在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);
    

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

以上是关于Android拦截HOME键的主要内容,如果未能解决你的问题,请参考以下文章

Android Back Home键监听

Android Back Home键监听

Android 按键 播放视频时,拦截上下键事件

Android 11.0 播放视频时,拦截上下键事件

android 如何通过app来关机,或者捕获home键

Android 进阶Android Home 键监听