本段代码的作用是当点击按键的时候,通过后台呗你要安装的apk安装完成,方法体你可以拷贝出来在你的应用程序中
套用,不用做任何修改哦!这段代码就是送给你的!呵呵,不过要通过源码编译的!
代码片段(1)[全屏查看所有代码]
1. [代码]android静默安装
01
这个还要一个非常重要的前提是要在源码下面编译一下 哦!
02
03
04
05
btn_backinstall.setOnClickListener(
new
OnClickListener() {
06
07
@Override
08
public
void
onClick(View v) {
09
10
String fileName = Environment.getExternalStorageDirectory()
11
+
"/feng"
+
"/MyHttpTest.apk"
;
12
install(fileName);
13
}
14
});
15
}
16
17
18
19
20
21
public
String install(String apkAbsolutePath ){
22
String[] args = {
"pm"
,
"install"
,
"-r"
, apkAbsolutePath };
23
String result =
""
;
24
ProcessBuilder processBuilder =
new
ProcessBuilder(args);
25
Process process =
null
;
26
InputStream errIs =
null
;
27
InputStream inIs =
null
;
28
try
{
29
ByteArrayOutputStream baos =
new
ByteArrayOutputStream();
30
int
read = -
1
;
31
process = processBuilder.start();
32
errIs = process.getErrorStream();
33
while
((read = errIs.read()) != -
1
) {
34
baos.write(read);
35
}
36
baos.write(
"/n"
.getBytes());
37
inIs = process.getInputStream();
38
while
((read = inIs.read()) != -
1
) {
39
baos.write(read);
40
}
41
byte
[] data = baos.toByteArray();
42
result =
new
String(data);
43
}
catch
(IOException e) {
44
e.printStackTrace();
45
}
catch
(Exception e) {
46
e.printStackTrace();
47
}
finally
{
48
try
{
49
if
(errIs !=
null
) {
50
errIs.close();
51
}
52
if
(inIs !=
null
) {
53
inIs.close();
54
}
55
}
catch
(IOException e) {
56
e.printStackTrace();
57
}
58
if
(process !=
null
) {
59
process.destroy();
60
}
61
}
62
return
result;
63
}
64
65
66
67
<uses-permission android:name=
"android.permission.WRITE_EXTERNAL_STORAGE"
/>
68
<uses-permission android:name=
"android.permission.INSTALL_PACKAGES"
/>
69
<uses-permission android:name=
"android.permission.DELETE_PACKAGES"
/>
70
<uses-permission android:name=
"android.permission.CLEAR_APP_CACHE"
/>
71
<uses-permission android:name=
"android.permission.CLEAR_APP_USER_DATA"
/>
72
<uses-permission android:name=
"android.permission.READ_PHONE_STATE"
/>
73
74
75
文献链接:http: