Android代码工具集——apk的下载和进度条的显示

本文介绍了一个基于Android平台的文件下载任务实现方式,使用了AsyncTask进行后台下载,并通过ProgressDialog显示下载进度。文章详细展示了如何设置HTTP请求参数、读取网络数据、写入本地文件以及更新UI界面。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

public class DownloadTask extends AsyncTask<Void, Integer, String> {

	ProgressDialog progressDialog;
	Context ctx;
	String downUrl;

	public DownloadTask(Context ctx, String downUrl) {
		this.ctx = ctx;
		this.downUrl = downUrl;
		progressDialog = new ProgressDialog(ctx);
		progressDialog.setMax(100);
		progressDialog.setProgress(0);
		progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
		progressDialog.show();
		progressDialog.setCanceledOnTouchOutside(false);
	}

	@Override
	protected String doInBackground(Void... params) {
		String returnDownloadFileName = "";
		HttpParams httpParams = new BasicHttpParams();
		HttpConnectionParams.setConnectionTimeout(httpParams, 10000);
		HttpConnectionParams.setSoTimeout(httpParams, 15000);
		HttpClient client = new DefaultHttpClient(httpParams);
		HttpGet get = new HttpGet(downUrl);
		InputStream is = null;
		FileOutputStream fileOutputStream = null;
		try {
			HttpResponse response = client.execute(get);
			HttpEntity entity = response.getEntity();
			if (entity != null && entity.getContentLength() > 512) {
				long length = entity.getContentLength();
				is = entity.getContent();
				if (is != null) {
					fileOutputStream = ctx.openFileOutput(FileUtil.getFileName(downUrl),Context.MODE_WORLD_READABLE+ Context.MODE_WORLD_WRITEABLE);
					returnDownloadFileName = ctx.getFilesDir().getPath() + "/"+ FileUtil.getFileName(downUrl);
					byte[] buf = new byte[2048];
					int len = -1;
					int byteReadCount = 0; 
					while ((len = is.read(buf)) != -1) {
						byteReadCount += len;
						fileOutputStream.write(buf, 0, len);
						publishProgress((int) ((byteReadCount / (float) length) * 100));
					}
					fileOutputStream.flush();
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (fileOutputStream != null)
					fileOutputStream.close();
				if (is != null)
					is.close();
				client.getConnectionManager().shutdown(); 
			} catch (IOException ex) {
			}
		}
		return returnDownloadFileName;
	}

	@Override
	protected void onPostExecute(String result) {
		if (progressDialog != null && progressDialog.isShowing()) {
			progressDialog.dismiss();
		}
		try {
			 install(result);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	protected void onProgressUpdate(Integer... values) {
		progressDialog.setProgress(values[0]);
	}
	
	public  void install(String path)throws Exception {
		Intent intent = new Intent(Intent.ACTION_VIEW);
		intent.setDataAndType(Uri.fromFile(new File(path)), "application/vnd.android.package-archive");
		ctx.startActivity(intent);
	}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值