【Android】Android Q适配startActivity

最近在适配Android Q,发现用的Hook框架不好用了,于是在同事大佬的帮助下拿到源码,翻了一下,以下是区别

Android 8

public ActivityResult execStartActivity(
            Context who, IBinder contextThread, IBinder token, Activity target,
            Intent intent, int requestCode, Bundle options) {
        //.....省略不重要的
        try {
            intent.migrateExtraStreamToClipData();
            intent.prepareToLeaveProcess(who);
           int result = ActivityManager.getService()
                .startActivity(whoThread, who.getBasePackageName(), intent,
                        intent.resolveTypeIfNeeded(who.getContentResolver()),
                        token, target != null ? target.mEmbeddedID : null,
                        requestCode, 0, null, options);
            checkStartActivityResult(result, intent);
        } catch (RemoteException e) {
            throw new RuntimeException("Failure from system", e);
        }
        return null;
    }

重点是这一句

ActivityManager.getService().startActivity();

这是通过ActivityManagerService启动的。

Android Q

@UnsupportedAppUsage
public ActivityResult execStartActivity(Context paramContext, IBinder paramIBinder1, IBinder paramIBinder2, Activity paramActivity, Intent paramIntent, int paramInt, Bundle paramBundle)
  {
    //.....省略不重要的
    try{
      paramIntent.migrateExtraStreamToClipData();
      paramIntent.prepareToLeaveProcess(paramContext);
      localIActivityTaskManager = ActivityTaskManager.getService();
      paramIBinder1 = paramContext.getBasePackageName();
      ??? = paramIntent.resolveTypeIfNeeded(paramContext.getContentResolver());
      if (paramActivity != null) {
        try{
          paramContext = paramActivity.mEmbeddedID;
        }
        catch (RemoteException paramContext){
          break label287;
        }
      } else {
        paramContext = null;
      }
      try{
        checkStartActivityResult(localIActivityTaskManager.startActivity(localIApplicationThread, paramIBinder1, paramIntent, (String)???, paramIBinder2, paramContext, paramInt, 0, null, paramBundle), paramIntent);
        return null;
      }
      catch (RemoteException paramContext) {}
      throw new RuntimeException("Failure from system", paramContext);
    }
    catch (RemoteException paramContext) {}
  }

可以看到重点是这里

localIActivityTaskManager = ActivityTaskManager.getService();
localIActivityTaskManager.startActivity();

这里是通过ActivityTaskManagerService来启动的。
因为启动类变了,所以Hook失效。

你可能感兴趣的:(【Android】Android Q适配startActivity)