从网络下载apk 到 rom 或者 sd 卡

				 URL url = new URL(info.getUrl());
//				URL url = new URL(surl);
				interceptFlag = false;
				// InetAddress ip = InetAddress.getByName("deamo.5ydian.com");
				// Log.e(TAG, ip.toString());
				HttpURLConnection conn = (HttpURLConnection) url
						.openConnection();
				conn.connect();
				int length = conn.getContentLength();
				InputStream is = conn.getInputStream();
				if (Environment.getExternalStorageState().equals(
						Environment.MEDIA_MOUNTED)) {
					File ApkFile = new File(savePath, apkname);
					if (ApkFile.exists())
						ApkFile.delete();
					ApkFile.createNewFile();
					FileOutputStream fos = new FileOutputStream(ApkFile);

					int count = 0;
					byte buf[] = new byte[1024];
					int times = 0;

					do {
						int numread = is.read(buf);
						count += numread;
						if ((times == 512) || (count == length)) {
							progress = (int) (((float) count / length) * 100);
							if (DEBUG)
								Log.e(TAG, "download process = " + progress);

							handler.sendEmptyMessage(DOWN_UPDATE);
							times = 0;
							// continue;
						}
						times++;
						if (numread <= 0) {
							// 下载完成通知安装
							fos.flush();
							fos.close();
							is.close();
							interceptFlag = true;
							break;
						}
						fos.write(buf, 0, numread);
					} while (!interceptFlag);// 点击取消就停止下载.
					if (DEBUG)
						Log.e(TAG, "下载完成--");

				} else {
					String apkPath = "/data/data/" + mContext.getPackageName()
							+ "/files";
					File file = new File(apkPath, apkname);
					if (file.exists())
						file.delete();
					file.createNewFile();
					FileOutputStream os = new FileOutputStream(file);
					int times = 0;
					int count = 0;
					byte[] buf = new byte[1024];

					do {
						int numread = is.read(buf);
						count += numread;
						if ((times == 512) || (count == length)) {
							progress = (int) (((float) count / length) * 100);
							if (DEBUG)
								Log.e(TAG, "download process = " + progress);

							handler.sendEmptyMessage(DOWN_UPDATE);
							times = 0;
							// continue;
						}
						times++;
						if (numread <= 0) {
							// 下载完成通知安装
							os.flush();
							os.close();
							is.close();
							interceptFlag = true;
							break;
						}
                                   //从网络下载写入文件时,一定要注意使用此方式写入
                                    os.write(buf, 0, numread);
					} while (!interceptFlag);// 点击取消就停止下载.
					if (DEBUG)
						Log.e(TAG, "下载完成--");
				}
				// 安装更新后的软件
				Message msg = new Message();
				msg.what = DOWN_OVER;
				handler.sendMessageDelayed(msg, 2000);


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值