安装:
1 |
String "/CanavaCancel.apk" ; |
2 |
String |
3 |
Intent new Intent(Intent.ACTION_VIEW); |
4 |
intent.setDataAndType(Uri.fromFile( new File(fileName)), "application/vnd.android.package-archive" ); |
5 |
startActivity(intent); |
卸载:
1 |
Uri "package:com.demo.CanavaCancel" ); |
2 |
Intent new Intent(Intent.ACTION_DELETE, |
3 |
startActivity(uninstallIntent); |
Environment拥有一些可以获取环境变量的方法
package:com.demo.CanavaCancel 这个形式是 package:程序完整的路径 (包名+程序名).
//下载apk程序代码
01 |
protected File |
02 |
// |
03 |
final String "updata.apk" ; |
04 |
File new File( "/sdcard/update" ); |
05 |
if (!tmpFile.exists()) |
06 |
tmpFile.mkdir(); |
07 |
} |
08 |
final File new File( "/sdcard/update/" + |
09 |
10 |
try { |
11 |
URL new URL(httpUrl); |
12 |
try { |
13 |
HttpURLConnection |
14 |
.openConnection(); |
15 |
InputStream |
16 |
FileOutputStream new FileOutputStream(file); |
17 |
byte [] new byte [ 256 ]; |
18 |
conn.connect(); |
19 |
double count 0 ; |
20 |
if (conn.getResponseCode() 400 ) |
21 |
Toast.makeText(Main. this , "连接超时" , |
22 |
.show(); |
23 |
} else { |
24 |
while (count 100 ) |
25 |
if (is null ) |
26 |
int numRead |
27 |
if (numRead 0 ) |
28 |
break ; |
29 |
} else { |
30 |
fos.write(buf, 0 , |
31 |
} |
32 |
33 |
} else { |
34 |
break ; |
35 |
} |
36 |
37 |
} |
38 |
} |
39 |
40 |
conn.disconnect(); |
41 |
fos.close(); |
42 |
is.close(); |
43 |
} catch (IOException |
44 |
// |
45 |
46 |
e.printStackTrace(); |
47 |
} |
48 |
} catch (MalformedURLException |
49 |
// |
50 |
51 |
e.printStackTrace(); |
52 |
} |
53 |
54 |
return file; |
55 |
} |
56 |
//打开APK程序代码 |
57 |
58 |
private void openFile(File |
59 |
// |
60 |
Log.e( "OpenFile" , |
61 |
Intent new Intent(); |
62 |
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
63 |
intent.setAction(android.content.Intent.ACTION_VIEW); |
64 |
intent.setDataAndType(Uri.fromFile(file), |
65 |
"application/vnd.android.package-archive" ); |
66 |
startActivity(intent); |
67 |
} |