Android如何实现文件下载并自动安装apk包!!!
Android程序使用代码的安装和卸载!!!
1 public class MainActivity extends Activity {
2
3 private String currentFilePath = "", currentTempFilePath = "", strURL = "",
4 fileEx = "", fileNa = "";
5 File file2 = new File(Environment.getExternalStorageDirectory()+"");
6 @SuppressLint("SetJavaScriptEnabled")
7 @Override
8 public void onCreate(Bundle savedInstanceState) {
9 super.onCreate(savedInstanceState);
10 setContentView(R.layout.activity_main);
11 fragmentLayout = (View) findViewById(R.id.fragment);
12 gotoButton = (Button) findViewById(R.id.goto_button);
13 gotoButton.setOnClickListener(gotoListener);
14 }
15
16 OnClickListener gotoListener = new OnClickListener() {
17
18 @Override
19 public void onClick(View v) {
20
21 // String webUrl = "http://fancy.189.cn/portal/getclientapk";
22 // Uri uri = Uri.parse(webUrl);
23 // Intent intent = new Intent(Intent.ACTION_VIEW, uri);
24 // startActivity(intent);
25 // finish();
26 strURL = "http://fancy.189.cn/portal/getclientapk";
27 /* 取得欲安装程序名称 */
28 fileEx = "telecom_mdesk";
29 fileNa = ".apk";
30 getFile(strURL);
31 }
32 };
33
34 /* 处理下载URL文件自定义函数 */
35 private void getFile(final String strPath) {
36 try {
37 if (strPath.equals(currentFilePath)) {
38 getDataSource(strPath);
39 }
40 currentFilePath = strPath;
41 Runnable r = new Runnable() {
42 public void run() {
43 try {
44 getDataSource(strPath);
45 } catch (Exception e) {
46 Log.e("TAG", e.getMessage(), e);
47 Log.e("TAG", "------>");
48 }
49 }
50 };
51 new Thread(r).start();
52 } catch (Exception e) {
53 e.printStackTrace();
54 }
55 }
56
57 /* 取得远程文件 */
58 private void getDataSource(String strPath) throws Exception {
59 // if (!URLUtil.isNetworkUrl(strPath))
60 // mTextView01.setText("错误的URL");
61 // else
62 // {
63 /* 取得URL */
64 URL myURL = new URL(strPath);
65 /* 创建连接 */
66 //URLConnection conn = myURL.openConnection();
67 HttpURLConnection conn = (HttpURLConnection) myURL.openConnection();
68 conn.setRequestMethod("POST");
69 // is = connection.getInputStream();
70 //conn.connect();
71 /* InputStream 下载文件 */
72 InputStream is = conn.getInputStream();
73 if (is == null)
74 throw new RuntimeException("stream is null");
75
76 /* 创建临时文件 */
77 File myTempFile = File.createTempFile(fileEx, fileNa, file2);
78 /* 取得临时文件路径 */
79 currentTempFilePath = myTempFile.getAbsolutePath();
80 /* 将文件写入暂存盘 */
81 FileOutputStream fos = new FileOutputStream(myTempFile);
82 byte buf[] = new byte[128];
83 do {
84 int numread = is.read(buf);
85 if (numread <= 0)
86 break;
87
88 Log.v("TAG", "下载中---");
89
90 fos.write(buf, 0, numread);
91 } while (true);
92
93 /* 打开文件进行安装 */
94 openFile(myTempFile);
95 try {
96 is.close();
97 } catch (Exception ex) {
98 Log.e("TAG", "error: " + ex.getMessage(), ex);
99 }
100 // }
101 }
102
103 /**
104 * 在手机上打开文件
105 */
106 private void openFile(File f) {
107 Intent intent = new Intent();
108 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
109 intent.setAction(android.content.Intent.ACTION_VIEW);
110
111 /* 调用getMIMEType()来取得MimeType */
112 String type = "application/vnd.android.package-archive";
113 /* 设置intent的file与MimeType */
114 intent.setDataAndType(Uri.fromFile(f), type);
115 startActivity(intent);
116 }
117 }
安装:
1 String str = "/CanavaCancel.apk";
2 String fileName = Environment.getExternalStorageDirectory() + str;
3 Intent intent = new Intent(Intent.ACTION_VIEW);
4 intent.setDataAndType(Uri.fromFile(new File(fileName)), "application/vnd.android.package-archive");
5 startActivity(intent);
卸载:
1 Uri packageURI = Uri.parse("package:com.demo.CanavaCancel");
2 Intent uninstallIntent = new Intent(Intent.ACTION_DELETE, packageURI);
3 startActivity(uninstallIntent);
Environment拥有一些可以获取环境变量的方法
package:com.demo.CanavaCancel 这个形式是 package:程序完整的路径 (包名+程序名)。
下载apk程序代码:
1 protected File downLoadFile(String httpUrl) {
2 // TODO Auto-generated method stub
3 final String fileName = "updata.apk";
4 File tmpFile = new File("/sdcard/update");
5 if (!tmpFile.exists()) {
6 tmpFile.mkdir();
7 }
8 final File file = new File("/sdcard/update/" + fileName);
9
10 try {
11 URL url = new URL(httpUrl);
12 try {
13 HttpURLConnection conn = (HttpURLConnection) url
14 .openConnection();
15 InputStream is = conn.getInputStream();
16 FileOutputStream fos = new FileOutputStream(file);
17 byte[] buf = new byte[256];
18 conn.connect();
19 double count = 0;
20 if (conn.getResponseCode() >= 400) {
21 Toast.makeText(Main.this, "连接超时", Toast.LENGTH_SHORT)
22 .show();
23 } else {
24 while (count <= 100) {
25 if (is != null) {
26 int numRead = is.read(buf);
27 if (numRead <= 0) {
28 break;
29 } else {
30 fos.write(buf, 0, numRead);
31 }
32
33 } else {
34 break;
35 }
36
37 }
38 }
39
40 conn.disconnect();
41 fos.close();
42 is.close();
43 } catch (IOException e) {
44 // TODO Auto-generated catch block
45
46 e.printStackTrace();
47 }
48 } catch (MalformedURLException e) {
49 // TODO Auto-generated catch block
50
51 e.printStackTrace();
52 }
53
54 return file;
55 }
打开APK程序代码:
1 private void openFile(File file) {
2 // TODO Auto-generated method stub
3 Log.e("OpenFile", file.getName());
4 Intent intent = new Intent();
5 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
6 intent.setAction(android.content.Intent.ACTION_VIEW);
7 intent.setDataAndType(Uri.fromFile(file),
8 "application/vnd.android.package-archive");
9 startActivity(intent);
10 }