需要先判断是否有root权限,可通过下面这个方法去进行判断
// 判断是否有root权限
public static boolean isRoot() {
PrintWriter PrintWriter = null;
Process process = null;
try {
process = Runtime.getRuntime().exec("su");
PrintWriter = new PrintWriter(process.getOutputStream());
PrintWriter.flush();
PrintWriter.close();
int value = process.waitFor();
//0代表成功 1代表失败
return value == 0 ? true : false;
} catch (Exception e) {
e.printStackTrace();
} finally {
if (process != null) {
process.destroy();
}
}
return false;
}
有root权限后,用pm install 命令进行安装
// 静默安装
private static void silenceInstall(String apkPath) {
PrintWriter PrintWriter = null;
Process process = null;
try {
process = Runtime.getRuntime().exec("su");
PrintWriter = new PrintWriter(process.getOutputStream());
PrintWriter.println("chmod 777 " + apkPath);
PrintWriter
.println("export LD_LIBRARY_PATH=/vendor/lib:/system/lib");
PrintWriter.println("pm install -r " + apkPath);
//包名 + 你要拉起的activity
PrintWriter.println("sleep 1; am start -n {包名}/.LaunchActivity");
PrintWriter.flush();
PrintWriter.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (process != null) {
process.destroy();
}
}
}
这样就OK了,可以去试试