国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統(tǒng) > Android > 正文

Android應(yīng)用APP自動(dòng)更新功能的代碼實(shí)現(xiàn)

2019-12-12 04:41:04
字體:
供稿:網(wǎng)友

由于Android項(xiàng)目開源所致,市面上出現(xiàn)了N多安卓軟件市場。為了讓我們開發(fā)的軟件有更多的用戶使用,我們需要向N多市場發(fā)布,軟件升級(jí)后,我們也必須到安卓市場上進(jìn)行更新,給我們?cè)黾恿斯ぷ髁俊R虼宋覀冇斜匾o我們的Android應(yīng)用增加自動(dòng)更新的功能。

既然實(shí)現(xiàn)自動(dòng)更新,我們首先必須讓我們的應(yīng)用知道是否存在新版本的軟件,因此我們可以在自己的網(wǎng)站上放置配置文件,存放軟件的版本信息:

<update>   <version>2</version>   <name>baidu_xinwen_1.1.0</name>   <url>http://gdown.baidu.com/data/wisegame/f98d235e39e29031/baiduxinwen.apk</url> </update> 

在這里我使用的是XML文件,方便讀取。由于XML文件內(nèi)容比較少,因此可通過DOM方式進(jìn)行文件的解析:

public class ParseXmlService {   public HashMap<String, String> parseXml(InputStream inStream) throws Exception   {     HashMap<String, String> hashMap = new HashMap<String, String>();          // 實(shí)例化一個(gè)文檔構(gòu)建器工廠     DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();     // 通過文檔構(gòu)建器工廠獲取一個(gè)文檔構(gòu)建器     DocumentBuilder builder = factory.newDocumentBuilder();     // 通過文檔通過文檔構(gòu)建器構(gòu)建一個(gè)文檔實(shí)例     Document document = builder.parse(inStream);     //獲取XML文件根節(jié)點(diǎn)     Element root = document.getDocumentElement();     //獲得所有子節(jié)點(diǎn)     NodeList childNodes = root.getChildNodes();     for (int j = 0; j < childNodes.getLength(); j++)     {       //遍歷子節(jié)點(diǎn)       Node childNode = (Node) childNodes.item(j);       if (childNode.getNodeType() == Node.ELEMENT_NODE)       {         Element childElement = (Element) childNode;         //版本號(hào)         if ("version".equals(childElement.getNodeName()))         {           hashMap.put("version",childElement.getFirstChild().getNodeValue());         }         //軟件名稱         else if (("name".equals(childElement.getNodeName())))         {           hashMap.put("name",childElement.getFirstChild().getNodeValue());         }         //下載地址         else if (("url".equals(childElement.getNodeName())))         {           hashMap.put("url",childElement.getFirstChild().getNodeValue());         }       }     }     return hashMap;   } 

 通過parseXml()方法,我們可以獲取服務(wù)器上應(yīng)用的版本、文件名以及下載地址。緊接著我們就需要獲取到我們手機(jī)上應(yīng)用的版本信息:

