判断Activity是否在顶层

在PhoneWindowManager.java中做需求时,需要判断是否是当前显示的Activity。采用的第二种。
第一种:经测试,效率较低
 public boolean isActivityTop(Sting packageName){
       ActivityManager am = (ActivityManager) mContext
               .getSystemService(Context.ACTIVITY_SERVICE);
        if (am != null) {
           ComponentName cn = am.getRunningTasks(1).get(0).topActivity;
           if (cn != null && cn.getPackageName() != null) {
               if(packageName.equals(cn.getPackageName())){
                  return true;
                };
            }
        }
        return false;
  }



第二中:

    private boolean isTopWindowActivity(WindowState win,String activityname){

        if (activityname == null) return false;

        final WindowState curWin = mFocusedWindow != null ? mFocusedWindow
                : mTopFullscreenOpaqueWindowState;
        WindowState windowState = null;
        if (win != null && win.getAttrs() != null) {
            windowState = win;
        } else if (curWin != null && curWin.getAttrs() != null) {
            windowState = curWin;
        }

        if (windowState.toString().indexOf(activityname) != -1){
            return true;
        }
        return false;
    }

你可能感兴趣的:(Framwork)