Android 7.0以上静默安装时采用常用的修改,此方法仅限于有系统权限的情况下的使用。
http://m.blog.csdn.net/x995630736/article/details/74348098
if (Build.VERSION.SDK_INT>=24){
String [] args={"pm", "install","-i ","所需要覆盖安装的应用的包名", "-r",filePath };
result=SystemUtils.installBySilent(args,filePath);
}else{
String[] args ={ "pm", "install", "-r", filePath };
result=SystemUtils.installBySilent(args,filePath);
}
// 安装方法 1 为安装成功, 安装前确保安装文件存在 就是你的应用帮别的应用实现静默升级,这里的包名还是用你的应用包名
public static int installBySilent(String [] args,String filePath) {
int result = 0;
try {
File file = new File(filePath);
if (filePath == null || filePath.length() == 0
|| (file = new File(filePath)) == null
|| file.length() <= 0 || !file.exists() || !file.isFile()) {
return 1;
}
ProcessBuilder processBuilder = new ProcessBuilder(args);
Process process = null;
BufferedReader successResult = null;
BufferedReader errorResult = null;
StringBuilder successMsg = new StringBuilder();
StringBuilder errorMsg = new StringBuilder();
try {
process = processBuilder.start();
successResult = new BufferedReader(new InputStreamReader(
process.getInputStream()));
errorResult = new BufferedReader(new InputStreamReader(
process.getErrorStream()));
String s;
while ((s = successResult.readLine()) != null) {
successMsg.append(s);
}
while ((s = errorResult.readLine()) != null) {
errorMsg.append(s);
}
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (successResult != null) {
successResult.close();
}
if (errorResult != null) {
errorResult.close();
}
} catch (IOException e) {
e.printStackTrace();
}
if (process != null) {
process.destroy();
}
}
if (successMsg.toString().contains("Success")
|| successMsg.toString().contains("success")) {
result = 1;
} else {
result = 2;
}
MyLog.i("ControlService","App升级信息:" + "successMsg:" + successMsg + ", ErrorMsg:" + errorMsg);
} catch (Exception e) {
result = -1;
}
return result;
}
网友评论