android版本更新全程解析

來源:互聯網
上載者:User

版本更新是每個項目必備的功能,思路大概都差不多,首先擷取到用戶端的版本號碼與伺服器上最新的版本進行比較,如果需要進行更新,則返回下載的連結,進行下載安裝,直接上代碼:

MainActivity:

package com.home.update;import android.os.Bundle;import android.app.Activity;public class MainActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);UpdateManager um = new UpdateManager(MainActivity.this);um.checkUpdateInfo();}}

版本更新工具類:

package com.home.update;import java.io.BufferedReader;import java.io.File;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.net.HttpURLConnection;import java.net.MalformedURLException;import java.net.URL;import org.apache.http.HttpResponse;import org.apache.http.HttpStatus;import org.apache.http.client.ClientProtocolException;import org.apache.http.client.HttpClient;import org.apache.http.client.methods.HttpGet;import org.apache.http.impl.client.DefaultHttpClient;import org.json.JSONException;import org.json.JSONObject;import android.app.AlertDialog;import android.app.Dialog;import android.app.AlertDialog.Builder;import android.content.Context;import android.content.DialogInterface;import android.content.Intent;import android.content.DialogInterface.OnClickListener;import android.content.pm.PackageManager.NameNotFoundException;import android.net.Uri;import android.os.Environment;import android.os.Handler;import android.os.Message;import android.view.LayoutInflater;import android.view.View;import android.view.WindowManager;import android.widget.ProgressBar;public class UpdateManager {private Context context;// 發起版本檢測的urlprivate final static String CHECKURL = "http://....";// 伺服器返回的apk安裝包下載路徑private static String downloadUrl = "";private Dialog noticeDialog;private ProgressBar mProgress;private boolean interceptFlag = false;private Dialog downloadDialog;private static final int DOWN_UPDATE = 1;private static final int DOWN_OVER = 2;private int progress;// 下載包安裝本地存放路徑private static final String savePath = Environment.getExternalStorageDirectory() + "/guyun/update/";private static final String saveFileName = savePath + "sport.apk";public UpdateManager(Context context) {this.context = context;}/** * 更新UI的Handler */private Handler handler = new Handler() {public void handleMessage(Message msg) {switch (msg.what) {case DOWN_UPDATE:mProgress.setProgress(progress);break;case DOWN_OVER:if (downloadDialog != null)downloadDialog.dismiss();installApk();break;default:break;}};};/** * 版本更新介面 */public void checkUpdateInfo() {// 向伺服器發出檢查請求,如果需要更新則彈出提示對話方塊if (isNeedUpdate()) {// 顯示提示對話方塊showNoticeDialog();}}/** * 檢查目前的版本是否需要更新 *  * @return */private boolean isNeedUpdate() {String versionName = getVersionName(context);boolean isNeedUpdate = testVersion(versionName);return isNeedUpdate;}/** * 擷取本地版本名稱 *  * @param context * @return */private String getVersionName(Context context) {String versionName = "";try {// 包名改為自己應用的包名即可versionName = context.getPackageManager().getPackageInfo("com.home.update", 1).versionName;} catch (NameNotFoundException e) {e.printStackTrace();}return versionName;}/** * 向伺服器發起版本檢測請求,伺服器根據提交的版本參數和伺服器上的目前的版本進行比較 如果符合更新要求,返回最新版本號碼和下載URL *  * @param versionName * @return */private boolean testVersion(String version) {boolean isNeedUpdate = false;// 這裡的url具體拼裝方式應根據伺服器需要的格式而定String url = CHECKURL + "?version=" + version;String result = get(url);try {// 這裡的具體解析方式應根據自己伺服器返回的資料而定JSONObject jsonObj = new JSONObject(result);int code = jsonObj.getInt("code");if (code == 0) {JSONObject valueObj = jsonObj.getJSONObject("value");String lastVersion = valueObj.getString("version");downloadUrl = valueObj.getString("url");if (version.equals(lastVersion)) {isNeedUpdate = false;} else {isNeedUpdate = true;}} else {isNeedUpdate = false;}} catch (JSONException e) {e.printStackTrace();}return isNeedUpdate;}/** * 以get方式發送請求 *  * @param url * @return */private String get(String url) {BufferedReader reader = null;StringBuffer sb = null;String result = "";HttpClient client = new DefaultHttpClient();HttpGet request = new HttpGet(url);request.addHeader("Content-Type", " text/json");try {// 發送請求,得到響應HttpResponse response = client.execute(request);// 如果請求成功if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));sb = new StringBuffer();String line = "";while ((line = reader.readLine()) != null) {sb.append(line);}} else if (response.getStatusLine().getStatusCode() == HttpStatus.SC_SERVICE_UNAVAILABLE) {result = "維護或繁忙";}} catch (ClientProtocolException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();} finally {try {if (reader != null) {reader.close();reader = null;}} catch (IOException e) {result = "維護或繁忙";e.printStackTrace();}}if (sb != null) {result = sb.toString();}return result;}/** * 彈出更新提示對話方塊 */private void showNoticeDialog() {AlertDialog.Builder builder = new Builder(context);builder.setTitle("軟體版本更新");builder.setMessage("版本可以更新哦");builder.setPositiveButton("下載", new OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {dialog.dismiss();showDownloadDialog();}});builder.setNegativeButton("以後再說", new OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {dialog.dismiss();}});noticeDialog = builder.create();noticeDialog.show();// 設定彈出對話方塊大小屬性WindowManager.LayoutParams lp = noticeDialog.getWindow().getAttributes();lp.width = 400;lp.height = 500;noticeDialog.getWindow().setAttributes(lp);}/** * 下載進度對話方塊 */private void showDownloadDialog() {AlertDialog.Builder builder = new Builder(context);builder.setTitle("正在下載,請稍後...");final LayoutInflater inflater = LayoutInflater.from(context);View v = inflater.inflate(R.layout.progress, null);mProgress = (ProgressBar) v.findViewById(R.id.progress);builder.setView(v);builder.setNegativeButton("取消", new OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {dialog.dismiss();interceptFlag = true;}});downloadDialog = builder.create();downloadDialog.show();downloadApk();}/** * 啟動線程下載apk */private void downloadApk() {Thread downLoadThread = new Thread(downApkRunnable);downLoadThread.start();}/** * 安裝包下載線程 */private Runnable downApkRunnable = new Runnable() {@Overridepublic void run() {try {URL url = new URL(downloadUrl);HttpURLConnection conn = (HttpURLConnection) url.openConnection();conn.connect();int length = conn.getContentLength();InputStream is = conn.getInputStream();File file = new File(savePath);if (!file.exists()) {file.mkdir();}String apkFile = saveFileName;File ApkFile = new File(apkFile);FileOutputStream fos = new FileOutputStream(ApkFile);int count = 0;byte buf[] = new byte[1024];do {int numread = is.read(buf);count += numread;progress = (int) (((float) count / length) * 100);// 更新進度handler.sendEmptyMessage(DOWN_UPDATE);if (numread <= 0) {// 下載完成通知安裝handler.sendEmptyMessage(DOWN_OVER);break;}fos.write(buf, 0, numread);} while (!interceptFlag);// 點擊取消就停止下載.fos.close();is.close();} catch (MalformedURLException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}}};/** * 安裝APK */private void installApk() {File apkfile = new File(saveFileName);if (!apkfile.exists()) {return;}Intent i = new Intent(Intent.ACTION_VIEW);i.setDataAndType(Uri.parse("file://" + apkfile.toString()),"application/vnd.android.package-archive");context.startActivity(i);}}

如果是在4.0以上版本,不能在主線程訪問網路,部分功能需要作相應調整開啟非同步子線程。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.