基于Android 8.1 的静默安装卸载

这个转自 : https://blog.csdn.net/u013270171/article/details/89320824
1.静默安装-------系统签名

/**
 * void installPackageAsUser(in String originPath,
 * in IPackageInstallObserver2 observer,
 * int flags,
 * in String installerPackageName,
 * int userId);
 * @param installPath
 */
public static 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 (ClassNotFoundException e) {
        e.printStackTrace();
    } catch (NoSuchMethodException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    } catch (InvocationTargetException e) {
        e.printStackTrace();
    }
}

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;
}
private static final int PER_USER_RANGE = 100000;
private static int getUserId(int uid) {
    return uid / PER_USER_RANGE;
}

这个卸载忘了是哪位老哥的了 抱歉

/**
 * 静默卸载 系统签名
 */
private void quietUnInstallApk() {
    if (checkDeviceAdminEnabled()) {
        Intent intent = new Intent();
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        PendingIntent sender = PendingIntent.getActivity(getActivity(), 0, intent, 0);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            PackageInstaller mPackageInstaller = getActivity().getPackageManager().getPackageInstaller();
            mPackageInstaller.uninstall("com.mcy.adbcommanddemo", sender.getIntentSender());// 卸载APK
        }
    } else {
        ToastUtil.show("Not Admin");
    }
}

猜你喜欢

转载自blog.csdn.net/xielunyan666/article/details/93079528