 /**  * 獲取軟件版本號(hào)  *  * @param context  * @return  */ private int getVersionCode(Context context) {   int versionCode = 0;   try   {     // 獲取軟件版本號(hào),     versionCode = context.getPackageManager().getPackageInfo("com.szy.update", 0).versionCode;   } catch (NameNotFoundException e)   {     e.printStackTrace();   }   return versionCode; } 

通過該方法我們獲取到的versionCode對(duì)應(yīng)AndroidManifest.xml下android:versionCode。android:versionCode和android:versionName兩個(gè)屬性分別表示版本號(hào),版本名稱。versionCode是整數(shù)型,而versionName是字符串。由于versionName是給用戶看的,不太容易比較大小,升級(jí)檢查時(shí),就可以檢查versionCode。把獲取到的手機(jī)上應(yīng)用版本與服務(wù)器端的版本進(jìn)行比較,應(yīng)用就可以判斷處是否需要更新軟件。

處理流程

處理代碼

package com.szy.update;  import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import java.util.HashMap;  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.widget.ProgressBar; import android.widget.Toast;   public class UpdateManager {   /* 下載中 */   private static final int DOWNLOAD = 1;   /* 下載結(jié)束 */   private static final int DOWNLOAD_FINISH = 2;   /* 保存解析的XML信息 */   HashMap<String, String> mHashMap;   /* 下載保存路徑 */   private String mSavePath;   /* 記錄進(jìn)度條數(shù)量 */   private int progress;   /* 是否取消更新 */   private boolean cancelUpdate = false;    private Context mContext;   /* 更新進(jìn)度條 */   private ProgressBar mProgress;   private Dialog mDownloadDialog;    private Handler mHandler = new Handler()   {     public void handleMessage(Message msg)     {       switch (msg.what)       {       // 正在下載       case DOWNLOAD:         // 設(shè)置進(jìn)度條位置         mProgress.setProgress(progress);         break;       case DOWNLOAD_FINISH:         // 安裝文件         installApk();         break;       default:         break;       }     };   };    public UpdateManager(Context context)   {     this.mContext = context;   }    /**    * 檢測軟件更新    */   public void checkUpdate()   {     if (isUpdate())     {       // 顯示提示對(duì)話框       showNoticeDialog();     } else     {       Toast.makeText(mContext, R.string.soft_update_no, Toast.LENGTH_LONG).show();     }   }    /**    * 檢查軟件是否有更新版本    *    * @return    */   private boolean isUpdate()   {     // 獲取當(dāng)前軟件版本     int versionCode = getVersionCode(mContext);     // 把version.xml放到網(wǎng)絡(luò)上,然后獲取文件信息     InputStream inStream = ParseXmlService.class.getClassLoader().getResourceAsStream("version.xml");     // 解析XML文件。 由于XML文件比較小,因此使用DOM方式進(jìn)行解析     ParseXmlService service = new ParseXmlService();     try     {       mHashMap = service.parseXml(inStream);     } catch (Exception e)     {       e.printStackTrace();     }     if (null != mHashMap)     {       int serviceCode = Integer.valueOf(mHashMap.get("version"));       // 版本判斷       if (serviceCode > versionCode)       {         return true;       }     }     return false;   }  /**  * 獲取軟件版本號(hào)  *  * @param context  * @return  */ private int getVersionCode(Context context) {   int versionCode = 0;   try   {     // 獲取軟件版本號(hào),對(duì)應(yīng)AndroidManifest.xml下android:versionCode     versionCode = context.getPackageManager().getPackageInfo("com.szy.update", 0).versionCode;   } catch (NameNotFoundException e)   {     e.printStackTrace();   }   return versionCode; }    /**    * 顯示軟件更新對(duì)話框    */   private void showNoticeDialog()   {     // 構(gòu)造對(duì)話框     AlertDialog.Builder builder = new Builder(mContext);     builder.setTitle(R.string.soft_update_title);     builder.setMessage(R.string.soft_update_info);     // 更新     builder.setPositiveButton(R.string.soft_update_updatebtn, new OnClickListener()     {       @Override       public void onClick(DialogInterface dialog, int which)       {         dialog.dismiss();         // 顯示下載對(duì)話框         showDownloadDialog();       }     });     // 稍后更新     builder.setNegativeButton(R.string.soft_update_later, new OnClickListener()     {       @Override       public void onClick(DialogInterface dialog, int which)       {         dialog.dismiss();       }     });     Dialog noticeDialog = builder.create();     noticeDialog.show();   }    /**    * 顯示軟件下載對(duì)話框    */   private void showDownloadDialog()   {     // 構(gòu)造軟件下載對(duì)話框     AlertDialog.Builder builder = new Builder(mContext);     builder.setTitle(R.string.soft_updating);     // 給下載對(duì)話框增加進(jìn)度條     final LayoutInflater inflater = LayoutInflater.from(mContext);     View v = inflater.inflate(R.layout.softupdate_progress, null);     mProgress = (ProgressBar) v.findViewById(R.id.update_progress);     builder.setView(v);     // 取消更新     builder.setNegativeButton(R.string.soft_update_cancel, new OnClickListener()     {       @Override       public void onClick(DialogInterface dialog, int which)       {         dialog.dismiss();         // 設(shè)置取消狀態(tài)         cancelUpdate = true;       }     });     mDownloadDialog = builder.create();     mDownloadDialog.show();     // 現(xiàn)在文件     downloadApk();   }    /**    * 下載apk文件    */   private void downloadApk()   {     // 啟動(dòng)新線程下載軟件     new downloadApkThread().start();   }    /**    * 下載文件線程    *    * @author coolszy    */   private class downloadApkThread extends Thread   {     @Override     public void run()     {       try       {         // 判斷SD卡是否存在,并且是否具有讀寫權(quán)限         if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED))         {           // 獲得存儲(chǔ)卡的路徑           String sdpath = Environment.getExternalStorageDirectory() + "/";           mSavePath = sdpath + "download";           URL url = new URL(mHashMap.get("url"));           // 創(chuàng)建連接           HttpURLConnection conn = (HttpURLConnection) url.openConnection();           conn.connect();           // 獲取文件大小           int length = conn.getContentLength();           // 創(chuàng)建輸入流           InputStream is = conn.getInputStream();            File file = new File(mSavePath);           // 判斷文件目錄是否存在           if (!file.exists())           {             file.mkdir();           }           File apkFile = new File(mSavePath, mHashMap.get("name"));           FileOutputStream fos = new FileOutputStream(apkFile);           int count = 0;           // 緩存           byte buf[] = new byte[1024];           // 寫入到文件中           do           {             int numread = is.read(buf);             count += numread;             // 計(jì)算進(jìn)度條位置             progress = (int) (((float) count / length) * 100);             // 更新進(jìn)度             mHandler.sendEmptyMessage(DOWNLOAD);             if (numread <= 0)             {               // 下載完成               mHandler.sendEmptyMessage(DOWNLOAD_FINISH);               break;             }             // 寫入文件             fos.write(buf, 0, numread);           } while (!cancelUpdate);// 點(diǎn)擊取消就停止下載.           fos.close();           is.close();         }       } catch (MalformedURLException e)       {         e.printStackTrace();       } catch (IOException e)       {         e.printStackTrace();       }       // 取消下載對(duì)話框顯示       mDownloadDialog.dismiss();     }   };    /**    * 安裝APK文件    */   private void installApk()   {     File apkfile = new File(mSavePath, mHashMap.get("name"));     if (!apkfile.exists())     {       return;     }     // 通過Intent安裝APK文件     Intent i = new Intent(Intent.ACTION_VIEW);     i.setDataAndType(Uri.parse("file://" + apkfile.toString()), "application/vnd.android.package-archive");     mContext.startActivity(i);   } } 

效果圖

檢查模擬器SDCARD是否存在下載文件:
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持武林網(wǎng)。

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 济南市| 泽州县| 武鸣县| 康马县| 信丰县| 宜黄县| 南城县| 集贤县| 阿尔山市| 广水市| 巴彦淖尔市| 当阳市| 北辰区| 沛县| 茂名市| 尼勒克县| 嵊州市| 河南省| 开鲁县| 饶阳县| 昌乐县| 富平县| 陕西省| 册亨县| 夹江县| 永定县| 鄂伦春自治旗| 东光县| 三台县| 南涧| 婺源县| 盱眙县| 肃北| 潞城市| 松阳县| 红桥区| 桂平市| 莆田市| 宜兰市| 嵊州市| 莱西市|