android7.0 静默安装

android7.0 静默安装,可以用来更新已经安装的应用。无法获取安装进度,可以使用系统广播来监听应用的安装、卸载、更新。7.1.2版本亲测可行。不过使用了系统签名再次签名!

/**
 * 静默安装
 * @param installPath
 * @param packageName
 * @return
 */
public static boolean installApkInSilence(String installPath, String packageName,int flag) {
   LogUtil.e("installApkInSilence "+AppCenter.isSystemApp);
   if(!AppCenter.isSystemApp){
      return false;
   }
   LogUtil.e("installApkInSilence ");
   Class<?> pmService;
   Class<?> activityTherad;
   Method method;
   try {
      activityTherad = Class.forName("android.app.ActivityThread");
      Class<?> paramTypes[] = getParamTypes(activityTherad, "getPackageManager");
      method = activityTherad.getMethod("getPackageManager", paramTypes);
      Object PackageManagerService = method.invoke(activityTherad);
      pmService = PackageManagerService.getClass();
      Class<?> paramTypes1[] = getParamTypes(pmService, "installPackageAsUser");
      method = pmService.getMethod("installPackageAsUser", paramTypes1);
      method.invoke(PackageManagerService, installPath, null, 0x00000002, packageName, getUserId(Binder.getCallingUid()));//getUserId
      LogUtil.e("installApkInSilence ok");
      return true;
   } catch (ClassNotFoundException e) {
      LogUtil.e("installApkInSilence ClassNotFoundException"+e.getMessage());
      Write.debug("installApkInSilence ClassNotFoundException"+e.getMessage());
   } catch (NoSuchMethodException e) {
      LogUtil.e("installApkInSilence NoSuchMethodException"+e.getMessage());
      Write.debug("installApkInSilence NoSuchMethodException"+e.getMessage());
   } catch (IllegalAccessException e) {
      LogUtil.e("installApkInSilence IllegalAccessException"+e.getMessage());
      Write.debug("installApkInSilence IllegalAccessException"+e.getMessage());
   } catch (InvocationTargetException e) {
      LogUtil.e("installApkInSilence InvocationTargetException"+e.getMessage());
      Write.debug("installApkInSilence InvocationTargetException"+e.getMessage());
   }
   return false;
}

private static Class<?>[] getParamTypes(Class<?> cls, String mName) {
   Class<?> cs[] = null;
   Method[] mtd = cls.getMethods();
   for (int i = 0; i < mtd.length; i++) {
      if (!mtd[i].getName().equals(mName)) {
         continue;
      }

      cs = mtd[i].getParameterTypes();
   }
   return cs;
}

猜你喜欢

转载自blog.csdn.net/xiaoyi848699/article/details/79271851