1 /**
2 * 下载apk
3 */
4 private static void downApk(final String url) {
5 new Thread() {
6 public void run() {
7 HttpClient client = new DefaultHttpClient();
8 HttpGet get = new HttpGet(url);
9 HttpResponse response;
10 try {
11 response = client.execute(get);
12 HttpEntity entity = response.getEntity();
13 apkSize = (int) entity.getContentLength();
14
15 InputStream is = entity.getContent();
16
17 FileOutputStream fileOutputStream = null;
18 if (is != null) {
19 File file = null;
20 if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
21 file = new File(Environment.getExternalStorageDirectory(), UPDATE_SERVERAPK);
22 if (!file.exists()) {
23 file.createNewFile();
24 }
25 } else {
26 file = new File(context.getFilesDir(), "**.apk");
27 if (!file.exists()) {
28 file.createNewFile();
29 }
30
31 }
32 fileOutputStream = new FileOutputStream(file);
33 byte[] b = new byte[1024];
34 int charb = -1;
35 int count = 0;
36 while ((charb = is.read(b)) != -1) {
37 fileOutputStream.write(b, 0, charb);
38 count += charb;
39 downLoadFileSize = count;
40 sendMsg(1);
41
42 }
43 }
44 fileOutputStream.flush();
45 if (fileOutputStream != null) {
46 fileOutputStream.close();
47 }
48 sendMsg(2);
49 } catch (Exception e) {
50 // TODO Auto-generated catch block
51 e.printStackTrace();
52 }
53 }
54 }.start();
55 }
56
57 private static Handler handler = new Handler() {
58
59 @Override
60 public void handleMessage(Message msg) {
61
62 super.handleMessage(msg);
63
64 // 定义一个Handler,用于处理下载线程与UI间通讯
65 if (!Thread.currentThread().isInterrupted()) {
66 switch (msg.what) {
67 case 0:
68 progress.setMax(100);
69 case 1:
70 int temp = ApkSize / 100;
71
72 progress.setProgress((downLoadFileSize / temp));
73 break;
74 case 2:
75 progress.cancel();
76 update();
77 break;
78
79 case -1:
80 String error = msg.getData().getString("error");
81 Toast.makeText(context, error, 1).show();
82 break;
83 }
84 }
85
86 }
87 };
88
89 /**
90 * 下载完成,通过handler将下载对话框取消
91 */
92 private static void sendMsg(int flag) {
93 Message msg = new Message();
94 msg.what = flag;
95 handler.sendMessage(msg);
96 }
97
98 /**
99 * 安装应用
100 */
101 private static void update() {
102 Intent intent = new Intent(Intent.ACTION_VIEW);
103 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
104 intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
105 intent.setAction(Intent.ACTION_VIEW);
106
107 if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
108 intent.setDataAndType(Uri.fromFile(new File(Environment.getExternalStorageDirectory(), "**.apk")), "application/vnd.android.package-archive");
109
110 } else {
111 File file = new File(context.getFilesDir(), "**.apk");
112
113 String cmd = "chmod 777 " + file.getAbsolutePath();
114 try {
115 Runtime.getRuntime().exec(cmd);
116
117 } catch (Exception e) {
118
119 e.printStackTrace();
120 }
121
122 intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");
123
124 }
125 context.startActivity(intent);
126 }
127 }