安卓升級服務端和客戶端設計

3、服務器端和客戶端設計

  1. 服務器端設計:

設計方法應該有不少,下面介紹個人一種方法:java

  • a.首先在服務器項目下創建一個文件夾來存放APK安裝文件:
  • b.其次在src下創建一個資源文件,apkVersion.properties,屬性定義以下:
[plain]  view plain copy print ?
 
  1. apkVersion=1 存版本號apkSize=550kb 大小apkPath=http://xx8080/srv/apk/Demo.apk 升級文件  
  • c.定義一個servlet來獲取資源中的信息:

定義類:UpdateApkServlet.javaandroid

[java]  view plain copy print ?
 
  1. //獲取資源文件信息  
  2. static {  
  3. Properties ppt = new Properties();  
  4. try {       
  5.     ppt.load(UpdateApkServlet.class .getResourceAsStream("/apkVersion.properties"));       
  6.     apkVersion = ppt.getProperty("apkVersion");       
  7.     apkSize = ppt.getProperty("apkSize");       
  8.     apkPath = ppt.getProperty("apkPath");   
  9. }catch (Exception e) {       
  10.     e.printStackTrace();  
  11. }  
  12. }   

 

獲取資源,而後生成JSON字串返回客戶端處理。 注:當客戶端版本有更新,服務器端只要把APK文件拷貝到APK目錄,而後更新apkVersion.properties文件中的信息就能夠了,切記。服務器

    客戶端設計:
  • 一、 客戶端首先獲取服務器的版本信息(http方式獲取)。
  • 二、 如何獲取本地客戶端的版本信息 以下參考代碼:
[java]  view plain copy print ?
 
  1. /**  
  2.  * 獲得本地應用的版本信息   
  3.  * @return  
  4. */  
  5. private int getAPKVersion(){  
  6.     //APK版本判斷  
  7.     int sdcardVersion = 0;  
  8.     String apkFilePath="sdcard/demo.apk";  //安裝包路徑  
  9.     PackageManager pm = getPackageManager();          
  10.     PackageInfo info = pm.getPackageArchiveInfo(apkFilePath, PackageManager.GET_ACTIVITIES);           
  11. if(info != null){               
  12.     sdcardVersion=info.versionCode;       //獲得版本信息               
  13.     Log.v(TAG, "Version="+sdcardVersion);          
  14. }           
  15. return sdcardVersion;  
  16. }  

 

  • 三、 版本比較,若是版本相同,則不執行更新,不一樣才進行更新操做。 這裏插入客戶端版本設置介紹: 客戶端版本設置在AndroidManifest.xml文件中,裏面有兩個屬性可進行版本信息設置, android:versionCode="1" 版本號 android:versionName="1.1" 版本名稱 這個版本號須要和服務器端對應。
  • 四、 須要的權限設置
[plain]  view plain copy print ?
 
  1. Sdcard訪問權限: uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"  
  2. 訪問網絡權限: uses-permission android:name="android.permission.INTERNET"   
  • 五、 更新安裝 當用戶點擊應用時執行檢查更新。相關代碼參考:

 //彈出框提示網絡

[java]  view plain copy print ?
 
  1. public Handler handler = new Handler() {  
  2. public void handleMessage(Message msg) {  
  3. super.handleMessage(msg);         
  4. Dialog dialog = new AlertDialog.Builder(MainActivity.this).setTitle("系統更新").setMessage("發現新版本,請更新!")   
  5. // 設置內容.setPositiveButton("肯定",// 設置肯定按鈕new DialogInterface.OnClickListener() {  
  6. @Override  
  7. public void onClick(DialogInterface dialog, int which) {  
  8. pBar = new ProgressDialog(MainActivity.this);  
  9. pBar.setTitle("正在下載");pBar.setMessage("請稍候...");  
  10. pBar.setProgressStyle(ProgressDialog.STYLE_SPINNER);downFile(apkPath);}}).setNegativeButton("取消"new DialogInterface.OnClickListener() {  
  11. public void onClick(DialogInterface dialog, int whichButton) {  
  12. // 點擊"取消"按鈕操做}}).create();// 建立  
  13. // 顯示對話框  
  14. dialog.show();  
  15.         }  
  16. };  

 

 //下載app

[java]  view plain copy print ?
 
  1. /**  
  2.  * DOWNLOAD APK FILE BY URL  
  3.  * @param url  
  4. */  
  5. public void downFile(final String url) {  
  6. pBar.show();  
  7. new Thread() {  
  8. public void run() {  
  9. HttpClient client = new DefaultHttpClient();  
  10. // params[0]表明鏈接的  
  11. urlHttpGet get = new HttpGet(url);  
  12. HttpResponse response;  
  13. try {  
  14. response = client.execute(get);  
  15. HttpEntity entity = response.getEntity();  
  16. long length = entity.getContentLength();  
  17. InputStream is = entity.getContent();  
  18. FileOutputStream fileOutputStream = null;  
  19. if (is != null) {  
  20. File file = new File(Environment.getExternalStorageDirectory(),"demo.apk");  
  21. fileOutputStream = new FileOutputStream(file);  
  22. byte[] buf = new byte[1024];  
  23. int ch = -1;  
  24. int count = 0;  
  25. while ((ch = is.read(buf)) != -1) {  
  26. // baos.write(buf, 0, ch);  
  27. fileOutputStream.write(buf, 0, ch);  
  28. count += ch;if (length > 0) {}  
  29. }  
  30. }  
  31. fileOutputStream.flush();  
  32. if (fileOutputStream != null) {  
  33. fileOutputStream.close();  
  34. }  
  35. down();  
  36. catch (ClientProtocolException e) {  
  37. e.printStackTrace();  
  38. catch (IOException e) {  
  39. e.printStackTrace();  
  40. }  
  41. }  
  42. }.start();  
  43. }  
  44. public void down() {  
  45. handler.post(new Runnable()   
  46. {  
  47. public void run() {  
  48. pBar.cancel();  
  49. update();  
  50. }});  
  51. }  

 

 //更新升級ide

[java]  view plain copy print ?
 
  1. public void update() {  
  2. Intent intent = new Intent(Intent.ACTION_VIEW);  
  3. intent.setDataAndType(Uri.fromFile(new File("/sdcard/demo.apk")),"application/vnd.android.package-archive");  
  4. startActivity(intent);  
  5. }  


結束,供參考。post

相關文章
相關標籤/搜索