Android apps應用檢查更新代碼

JAVA代碼:
UpdateManager.java

[java] 
<pre name="code" class="java">package com.iteye.update; 
 
import java.io.File; 
import java.io.FileOutputStream; 
import java.io.IOException; 
import java.io.InputStream; 
import java.net.HttpURLConnection; 
import java.net.MalformedURLException; 
import java.net.URL; 
import java.util.HashMap; 
 
import android.app.AlertDialog; 
import android.app.AlertDialog.Builder; 
import android.app.Dialog; 
import android.content.Context; 
import android.content.DialogInterface; 
import android.content.DialogInterface.OnClickListener; 
import android.content.Intent; 
import android.content.pm.PackageManager.NameNotFoundException; 
import android.net.Uri; 
import android.os.Environment; 
import android.os.Handler; 
import android.os.Message; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.widget.ProgressBar; 
import android.widget.Toast; 
 
import com.iteye.androidtoast.R; 
 
public class UpdateManager 

    /* 下載中 */ 
    private static final int DOWNLOAD = 1; 
    /* 下載結束 */ 
    private static final int DOWNLOAD_FINISH = 2; 
    /* 保存解析的XML信息 */ 
    HashMap<String, String> mHashMap; 
    /* 下載保存路徑 */ 
    private String mSavePath; 
    /* 記錄進度條數量 */ 
    private int progress; 
    /* 是否取消更新 */ 
    private boolean cancelUpdate = false; 
 
    private Context mContext; 
    /* 更新進度條 */ 
    private ProgressBar mProgress; 
    private Dialog mDownloadDialog; 
 
    private Handler mHandler = new Handler() 
    { 
        public void handleMessage(Message msg) 
        { 
            switch (msg.what) 
            { 
            // 正在下載 
            case DOWNLOAD: 
                // 設置進度條位置 
                mProgress.setProgress(progress); 
                break; 
            case DOWNLOAD_FINISH: 
                // 安裝文件 
                installApk(); 
                break; 
            default: 
                break; 
            } 
        }; 
    }; 
 
    public UpdateManager(Context context) 
    { 
        this.mContext = context; 
    } 
 
    /**
     * 檢測軟件更新
     */ 
    public void checkUpdate() 
    { 
        if (isUpdate()) 
        { 
            // 顯示提示對話框 
            showNoticeDialog(); 
        } else 
        { 
            Toast.makeText(mContext, R.string.soft_update_no, Toast.LENGTH_LONG).show(); 
        } 
    } 
 
    /**
     * 檢查軟件是否有更新版本
     * 
     * @return
     */ 
    private boolean isUpdate() 
    { 
        // 獲取當前軟件版本 
        int versionCode = getVersionCode(mContext); 
        // 把version.xml放到網絡上,然後獲取文件信息 
        //InputStream inStream = ParseXmlService.class.getClassLoader().getResourceAsStream("version.xml"); 
        // 解析XML文件。 由於XML文件比較小,因此使用DOM方式進行解析 
        ParseXmlService service = new ParseXmlService(); 
        try 
        { 
            String path = "http://192.168.1.146:8080/picweb/xml/version.xml";   //地址是服務器上version.xml鏈接地址 
            URL url = new URL(path); 
            HttpURLConnection conn = (HttpURLConnection)url.openConnection(); 
            conn.setReadTimeout(5*1000); 
            conn.setRequestMethod("GET"); 
            InputStream inStream = conn.getInputStream(); 
            mHashMap = service.parseXml(inStream); 
        } catch (Exception e) 
        { 
            e.printStackTrace(); 
        } 
        if (null != mHashMap) 
        { 
            int serviceCode = Integer.valueOf(mHashMap.get("version")); 
            // 版本判斷 
            if (serviceCode > versionCode) 
            { 
                return true; 
            } 
        } 
        return false; 
    } 
 
    /**
     * 獲取軟件版本號
     * 
     * @param context
     * @return
     */ 
    private int getVersionCode(Context context) 
    { 
        int versionCode = 0; 
        try 
        { 
            // 獲取軟件版本號,對應AndroidManifest.xml下android:versionCode 
            versionCode = context.getPackageManager().getPackageInfo("com.iteye.androidtoast", 0).versionCode; 
        } catch (NameNotFoundException e) 
        { 
            e.printStackTrace(); 
        } 
        return versionCode; 
    } 
 
    /**
     * 顯示軟件更新對話框
     */ 
    private void showNoticeDialog() 
    { 
        // 構造對話框 
        AlertDialog.Builder builder = new Builder(mContext); 
        builder.setTitle(R.string.soft_update_title); 
        builder.setMessage(R.string.soft_update_info); 
        // 更新 
        builder.setPositiveButton(R.string.soft_update_updatebtn, new OnClickListener() 
        { 
            @Override 
            public void onClick(DialogInterface dialog, int which) 
            { 
                dialog.dismiss(); 
                // 顯示下載對話框 
                showDownloadDialog(); 
            } 
        }); 
        // 稍後更新 
        builder.setNegativeButton(R.string.soft_update_later, new OnClickListener() 
        { 
            @Override 
            public void onClick(DialogInterface dialog, int which) 
            { 
                dialog.dismiss(); 
            } 
        }); 
        Dialog noticeDialog = builder.create(); 
        noticeDialog.show(); 
    } 
 
    /**
     * 顯示軟件下載對話框
     */ 
    private void showDownloadDialog() 
    { 
        // 構造軟件下載對話框 
        AlertDialog.Builder builder = new Builder(mContext); 
        builder.setTitle(R.string.soft_updating); 
        // 給下載對話框增加進度條 
        final LayoutInflater inflater = LayoutInflater.from(mContext); 
        View v = inflater.inflate(R.layout.softupdate_progress, null); 
        mProgress = (ProgressBar) v.findViewById(R.id.update_progress); 
        builder.setView(v); 
        // 取消更新 
        builder.setNegativeButton(R.string.soft_update_cancel, new OnClickListener() 
        { 
            @Override 
            public void onClick(DialogInterface dialog, int which) 
            { 
                dialog.dismiss(); 
                // 設置取消狀態 
                cancelUpdate = true; 
            } 
        }); 
        mDownloadDialog = builder.create(); 
        mDownloadDialog.show(); 
        // 現在文件 
        downloadApk(); 
    } 
 
    /**
     * 下載apk文件
     */ 
    private void downloadApk() 
    { 
        // 啟動新線程下載軟件 
        new downloadApkThread().start(); 
    } 
 
    /**
     * 下載文件線程
     */ 
    private class downloadApkThread extends Thread 
    { 
        @Override 
        public void run() 
        { 
            try 
            { 
                // 判斷SD卡是否存在,並且是否具有讀寫權限 
                if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) 
                { 
                    // 獲得存儲卡的路徑 
                    String sdpath = Environment.getExternalStorageDirectory() + "/"; 
                    mSavePath = sdpath + "download"; 
                    URL url = new URL(mHashMap.get("url")); 
                    // 創建連接 
                    HttpURLConnection conn = (HttpURLConnection) url.openConnection(); 
                    conn.connect(); 
                    // 獲取文件大小 
                    int length = conn.getContentLength(); 
                    // 創建輸入流 
                    InputStream is = conn.getInputStream(); 
 
                    File file = new File(mSavePath); 
                    // 判斷文件目錄是否存在 
                    if (!file.exists()) 
                    { 
                        file.mkdir(); 
                    } 
                    File apkFile = new File(mSavePath, mHashMap.get("name")); 
                    FileOutputStream fos = new FileOutputStream(apkFile); 
                    int count = 0; 
                    // 緩存 
                    byte buf[] = new byte[1024]; 
                    // 寫入到文件中 
                    do 
                    { 
                        int numread = is.read(buf); 
                        count += numread; 
                        // 計算進度條位置 
                        progress = (int) (((float) count / length) * 100); 
                        // 更新進度 
                        mHandler.sendEmptyMessage(DOWNLOAD); 
                        if (numread <= 0) 
                        { 
                            // 下載完成 
                            mHandler.sendEmptyMessage(DOWNLOAD_FINISH); 
                            break; 
                        } 
                        // 寫入文件 
                        fos.write(buf, 0, numread); 
                    } while (!cancelUpdate);// 點擊取消就停止下載. 
                    fos.close(); 
                    is.close(); 
                } 
            } catch (MalformedURLException e) 
            { 
                e.printStackTrace(); 
            } catch (IOException e) 
            { 
                e.printStackTrace(); 
            } 
            // 取消下載對話框顯示 
            mDownloadDialog.dismiss(); 
        } 
    }; 
 
    /**
     * 安裝APK文件
     */ 
    private void installApk() 
    { 
        File apkfile = new File(mSavePath, mHashMap.get("name")); 
        if (!apkfile.exists()) 
        { 
            return; 
        } 
        // 通過Intent安裝APK文件 
        Intent i = new Intent(Intent.ACTION_VIEW); 
        i.setDataAndType(Uri.parse("file://" + apkfile.toString()), "application/vnd.android.package-archive"); 
        mContext.startActivity(i); 
    } 
}</pre> 
<pre></pre> 
<pre name="code" class="java" style="background-color: rgb(255, 255, 255); "></pre>ParseXmlService.java 
<pre></pre> 
<pre name="code" class="java" style="background-color: rgb(255, 255, 255); "><p></p><pre name="code" class="java" style="background-color: rgb(255, 255, 255); "><pre name="code" class="java">package com.iteye.update; 
 
