Android静默安装

静默安装

First blood
今天公司有个静默安装的需求,由于是自己的系统,所以操作起来比较容易,无需kengdie的root权限,在网上找了下资料,记录如下:

具体实现

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
static DataOutputStream dataOutputStream = null;
static BufferedReader errorStream = null;
static Process process;
static boolean result = false;
public static boolean slientInstall(String apkPath)
{
try {
process = Runtime.getRuntime().exec("su"); //获取su权限
dataOutputStream = new DataOutputStream(process.getOutputStream());//获取
String command = "pm install -r "+apkPath+"\n"; //安装命令
String path = apkPath;
Log.e("Tag",command);
Log.e("Tag",path+"+++>path");
dataOutputStream.write(command.getBytes(Charset.forName("utf-8")));
dataOutputStream.flush();
dataOutputStream.writeBytes("exit\n");
dataOutputStream.flush();
try {
process.waitFor();
errorStream = new BufferedReader(new InputStreamReader(process.getErrorStream())); //记录安装的信息
String msg = "";
String line;
while ((line = errorStream.readLine())!= null)
{
msg+=line;
}
Log.e("Tag","安装的信息是"+msg);
if (!msg.contains("Failure"))
{
return true;
}
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
catch (IOException e)
{
e.printStackTrace();
}finally {
try {
if (dataOutputStream!=null){
dataOutputStream.close();
}
if (errorStream!= null){
errorStream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return result;
}

上面就实现了静默安装的功能,需要应用有root 权限才行,第一次写博客代码有点乱,,zzzzz
待续。。

###作者
fuyifang

Contents
  1. 1. 静默安装
    1. 1.1. 具体实现
|