1、<=Api 23 时 相信大家都是通过installPackage这个类反射来静默安装apk的。但是呢api24及其之后就没有这个方法了,你再PackageManagerService或IPackageManager都是没有这个方法了。所以我们只能通过installPacakageAsUser来实现静默安装了。代码如下:
/**
* void installPackageAsUser(in String originPath,
* in IPackageInstallObserver2 observer,
* int flags,
* in String installerPackageName,
* int userId);
* @param installPath
*/
private void installApkInSilence(String installPath,String packageName) {
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, 0x00000040, packageName, getUserId(Binder.getCallingUid()));//getUserId
} catch (C