下载新版本以及跳转安装

public class TfkDownLoadService extends Service {//下载新版本服务

	private int NOTIFY_ID = 0;
	public static boolean isrunningLoadingActivity = false;
	public static boolean done = false;
	private Context mContext = this;
	private NotificationManager mNotificationManager;
	private Notification mNotification;
	String url = "";
	String path = "";
	String name = "";
	Resources r;

	private Handler handler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			if (!Thread.currentThread().isInterrupted()) {
				Intent intent = null;
				switch (msg.what) {
				case 0:
					break;
				case 1: // 不用通知
					break;
				case 2:// 未在loading 通知下载完成
					int icon = R.drawable.icon;
					long when = System.currentTimeMillis();
					mNotification = new Notification(icon, r
							.getString(R.string.update_title), when);
					mNotification.flags = Notification.FLAG_AUTO_CANCEL;
					intent = new Intent(Intent.ACTION_VIEW);
					intent.setDataAndType(Uri.fromFile(new File(
							Tfk.downLoadPath + name)),
							"application/vnd.android.package-archive");

					PendingIntent contentIntent = PendingIntent.getActivity(
							mContext, 0, intent,
							PendingIntent.FLAG_UPDATE_CURRENT);
					mNotification.setLatestEventInfo(mContext, r
							.getString(R.string.update_title), r
							.getString(R.string.update_down_done), contentIntent);
					mNotificationManager.notify(NOTIFY_ID, mNotification);
					break;
				default:
					break;
				}
				stopSelf();
			}
		}

	};

	@Override
	public void onCreate() {
		super.onCreate();
		mNotificationManager = (NotificationManager) getSystemService(android.content.Context.NOTIFICATION_SERVICE);
		r = getResources();
	}

	@Override
	public void onStart(Intent intent, int startId) {
		Log.d(Tfk.tag, "name:" + intent.getStringExtra("name"));
		Log.d(Tfk.tag, "url:" + intent.getStringExtra("url"));
		Log.d(Tfk.tag, "path:" + intent.getStringExtra("path"));

		super.onStart(intent, startId);
		name = intent.getStringExtra("name");
		url = intent.getStringExtra("url");
		path = intent.getStringExtra("path");
		new Thread() {
			public void run() {
				try {
					startDownload(url, path, name);
				} catch (IOException e) {
					e.printStackTrace();
				}
			};
		}.start();
	}

	private void startDownload(String url, String path, String filename)
			throws IOException {

		URL myURL = new URL(url);
		URLConnection conn = myURL.openConnection();
		conn.connect();
		InputStream is = conn.getInputStream();
		int fileSize = 0;
		int downLoadSize = 0;
		fileSize = conn.getContentLength();
		System.out.println("fileSize:"+fileSize);
		//fileSize = 
		if (fileSize <= 0)
			throw new RuntimeException("can't get file size");
		if (is == null)
			throw new RuntimeException("stream is null");
		FileOutputStream fos = new FileOutputStream(path + filename);
		byte buf[] = new byte[1024];
		// downLoadSize = 0;
		int downloadCount = 0;
		int rate = 0;
		Intent intent = null;
		do {
			int numread = is.read(buf);
			if (numread == -1) {
				break;
			}
			fos.write(buf, 0, numread);
			downLoadSize += numread;
			rate = (int) ((double) downLoadSize / (double) fileSize * 100);
			if ((downloadCount == 0) || rate > downloadCount) {
				downloadCount += 1;
				intent = new Intent();
				intent.setAction("android.intent.action.MY_RECEIVER");
				intent.putExtra("progress", rate);
				sendBroadcast(intent);
				// sendMsg(1, rate);
				if (rate >= 100) {
					if (!isrunningLoadingActivity) {
						sendMsg(2, rate);
					} else {
						sendMsg(1, rate);
					}
				}
			}

		} while (true);

		// sendMsg(2, rate);
		try {
			is.close();
			done = true;
		} catch (Exception ex) {
			Log.e("tag", "error: " + ex.getMessage(), ex);
		}

	}

	private void sendMsg(int flag, int rate) {
		Message msg = new Message();
		msg.what = flag;
		msg.arg1 = rate;
		handler.sendMessage(msg);
	}

	@Override
	public IBinder onBind(Intent intent) {
		return null;
	}
}

下面是activity中确认下载后的工作


private void doDownLoad(String url1, String path1) {
		// 创建ProgressDialog对象
		tfkDialog = new ProgressDialog(LoadingActivity.this);

		// 设置进度条风格
		tfkDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);

		// 设置ProgressDialog 标题
		tfkDialog.setTitle(resources.getString(R.string.update_down_title));

		// 设置ProgressDialog提示信息
		tfkDialog.setMessage(resources.getString(R.string.update_downloading));

		// // 设置ProgressDialog标题图标
		// tfkDialog.setIcon(R.drawable.img2);
		// 设置ProgressDialog 的进度条是否不明确 false 就是不设置为不明确
		tfkDialog.setIndeterminate(false);

		// 设置ProgressDialog 进度条进度
		tfkDialog.setProgress(100);

		// 设置ProgressDialog 是否可以按退回键取消
		tfkDialog.setCancelable(false);
		// 让ProgressDialog显示
		tfkDialog.show();
		url = url1;
		path = path1;

		url = sr.getVersionCheckReponse().getAppUrl();
		filename = url.substring(url.lastIndexOf("/") + 1);

		Intent intent = new Intent();
		intent.setClass(LoadingActivity.this, TfkDownLoadService.class);
		intent.putExtra("name", filename);
		intent.putExtra("path", Tfk.downLoadPath);
		intent.putExtra("url", url);
		LoadingActivity.this.startService(intent);
	}


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值