import java.io.InputStream; 
import java.util.HashMap; 
 
import javax.xml.parsers.DocumentBuilder; 
import javax.xml.parsers.DocumentBuilderFactory; 
 
import org.w3c.dom.Document; 
import org.w3c.dom.Element; 
import org.w3c.dom.Node; 
import org.w3c.dom.NodeList; 
 
 
public class ParseXmlService 

    public HashMap<String, String> parseXml(InputStream inStream) throws Exception 
    { 
        HashMap<String, String> hashMap = new HashMap<String, String>(); 
         
        // 實例化一個文檔構建器工廠 
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 
        // 通過文檔構建器工廠獲取一個文檔構建器 
        DocumentBuilder builder = factory.newDocumentBuilder(); 
        // 通過文檔通過文檔構建器構建一個文檔實例 
        Document document = builder.parse(inStream); 
        //獲取XML文件根節點 
        Element root = document.getDocumentElement(); 
        //獲得所有子節點 
        NodeList childNodes = root.getChildNodes(); 
        for (int j = 0; j < childNodes.getLength(); j++) 
        { 
            //遍歷子節點 
            Node childNode = (Node) childNodes.item(j); 
            if (childNode.getNodeType() == Node.ELEMENT_NODE) 
            { 
                Element childElement = (Element) childNode; 
                //版本號 
                if ("version".equals(childElement.getNodeName())) 
                { 
                    hashMap.put("version",childElement.getFirstChild().getNodeValue()); 
                } 
                //軟件名稱 
                else if (("name".equals(childElement.getNodeName()))) 
                { 
                    hashMap.put("name",childElement.getFirstChild().getNodeValue()); 
                } 
                //下載地址 
                else if (("url".equals(childElement.getNodeName()))) 
                { 
                    hashMap.put("url",childElement.getFirstChild().getNodeValue()); 
                } 
            } 
        } 
        return hashMap; 
    } 
}</pre> 
<pre></pre> 
XML代碼: 
<p></p> 
<p>version.xml</p> 
<p></p> 
<pre name="code" class="html" style="background-color: rgb(255, 255, 255); "><pre name="code" class="html"><?xml version="1.0" encoding="UTF-8"?> 
<update> 
    <version>1</version> 
    <name>baidu_xinwen_1.1.0</name> 
    <url>http://gdown.baidu.com/data/wisegame/f98d235e39e29031/baiduxinwen.apk</url> 
</update></pre><pre name="code" class="html" style="background-color: rgb(255, 255, 255); "></pre>測試代碼: 
<p><br> 
</p> 
<pre name="code" class="java" style="background-color: rgb(255, 255, 255); "><pre name="code" class="java"></pre><pre name="code" class="java">UpdateManager manager = new UpdateManager(MainTwoActivity.this); 
// 檢查軟件更新 
manager.checkUpdate();</pre> 
<p><br> 
</p> 
<p>註:隻需要把version.xml放到網上服務器去,更新版本時,到服務器把版本號和url修改一下就可以。</p> 
<p></p> 
<p></p> 
<span style="font-family:Arial,Helvetica,sans-serif"><span style="white-space:normal"></span></span> 
<pre></pre> 
<pre></pre> 
<pre></pre> 
<pre></pre> 
<pre></pre> 
 
</pre></pre></pre></pre> 
 

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。