我说的这种方法 对开发程序环境要求比较苛刻,需要运行程序的系统必须已经root过的才可以用此方法:
代码很简单就几句话:
public boolean silentInstall(String apkPath)
{
//第一步,改变apk的权限
String cmd1 = "chmod 777 " + apkPath + " \n";
//第二步,注意,在android系统下映射pm的路径,这一条很重要,在android4.0以上需要添加系统环境变量,4.0以下就不需要
//如果是4.0以下,只需要运行pm install -r *.apk 即可
String cmd2 = "LD_LIBRARY_PATH=/vendor/lib:/system/lib pm install -r "
+ apkPath + " \n";
return execWithSID(cmd1, cmd2);
}
private boolean execWithSID(String... args)
{
boolean isSuccess = true;
Process process = null;
OutputStream out = null;
try
{
process = Runtime.getRuntime().exec("su");
out = process.getOutputStream();
DataOutputStream dataOutputStream = new DataOutputStream(out);
for (String tmp : args)
{
dataOutputStream.writeBytes(tmp);
}
dataOutputStream.flush(); // 提交命令
dataOutputStream.close(); // 关闭流操作
out.close();
// isSuccess = waitForProcess(process);
}
catch (IOException e)
{
e.printStackTrace();
isSuccess = false;
}
return isSuccess;
}
经测试,可以使用