1. 程式人生 > >android7.0 靜默安裝

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; }