亚洲av成人无遮挡网站在线观看,少妇性bbb搡bbb爽爽爽,亚洲av日韩精品久久久久久,兔费看少妇性l交大片免费,无码少妇一区二区三区

  免費(fèi)注冊(cè) 查看新帖 |

Chinaunix

  平臺(tái) 論壇 博客 文庫
最近訪問板塊 發(fā)新帖
查看: 1423 | 回復(fù): 0
打印 上一主題 下一主題

[Android] Android應(yīng)用自動(dòng)更新功能的實(shí)現(xiàn)。! [復(fù)制鏈接]

論壇徽章:
1
操作系統(tǒng)版塊每日發(fā)帖之星
日期:2015-06-05 22:20:00
跳轉(zhuǎn)到指定樓層
1 [收藏(0)] [報(bào)告]
發(fā)表于 2015-06-04 09:23 |只看該作者 |倒序?yàn)g覽
一個(gè)好的應(yīng)用軟件都是需要好的維護(hù),從初出版本到最后精品,這個(gè)過程需要版本不停的更新,那么如何讓用戶第一時(shí)間獲取最新的應(yīng)用安裝包呢?那么就要求我們從第一個(gè)版本就要實(shí)現(xiàn)升級(jí)模塊這一功能。

自動(dòng)更新功能的實(shí)現(xiàn)原理,就是我們事先和后臺(tái)協(xié)商好一個(gè)接口,我們?cè)趹?yīng)用的主Activity里,去訪問這個(gè)接口,如果需要更新,后臺(tái)會(huì)返回一些數(shù)據(jù)(比如,提示語;最新版本的url等)。然后我們給出提示框,用戶點(diǎn)擊開始下載,下載完成開始覆蓋安裝程序,這樣用戶的應(yīng)用就保持最新的拉。

為了讓大家容易理解,我像往常一樣準(zhǔn)備一個(gè)小例子,這里為了方便我就省去了和后臺(tái)交互部分了。步驟分別如下:
第一步:新建一個(gè)Android工程命名為:UpdateDemo.代碼結(jié)構(gòu)如下圖所示:

