




版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
1、Android程序的版本檢測與更新 做個網站的安卓客戶端,用戶安裝到自己手機上,如果我出了新版本怎么辦呢?要有版本更新功能。本來版本檢測最好可以自動進行。但如果每次開啟程序,都要先檢測一輪,是一種浪費,畢竟版本更新是小概率的事情。或許可以程序開啟的時候,判斷一下時間,單日就檢測,雙日就不檢測,或者隨機什么的,降低一下檢測的頻率?我采取的做法是將檢測功能做到了菜單上,用戶有需要,就手動打開自己檢測一下。反正我們這個是網站客戶端,有版本更新,在網站上發個通告就行了。版本檢測與更新有以下幾個關鍵步驟:1、檢測有無新版本2、下載新版本3、安裝替換新版本我處理的方案是1、在assets文件夾新增一個文件
2、:ver.cfg,記錄版本信息,純文本格式,內容只有一句話:Version=1.0這個會隨安裝包裝到用戶的手機上然后在網站里面,設置一XML文件ver_apk.xml,內容也只有這么一點:<?xml version="1.0" encoding="utf-8" ?><string>1.0</string>檢測的時候,就先訪問網站的這個XML,得到最新版本號,然后與手機上的ver.cfg文件里記錄的進行比對,不同的話就可以認為存在新版本,提示進行更新。2、下載的話就是直接下載的,我還不知道怎么弄斷點續傳3、安裝替換,關鍵
3、在于簽名。就是每個版本的簽名要保持一致。否則新的無法替換舊的,提示安裝未完成。- 天氣太冷,咯咯咯 -這個功能做在菜單上,觸發代碼如下:/=/ 菜單/= Overridepublic boolean onCreateOptionsMenu(Menu menu) menu.add(Menu.NONE, Menu.FIRST + 1, 5, "檢測更新").setIcon( return true;Overridepublic boolean onOptionsItemSelected(MenuItem item) switch (item.getItemId() case M
4、enu.FIRST + 1: Toast.makeText(this, "正在檢測版本", Toast.LENGTH_LONG).show(); UpdateVer uv = new UpdateVer(urlApk,urlVer,MainActivity.this); uv.checkVer(); break; case Menu.FIRST + 2: confirmExit(); break; return false;檢測更新因為代碼比較多,寫成一個類進行封裝UpdateVer.javapackage android.*;public class UpdateVer
5、extends Activity private static final String TAG = "DOWNLOADAPK" private String PastVersion; private String NowVersion; public ProgressDialog pBar; private String currentFilePath = "" private String fileEx="" private String fileNa="" private String strURL=&quo
6、t;" private String VersionUri ="" private Context mContext; private final String fileVer = "ver.cfg" public UpdateVer(String urlapk,String urlver,final Context context) SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss"); String ver = "?ver=" + d
7、f.format(new Date();/主要是避開手機的緩存 strURL = urlapk + ver; VersionUri = urlver + ver; mContext = context; public void checkVer() / 解析Version網頁,獲取版本號 getVersionxml(VersionUri); private void compareVer() load(); /當有最新版本的時候 if(PastVersion != null && !PastVersion.equals(NowVersion) Dialog dialog = n
8、ew AlertDialog.Builder(mContext).setTitle("系統更新") .setMessage(String.format("發現新版本%s,目前版本為%s,請更新!",NowVersion,PastVersion)/ 設置內容 / 設置確定按鈕 .setPositiveButton("確定" ,new DialogInterface.OnClickListener() Override public void onClick(DialogInterface dialog, int which) pBar
9、= new ProgressDialog(mContext); pBar.setTitle("正在下載"); pBar.setMessage("請稍候."); pBar.setProgressStyle(ProgressDialog.STYLE_SPINNER); fileEx = strURL.substring(strURL.lastIndexOf(".") + 1,strURL.length().toLowerCase(); fileEx = fileEx.substring(0,fileEx.lastIndexOf("
10、;?"); fileNa = strURL.substring(strURL.lastIndexOf("/") + 1,strURL.lastIndexOf("."); getFile(strURL); ).setNegativeButton("取消", new DialogInterface.OnClickListener() Override public void onClick(DialogInterface dialog, int whichButton) / 點擊"取消"按鈕之后退出程序 ).
11、create();/ 創建 / 顯示對話框 dialog.show(); else Toast.makeText(mContext, String.format("當前為最新版本%s",PastVersion), Toast.LENGTH_LONG).show(); private void getFile(final String strPath) pBar.show(); try if (strPath.equals(currentFilePath) ) getDataSource(strPath); currentFilePath = strPath; Runnabl
12、e r = new Runnable() Override public void run() try getDataSource(strPath); catch (Exception e) Log.e(TAG, e.getMessage(), e); ; new Thread(r).start(); catch(Exception e) e.printStackTrace(); /*取得遠程文件*/ private void getDataSource(String strPath) throws Exception if (!URLUtil.isNetworkUrl(strPath) Lo
13、g.d("Tag","error"); else /*取得URL*/ URL myURL = new URL(strPath); /*建立聯機*/ URLConnection conn = myURL.openConnection(); conn.connect(); /*InputStream 下載文件*/ InputStream is = conn.getInputStream(); if (is = null) Log.d("tag","error"); throw new RuntimeException(
14、"沒有讀取到文件內容"); /*建立臨時文件*/ File myTempFile = File.createTempFile(fileNa, "." + fileEx); myTempFile.getAbsolutePath(); /*將文件寫入臨時盤*/ FileOutputStream fos = new FileOutputStream(myTempFile); byte buf = new byte128; do int numread = is.read(buf); if (numread <= 0) break; fos.write(b
15、uf, 0, numread); while (true); /*打開文件進行安裝*/ openFile(myTempFile); try is.close(); catch (Exception ex) Log.d("Tag","error"); Log.e(TAG, "error: " + ex.getMessage(), ex); /* 在手機上打開文件 */ private void openFile(File f) pBar.cancel(); Intent intent = new Intent(); intent.add
16、Flags(Intent.FLAG_ACTIVITY_NEW_TASK); /* 調用getMIMEType()來取得MimeType */ String type = getMIMEType(f); /* 設定intent的file與MimeType */ intent.setDataAndType(Uri.fromFile(f),type); mContext.startActivity(intent); /* 判斷文件MimeType的method */ private String getMIMEType(File f) String type = "" Strin
17、g fName = f.getName(); /* 取得擴展名 */ String end = fName.substring(fName.lastIndexOf(".")+1,fName.length().toLowerCase(); /* 按擴展名的類型決定MimeType */ if(end.equals("m4a") | end.equals("mp3") | end.equals("mid") | end.equals("xmf") | end.equals("ogg&quo
18、t;) | end.equals("wav") type = "audio" else if(end.equals("3gp") | end.equals("mp4") type = "video" else if(end.equals("jpg") | end.equals("gif") | end.equals("png") | end.equals("jpeg") | end.equals("bmp&
19、quot;) type = "image" else if(end.equals("apk") else type = "*" /*如果無法直接打開,就跳出軟件清單給使用者選擇 */ if(!end.equals("apk") type += "/*" return type; private void getVersionxml(String resourceUrl) GetVer gv = new GetVer(); gv.execute(resourceUrl); private bool
20、ean load() Properties properties = new Properties(); try InputStream stream = mContext.getAssets().open(fileVer); /FileInputStream stream = mContext.openFileInput(fileVer); /讀取文件內容 properties.load(stream); catch (FileNotFoundException e) return false; catch(IOException e) return false; catch(Exception e) return false; PastVersion = String.valueOf(properti
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經權益所有人同意不得將文件中的內容挪作商業或盈利用途。
- 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 張掖市中國消防救援政府專職消防員招聘筆試真題2024
- 舊車庫存管理優化路徑-洞察及研究
- 行業自律機制創新-第4篇-洞察及研究
- 無人機遙感監測設備在農業氣象監測中的應用考核試卷
- 振動數據分析與可視化技術考核試卷
- 多元智能開發考核試卷
- 研究報告-2026-2024年西服定制行業市場深度調研及發展戰略研究咨詢(目錄)
- 四川商務職業學院《數字攝像與表現》2023-2024學年第一學期期末試卷
- 河北工業職業技術大學《視頻節目包裝》2023-2024學年第一學期期末試卷
- 普洱學院《農業技術推廣學》2023-2024學年第一學期期末試卷
- 2022年輔警招考公共基礎知識練習題(附解析)
- 微波技術與天線 第5章
- 衛生監督協管試題庫
- 鋼灰庫鋼制灰庫技術協議
- 變速箱廠總平面布置設計課程設計說明書
- 材料送檢計劃表(共7頁)
- 信息安全評估表(共23頁)
- 治安案件詢問筆錄(適用口頭傳喚)
- 《髓質海綿腎》課件.ppt
- 共青團委員會選票模板
- 系統是成功的秘密
評論
0/150
提交評論