安卓软件更新

新建一个UpdateManager.java类,负责软件更新功能模块,代码如下:

[java] view plain copy
    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.     /* 进度条与通知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调用  
    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.                     //更新进度  
    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);//点击取消就停止下载.  
    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.     }  

你可能感兴趣的:(安卓)