Android updataApp更新安裝方法封裝,

前段時間一直忙着找工做的事,就沒有什麼時間去更新博客了。如今已經到新公司上班了。沒事就先更新下吧,這段時間以爲本身仍是有好多東西須要學習,每一次面試都感受本身能夠知道不少東西,好比如今主流什麼,你們都用什麼的東西等,都還挺不錯的,不過比較不喜歡換工做了。比較煩躁。 好了抱怨就到這裏了。開始今天的正題吧, java

上代碼: android


/* @author x.j 更新安裝APK
 * @time 2014.09.18
 */
public class UploadApp {
	private ProgressDialog mpDialog;// 建立精度條
	private int fileSize;// 設置文件大小
	private int downLoadFileSize;// 當前已下載的文件的大小
	private Context mContext;
	// APK的安裝路徑
	private static final String savePath = "/sdcard/updatedemo/"; //保存下載文件的路徑
    private static final String saveFileName = savePath + "UpdateDemo.apk";//保存下載文件的名稱

	/**
	 * 提示用戶更新
	 * 
	 * @param mcontext
	 * @param url.下載連接
	 * @param str.更新內容
	 */
	public void uploadApp(Context mcontext, String str, final String url) {
		this.mContext = mcontext;
		AlertDialog.Builder builder = new Builder(mContext);
		builder.setMessage("有新的版本升級,是否下載安裝?\n" + str);
		builder.setTitle("系統版本更新");// str能夠提示的內容顯示
		builder.setPositiveButton("肯定", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				mpDialog = new ProgressDialog(mContext);
				mpDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
				mpDialog.setTitle("提示");
				mpDialog.setMessage("正在下載中,請稍後");
				mpDialog.setIndeterminate(false);// 是進度條是否明確
				mpDialog.setCancelable(false);// 點擊返回按鈕的時候沒法取消對話框
				mpDialog.setCanceledOnTouchOutside(false);// 點擊對話框外部取消對話框顯示
				mpDialog.setProgress(0);// 設置初始進度條爲0
				mpDialog.incrementProgressBy(1);// 設置進度條增漲。
				mpDialog.show();
				new Thread() {
					public void run() {
						String apkUrl = url;// 下載APK的url
						URL url = null;
						try {
							url = new URL(apkUrl);
							HttpURLConnection con = (HttpURLConnection) url.openConnection();
							InputStream in = con.getInputStream();
							fileSize = con.getContentLength();// 獲取下載文件的長度
							File file = new File(savePath);
							if (!file.exists()) {
								file.mkdir();
								File fileOut = new File(saveFileName);// 下載文件的存放地址
								FileOutputStream out = new FileOutputStream(fileOut);
								byte[] bytes = new byte[1024];
								downLoadFileSize = 0;
								sendMsg(0);// sendMeg爲0的時候顯示下載完成
								int c;
								while ((c = in.read(bytes)) != -1) {
									out.write(bytes, 0, c);
									downLoadFileSize += c;
									sendMsg(1);
								}
								in.close();
								out.close();
							}
						} catch (Exception e) {
							e.printStackTrace();
						}
						sendMsg(2);
					}
				}.start();
				dialog.dismiss();
			}
		});
		builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {

			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();
			}
		});

		builder.create().show();
	}

	// 安裝apk方法
	private void installApk(String filename) {
		File file = new File(filename);
		Intent intent = new Intent();
		intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		intent.setAction(Intent.ACTION_VIEW);
		String type = "application/vnd.android.package-archive";
		intent.setDataAndType(Uri.fromFile(file), type);
		mContext.startActivity(intent);
		if (mpDialog != null) {
			mpDialog.cancel();
		}
	}

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

	private final Handler handler = new Handler(Looper.getMainLooper()) {
		public void handleMessage(Message msg) {
			if (!Thread.currentThread().isInterrupted()) {
				switch (msg.what) {
				case 0:
					mpDialog.setMax(100);
					break;
				case 1:
					int result = downLoadFileSize * 100 / fileSize;
					mpDialog.setProgress(result);
					break;
				case 2:
					mpDialog.setMessage("文件下載完成");
					installApk(saveFileName);
					break;
				case -1:
					String error = msg.getData().getString("error");
					mpDialog.setMessage(error);
					break;
				default:
					break;
				}
			}
			super.handleMessage(msg);
		}
	};
}



這裏可能會須要在權限中加上網絡權限呢,由於是須要從網絡上下載APK而後在覆蓋安裝。而後這個地方可能會出現一個異常就是沒有找到當前目錄。這個時候也須要添加權限就是容許寫入權限。

<!--往sdcard中寫入數據的權限 -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
<!--在sdcard中建立/刪除文件的權限 -->
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"></uses-permission>
相關文章
相關標籤/搜索