Android程序的版本檢測與更新_第1頁
Android程序的版本檢測與更新_第2頁
Android程序的版本檢測與更新_第3頁
Android程序的版本檢測與更新_第4頁
Android程序的版本檢測與更新_第5頁
已閱讀5頁,還剩12頁未讀 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)

文檔簡介

1、Android程序的版本檢測與更新 做個網(wǎng)站的安卓客戶端,用戶安裝到自己手機上,如果我出了新版本怎么辦呢?要有版本更新功能。本來版本檢測最好可以自動進行。但如果每次開啟程序,都要先檢測一輪,是一種浪費,畢竟版本更新是小概率的事情。或許可以程序開啟的時候,判斷一下時間,單日就檢測,雙日就不檢測,或者隨機什么的,降低一下檢測的頻率?我采取的做法是將檢測功能做到了菜單上,用戶有需要,就手動打開自己檢測一下。反正我們這個是網(wǎng)站客戶端,有版本更新,在網(wǎng)站上發(fā)個通告就行了。版本檢測與更新有以下幾個關(guān)鍵步驟:1、檢測有無新版本2、下載新版本3、安裝替換新版本我處理的方案是1、在assets文件夾新增一個文件

2、:ver.cfg,記錄版本信息,純文本格式,內(nèi)容只有一句話:Version=1.0這個會隨安裝包裝到用戶的手機上然后在網(wǎng)站里面,設(shè)置一XML文件ver_apk.xml,內(nèi)容也只有這么一點:<?xml version="1.0" encoding="utf-8" ?><string>1.0</string>檢測的時候,就先訪問網(wǎng)站的這個XML,得到最新版本號,然后與手機上的ver.cfg文件里記錄的進行比對,不同的話就可以認為存在新版本,提示進行更新。2、下載的話就是直接下載的,我還不知道怎么弄斷點續(xù)傳3、安裝替換,關(guān)鍵

3、在于簽名。就是每個版本的簽名要保持一致。否則新的無法替換舊的,提示安裝未完成。- 天氣太冷,咯咯咯 -這個功能做在菜單上,觸發(fā)代碼如下:/=/ 菜單/= 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網(wǎng)頁,獲取版本號 getVersionxml(VersionUri); private void compareVer() load(); /當有最新版本的時候 if(PastVersion != null && !PastVersion.equals(NowVersion) Dialog dialog = n

8、ew AlertDialog.Builder(mContext).setTitle("系統(tǒng)更新") .setMessage(String.format("發(fā)現(xiàn)新版本%s,目前版本為%s,請更新!",NowVersion,PastVersion)/ 設(shè)置內(nèi)容 / 設(shè)置確定按鈕 .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();/ 創(chuàng)建 / 顯示對話框 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); /*建立聯(lián)機*/ URLConnection conn = myURL.openConnection(); conn.connect(); /*InputStream 下載文件*/ InputStream is = conn.getInputStream(); if (is = null) Log.d("tag","error"); throw new RuntimeException(

14、"沒有讀取到文件內(nèi)容"); /*建立臨時文件*/ 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); /* 調(diào)用getMIMEType()來取得MimeType */ String type = getMIMEType(f); /* 設(shè)定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); /讀取文件內(nèi)容 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. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
  • 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論