Android非Root下静默安装apk
程序员文章站
2022-04-21 11:58:50
1.实现静默安装应用的话需要系统权限:在AndroidManifest.xml中添加android:sharedUserId=“android.uid.system”.使用系统签名文件(platform.pk8、platform.x509.pem,签名文件位于build/target/product/security/目录下)对apk进行签名2.在AndroidManifest.xml添加相应需要的权限
1.实现静默安装应用的话需要系统权限:
在AndroidManifest.xml中添加android:sharedUserId=“android.uid.system”.
使用系统签名文件(platform.pk8、platform.x509.pem,签名文件位于build/target/product/security/目录下)对apk进行签名
2.在AndroidManifest.xml添加相应需要的权限
<uses-permission android:name="android.permission.INSTALL_PACKAGES" />
3.
/*
* 23版本以上的静默安装方法一
*/
private void install(String path,String packageName) {
PackageManager mPm = getApplicationContext().getPackageManager();
Class<?> paramTypes[] = getParamTypes(mPm.getClass(), "installPackage");
try {
Method method = mPm.getClass().getMethod("installPackage", paramTypes);
method.invoke(mPm, Uri.fromFile(new File(path)), null,INSTALL_REPLACE_EXISTING, packageName);
mHandler.sendEmptyMessageDelayed(1, 10000);
} catch (Exception e) {
e.printStackTrace();
}
}
private 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;
}
/*
* 23版本以上的静默安装方法二
*/
private void ApkInSilence(String installPath, String packageName,int type) {
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();
if(type == INSTALL_APK) {
Class<?> paramTypes1[] = getParamTypes(pmService, "installPackageAsUser");
method = pmService.getMethod("installPackageAsUser", paramTypes1);
method.invoke(PackageManagerService, installPath, null, 0x00000040, packageName, getUserId(Binder.getCallingUid()));//getUserId
}
else {
Class<?> paramTypes1[] = getParamTypes(pmService, "deletePackageAsUser");
method = pmService.getMethod("deletePackageAsUser", paramTypes1);
method.invoke(PackageManagerService, packageName, null, getUserId(Binder.getCallingUid()),0x00000040);//getUserId
}
Toast.makeText(this, "成功", 0).show();
} catch (Exception e) {
Toast.makeText(this, "失败", 0).show();
}
}
public static final int PER_USER_RANGE = 100000;
public static int getUserId(int uid) {
return uid / PER_USER_RANGE;
}
/**
* 23版本以下的静默安装方法
* @param path
*/
private void runInstall(String path) {
// 静默安装
IPackageManager mPm;
try {
Class<?> forName = Class.forName("android.os.ServiceManager");
Method method = forName.getMethod("getService", String.class);
IBinder iBinder = (IBinder) method.invoke(null, "package");
Log.e("yzy", "iBinder------------:");
mPm = IPackageManager.Stub.asInterface(iBinder);
File apkFile = new File(path);
mPm.installPackage(Uri.fromFile(apkFile), new MyObserver(), INSTALL_REPLACE_EXISTING, apkFile.getPath());
Toast.makeText(this, "安装成功", 0).show();
} catch (Exception e) {
Log.e("yzy", "Exception:"+e.toString());
Toast.makeText(this, "安装失败", 0).show();
}
}
private class MyObserver extends IPackageInstallObserver.Stub{
@Override
public void packageInstalled(String packageName, int returnCode)
throws RemoteException {
Log.e("yzy", "returnCode:"+returnCode);
}
}
本文地址:https://blog.csdn.net/vista1995/article/details/107578303