第二步:新建一個(gè)UpdateManager.java類,負(fù)責(zé)軟件更新功能模塊,代碼如下:
  1. package com.tutor.update;  
  2.   
  3. import java.io.File;  
  4. import java.io.FileOutputStream;  
  5. import java.io.IOException;  
  6. import java.io.InputStream;  
  7. import java.net.HttpURLConnection;  
  8. import java.net.MalformedURLException;  
  9. import java.net.URL;  
  10.   
  11.   
  12. import android.app.AlertDialog;  
  13. import android.app.Dialog;  
  14. import android.app.AlertDialog.Builder;  
  15. import android.content.Context;  
  16. import android.content.DialogInterface;  
  17. import android.content.Intent;  
  18. import android.content.DialogInterface.OnClickListener;  
  19. import android.net.Uri;  
  20. import android.os.Handler;  
  21. import android.os.Message;  
  22. import android.view.LayoutInflater;  
  23. import android.view.View;  
  24. import android.widget.ProgressBar;  
  25.   
  26. public class UpdateManager {  
  27.   
  28.     private Context mContext;  
  29.       
  30.     //提示語  
  31.     private String updateMsg = "有最新的軟件包哦,親快下載吧~";  
  32.       
  33.     //返回的安裝包url  
  34.     private String apkUrl = "http://softfile.3g.qq.com:8080/msoft/179/24659/43549/qq_hd_mini_1.4.apk";  
  35.       
  36.       
  37.     private Dialog noticeDialog;  
  38.       
  39.     private Dialog downloadDialog;  
  40.      /* 下載包安裝路徑 */  
  41.     private static final String savePath = "/sdcard/updatedemo/";  
  42.       
  43.     private static final String saveFileName = savePath + "UpdateDemoRelease.apk";  
  44.   
  45.     /* 進(jìn)度條與通知ui刷新的handler和msg常量 */  
  46.     private ProgressBar mProgress;  
  47.   
  48.       
  49.     private static final int DOWN_UPDATE = 1;  
  50.       
  51.     private static final int DOWN_OVER = 2;  
  52.       
  53.     private int progress;  
  54.       
  55.     private Thread downLoadThread;  
  56.       
  57.     private boolean interceptFlag = false;  
  58.       
  59.     private Handler mHandler = new Handler(){  
  60.         public void handleMessage(Message msg) {  
  61.             switch (msg.what) {  
  62.             case DOWN_UPDATE:  
  63.                 mProgress.setProgress(progress);  
  64.                 break;  
  65.             case DOWN_OVER:  
  66.                   
  67.                 installApk();  
  68.                 break;  
  69.             default:  
  70.                 break;  
  71.             }  
  72.         };  
  73.     };  
  74.       
  75.     public UpdateManager(Context context) {  
  76.         this.mContext = context;  
  77.     }  
  78.       
  79.     //外部接口讓主Activity調(diào)用  
  80.     public void checkUpdateInfo(){  
  81.         showNoticeDialog();  
  82.     }  
  83.       
  84.       
  85.     private void showNoticeDialog(){  
  86.         AlertDialog.Builder builder = new Builder(mContext);  
  87.         builder.setTitle("軟件版本更新");  
  88.         builder.setMessage(updateMsg);  
  89.         builder.setPositiveButton("下載", new OnClickListener() {           
  90.             @Override  
  91.             public void onClick(DialogInterface dialog, int which) {  
  92.                 dialog.dismiss();  
  93.                 showDownloadDialog();            
  94.             }  
  95.         });  
  96.         builder.setNegativeButton("以后再說", new OnClickListener() {            
  97.             @Override  
  98.             public void onClick(DialogInterface dialog, int which) {  
  99.                 dialog.dismiss();                 
  100.             }  
  101.         });  
  102.         noticeDialog = builder.create();  
  103.         noticeDialog.show();  
  104.     }  
  105.       
  106.     private void showDownloadDialog(){  
  107.         AlertDialog.Builder builder = new Builder(mContext);  
  108.         builder.setTitle("軟件版本更新");  
  109.          
  110.         final LayoutInflater inflater = LayoutInflater.from(mContext);  
  111.         View v = inflater.inflate(R.layout.progress, null);  
  112.         mProgress = (ProgressBar)v.findViewById(R.id.progress);  
  113.          
  114.         builder.setView(v);  
  115.         builder.setNegativeButton("取消", new OnClickListener() {   
  116.             @Override  
  117.             public void onClick(DialogInterface dialog, int which) {  
  118.                 dialog.dismiss();  
  119.                 interceptFlag = true;  
  120.             }  
  121.         });  
  122.         downloadDialog = builder.create();  
  123.         downloadDialog.show();  
  124.          
  125.         downloadApk();  
  126.     }  
  127.       
  128.     private Runnable mdownApkRunnable = new Runnable() {      
  129.         @Override  
  130.         public void run() {  
  131.             try {  
  132.                 URL url = new URL(apkUrl);  
  133.               
  134.                 HttpURLConnection conn = (HttpURLConnection)url.openConnection();  
  135.                 conn.connect();  
  136.                 int length = conn.getContentLength();  
  137.                 InputStream is = conn.getInputStream();  
  138.                   
  139.                 File file = new File(savePath);  
  140.                 if(!file.exists()){  
  141.                     file.mkdir();  
  142.                 }  
  143.                 String apkFile = saveFileName;  
  144.                 File ApkFile = new File(apkFile);  
  145.                 FileOutputStream fos = new FileOutputStream(ApkFile);  
  146.                   
  147.                 int count = 0;  
  148.                 byte buf[] = new byte[1024];  
  149.                   
  150.                 do{                  
  151.                     int numread = is.read(buf);  
  152.                     count += numread;  
  153.                     progress =(int)(((float)count / length) * 100);  
  154.                     //更新進(jìn)度  
  155.                     mHandler.sendEmptyMessage(DOWN_UPDATE);  
  156.                     if(numread <= 0){      
  157.                         //下載完成通知安裝  
  158.                         mHandler.sendEmptyMessage(DOWN_OVER);  
  159.                         break;  
  160.                     }  
  161.                     fos.write(buf,0,numread);  
  162.                 }while(!interceptFlag);//點(diǎn)擊取消就停止下載.  
  163.                   
  164.                 fos.close();  
  165.                 is.close();  
  166.             } catch (MalformedURLException e) {  
  167.                 e.printStackTrace();  
  168.             } catch(IOException e){  
  169.                 e.printStackTrace();  
  170.             }  
  171.               
  172.         }  
  173.     };  
  174.       
  175.      /**
  176.      * 下載apk
  177.      * @param url
  178.      */  
  179.       
  180.     private void downloadApk(){  
  181.         downLoadThread = new Thread(mdownApkRunnable);  
  182.         downLoadThread.start();  
  183.     }  
  184.      /**
  185.      * 安裝apk
  186.      * @param url
  187.      */  
  188.     private void installApk(){  
  189.         File apkfile = new File(saveFileName);  
  190.         if (!apkfile.exists()) {  
  191.             return;  
  192.         }      
  193.         Intent i = new Intent(Intent.ACTION_VIEW);  
  194.         i.setDataAndType(Uri.parse("file://" + apkfile.toString()), "application/vnd.android.package-archive");   
  195.         mContext.startActivity(i);  
  196.       
  197.     }  
  198. }  
