Andorid更新

public class UpdateTest extends Activity {
	public static final String UPDATE_URL = "http://10.1.170.6:8080/android_test/update.apk";

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		((TextView) findViewById(R.id.tv_main)).setText("版本:" + AppInfo.getVerionName() + "(" + AppInfo.getVerionCode() + ")");

		final UpdateInfo info = new UpdateInfo();
		info.setToUpdate(true);
		info.setUpdateUrl(UPDATE_URL);
		info.setVersionCode(2);
		info.setUpdateText("1. 更新到新版本 \n 2. 修复若干bug\n 3.没有了,谢谢");

		new AlertDialog.Builder(UpdateTest.this).setTitle("更新提示").setMessage(info.getUpdateText()).setCancelable(false)
				.setPositiveButton("立即更新", new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						downloadApk(info);
					}
				}).setNegativeButton("以后再说", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int whichButton) {
					}
				}).create().show();
	}

	private ProgressDialog mProgressDialog;

	public void showProgressDialog(CharSequence title, CharSequence message) {
		if (mProgressDialog == null) {
			mProgressDialog = new ProgressDialog(this);
			mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
		}
		mProgressDialog.setMessage("正在下载");
		mProgressDialog.setProgress(0);
		mProgressDialog.setTitle(title);
		mProgressDialog.setMessage(message);
		mProgressDialog.show();
	}

	private void downloadApk(final UpdateInfo updateInfo) {
		new AsyncTask<String, Object, Boolean>() {

			@Override
			protected void onPreExecute() {
				showProgressDialog("", "正在连接服务器...");
			}

			@Override
			protected Boolean doInBackground(String... params) {
				try {
					Request request = Request.createHttpRequest(updateInfo.getUpdateUrl());
					request.setMthod(HttpMethod.Get);
					Response response = request.execute();
					BufferedInputStream is = response.getBufferedInputStream();
					File file = new File(AppInfo.getAppRootPath() + "/pwmob.apk");
					if (file.exists()) {
						file.delete();
					}
					file.createNewFile();
					FileOutputStream fos = new FileOutputStream(file);
					int length = (int) response.getContentLength(), readLen = 0, currentLen = 0;
					publishProgress("正在下载安装程序", length);
					byte[] buffer = new byte[10000];
					while (currentLen < length) {
						readLen = is.read(buffer);
						fos.write(buffer, 0, readLen);
						currentLen += readLen;
						publishProgress(currentLen);
					}
					buffer = null;
					return true;
				} catch (IOException e) {
					e.printStackTrace(System.err);
				} catch (HttpRequestException e) {
					e.printStackTrace();
				} catch (Exception e) {
					e.printStackTrace();
				}
				return false;
			}

			@Override
			protected void onProgressUpdate(Object... values) {
				if (values.length > 1) {
					mProgressDialog.setTitle(values[0].toString());
					mProgressDialog.setMax((Integer) values[1] / 1024);
				} else {
					mProgressDialog.setProgress((Integer) values[0] / 1024);
				}
			}

			@Override
			protected void onPostExecute(Boolean result) {
				if (result) {
					Intent intent = new Intent(Intent.ACTION_VIEW);
					intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); // 注意本行的FLAG设置
					intent.setDataAndType(Uri.fromFile(new File(AppInfo.getAppRootPath() + "/pwmob.apk")),
							"application/vnd.android.package-archive");
					UpdateTest.this.startActivity(intent);
					TestApp.exitApplication();
				}
			}

		}.execute("");
	}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值