復(fù)制代碼
第三步:在MainActivity.java也就是主Activity調(diào)用,代碼如下:
  1. package com.tutor.update;  
  2.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5.   
  6. public class MainAcitivity extends Activity {  
  7.       
  8.   
  9.     private UpdateManager mUpdateManager;  
  10.     @Override  
  11.     public void onCreate(Bundle savedInstanceState) {  
  12.         super.onCreate(savedInstanceState);  
  13.         setContentView(R.layout.main);  
  14.          
  15.         //這里來檢測(cè)版本是否需要更新  
  16.         mUpdateManager = new UpdateManager(this);  
  17.         mUpdateManager.checkUpdateInfo();  
  18.     }      
  19. }  
復(fù)制代碼
第四步:添加程序所用的資源與權(quán)限:
下載的時(shí)候用到了ProgressBar,所以事先寫了一個(gè)progress.xml布局文件,代碼如下:
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout  
  3.   xmlns:android="http://schemas.android.com/apk/res/android"  
  4.   android:layout_width="fill_parent"  
  5.   android:layout_height="wrap_content">  
  6.    
  7.   <ProgressBar  
  8.     android:id="@+id/progress"  
  9.     android:layout_width="fill_parent"  
  10.     android:layout_height="wrap_content"  
  11.     style="?android:attr/progressBarStyleHorizontal"  
  12.   />  
  13. </LinearLayout>  
復(fù)制代碼
下載的時(shí)候用到了網(wǎng)絡(luò)部分,所以要在AndroidManifest.xml中添加網(wǎng)絡(luò)權(quán)限,代碼如下:
  1. <uses-permission android:name="android.permission.INTERNET" />
復(fù)制代碼
第五步:運(yùn)行查看效果如下:
您需要登錄后才可以回帖 登錄 | 注冊(cè)

本版積分規(guī)則 發(fā)表回復(fù)

  

北京盛拓優(yōu)訊信息技術(shù)有限公司. 版權(quán)所有 京ICP備16024965號(hào)-6 北京市公安局海淀分局網(wǎng)監(jiān)中心備案編號(hào):11010802020122 niuxiaotong@pcpop.com 17352615567
未成年舉報(bào)專區(qū)
中國(guó)互聯(lián)網(wǎng)協(xié)會(huì)會(huì)員  聯(lián)系我們:huangweiwei@itpub.net
感謝所有關(guān)心和支持過ChinaUnix的朋友們 轉(zhuǎn)載本站內(nèi)容請(qǐng)注明原作者名及出處

清除 Cookies - ChinaUnix - Archiver - WAP - TOP