Android 程序自动更新功能模块实现 – wainiwann – 博客园

在程序启动的时候检测服务器上有没有对应版本更新,如果有更新,提示用户是否更新。

在程序启动的时候首先调用更新模块检测服务器上存放的版本号跟当前程序的版本号如果大于当前版本号,弹出更新对话框,如果用户选择更新,则显示当前更新状态,然后替换当前程序

程序调用版本更新检测:

复制代码

<span style="color: #0000ff;">    private</span> UpdateManager updateMan;<br>    <span style="color: #0000ff;">private</span> ProgressDialog updateProgressDialog;<br>    <br>    <span style="color: #008000;">/**</span><span style="color: #008000;"> Called when the activity is first created. </span><span style="color: #008000;">*/</span><br>    @Override<br>    <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span> onCreate(Bundle savedInstanceState) <br>    {<br>        <span style="color: #0000ff;">super</span>.onCreate(savedInstanceState);<br>        setContentView(R.layout.main);<br>        <br>        <span style="color: #008000;">//</span><span style="color: #008000;">没有判断网路是否连接<br>        <br>        <br>        </span><span style="color: #008000;">//</span><span style="color: #008000;">检查是否有更新<br>        </span><span style="color: #008000;">//</span><span style="color: #008000;">如果有更新提示下载</span><span style="color: #008000;"><br></span>        updateMan = <span style="color: #0000ff;">new</span> UpdateManager(Update_TestActivity.<span style="color: #0000ff;">this</span>, appUpdateCb);<br>       updateMan.checkUpdate();<br>    }

复制代码

 执行检测版本号以及回调更新提示

下载更新文件等实现:

复制代码

<span style="color: #0000ff;">package</span> update.test;<br><br><span style="color: #0000ff;">import</span> java.io.File;<br><span style="color: #0000ff;">import</span> java.io.FileOutputStream;<br><span style="color: #0000ff;">import</span> java.io.IOException;<br><span style="color: #0000ff;">import</span> java.io.InputStream;<br><span style="color: #0000ff;">import</span> java.net.HttpURLConnection;<br><span style="color: #0000ff;">import</span> java.net.MalformedURLException;<br><span style="color: #0000ff;">import</span> java.net.URL;<br><br><span style="color: #0000ff;">import</span> org.json.JSONArray;<br><span style="color: #0000ff;">import</span> org.json.JSONObject;<br><br><span style="color: #0000ff;">import</span> com.trinet.util.NetHelper;<br><br><span style="color: #0000ff;">import</span> android.content.Context;<br><span style="color: #0000ff;">import</span> android.content.Intent;<br><span style="color: #0000ff;">import</span> android.content.pm.PackageInfo;<br><span style="color: #0000ff;">import</span> android.content.pm.PackageManager.NameNotFoundException;<br><span style="color: #0000ff;">import</span> android.net.Uri;<br><span style="color: #0000ff;">import</span> android.os.Handler;<br><span style="color: #0000ff;">import</span> android.os.Message;<br><span style="color: #0000ff;">import</span> android.util.Log;<br> <br><span style="color: #0000ff;">     public</span> <span style="color: #0000ff;">class</span> UpdateManager {<br><br>     <span style="color: #0000ff;">private</span> String curVersion;<br>     <span style="color: #0000ff;">private</span> String newVersion;<br>     <span style="color: #0000ff;">private</span> <span style="color: #0000ff;">int</span> curVersionCode;<br>     <span style="color: #0000ff;">private</span> <span style="color: #0000ff;">int</span> newVersionCode;<br>     <span style="color: #0000ff;">private</span> String updateInfo;<br>     <span style="color: #0000ff;">private</span> UpdateCallback callback;<br>     <span style="color: #0000ff;">private</span> Context ctx;<br>    <br>     <span style="color: #0000ff;">private</span> <span style="color: #0000ff;">int</span> progress;  <br>     <span style="color: #0000ff;">private</span> Boolean hasNewVersion;<br>     <span style="color: #0000ff;">private</span> Boolean canceled;<br><br>     <span style="color: #008000;">//</span><span style="color: #008000;">存放更新APK文件的路径</span><span style="color: #008000;"><br></span>      <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">final</span> String UPDATE_DOWNURL = "http://www.baidu.com/update/update_test.apk";<br>     <span style="color: #008000;">//</span><span style="color: #008000;">存放更新APK文件相应的版本说明路径</span><span style="color: #008000;"><br></span>      <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">final</span> String UPDATE_CHECKURL = "http://www.baidu.com/update/update_verson.txt";<br>     <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">final</span> String UPDATE_APKNAME = "update_test.apk";<br>     <span style="color: #008000;">//</span><span style="color: #008000;">public static final String UPDATE_VERJSON = "ver.txt";</span><span style="color: #008000;"><br></span>     <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">final</span> String UPDATE_SAVENAME = "updateapk.apk";<br>     <span style="color: #0000ff;">private</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">final</span> <span style="color: #0000ff;">int</span> UPDATE_CHECKCOMPLETED = 1;<br>     <span style="color: #0000ff;">private</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">final</span> <span style="color: #0000ff;">int</span> UPDATE_DOWNLOADING = 2; <br>     <span style="color: #0000ff;">private</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">final</span> <span style="color: #0000ff;">int</span> UPDATE_DOWNLOAD_ERROR = 3; <br>     <span style="color: #0000ff;">private</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">final</span> <span style="color: #0000ff;">int</span> UPDATE_DOWNLOAD_COMPLETED = 4; <br>     <span style="color: #0000ff;">private</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">final</span> <span style="color: #0000ff;">int</span> UPDATE_DOWNLOAD_CANCELED = 5;<br><br>     <span style="color: #008000;">//</span><span style="color: #008000;">从服务器上下载apk存放文件夹</span><span style="color: #008000;"><br></span>     <span style="color: #0000ff;">private</span> String savefolder = "/mnt/innerDisk/";<br>    <span style="color: #008000;">//</span><span style="color: #008000;">private String savefolder = "/sdcard/";<br>    </span><span style="color: #008000;">//</span><span style="color: #008000;">public static final String SAVE_FOLDER =Storage. </span><span style="color: #008000;">//</span><span style="color: #008000;"> "/mnt/innerDisk";</span><span style="color: #008000;"><br></span>    <span style="color: #0000ff;">public</span> UpdateManager(Context context, UpdateCallback updateCallback) {<br>        ctx = context;<br>        callback = updateCallback;<br>        <span style="color: #008000;">//</span><span style="color: #008000;">savefolder = context.getFilesDir();</span><span style="color: #008000;"><br></span>        canceled = <span style="color: #0000ff;">false</span>;<br>        getCurVersion();<br>    }<br>    <br>    <span style="color: #0000ff;">public</span> String getNewVersionName()<br>    {<br>        <span style="color: #0000ff;">return</span> newVersion;<br>    }<br>    <br>    <span style="color: #0000ff;">public</span> String getUpdateInfo()<br>    {<br>        <span style="color: #0000ff;">return</span> updateInfo;<br>    }<br><br>    <span style="color: #0000ff;">private</span> <span style="color: #0000ff;">void</span> getCurVersion() {<br>        <span style="color: #0000ff;">try</span> {<br>            PackageInfo pInfo = ctx.getPackageManager().getPackageInfo(<br>                    ctx.getPackageName(), 0);<br>            curVersion = pInfo.versionName;<br>            curVersionCode = pInfo.versionCode;<br>        } <span style="color: #0000ff;">catch</span> (NameNotFoundException e) {<br>            Log.e("update", e.getMessage());<br>            curVersion = "1.1.1000";<br>            curVersionCode = 111000;<br>        }<br><br>    }<br><br>    <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span> checkUpdate() {        <br>        hasNewVersion = <span style="color: #0000ff;">false</span>;<br>        <span style="color: #0000ff;">new</span> Thread(){<br>            <span style="color: #008000;">//</span><span style="color: #008000;"> ***************************************************************</span><span style="color: #008000;"><br></span>            <span style="color: #008000;">/**</span><span style="color: #008000;"><br>             * @by wainiwann <br>             * <br>             </span><span style="color: #008000;">*/</span><br>            @Override<br>            <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span> run() {<br>                Log.i("@@@@@", "&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;getServerVerCode() ");<br>                <span style="color: #0000ff;">try</span> {<br><br>                    String verjson = NetHelper.httpStringGet(UPDATE_CHECKURL);<br>                    Log.i("@@@@", verjson<br>                            + "**************************************************");<br>                    JSONArray array = <span style="color: #0000ff;">new</span> JSONArray(verjson);<br><br>                    <span style="color: #0000ff;">if</span> (array.length() &gt; 0) {<br>                        JSONObject obj = array.getJSONObject(0);<br>                        <span style="color: #0000ff;">try</span> {<br>                            newVersionCode = Integer.parseInt(obj.getString("verCode"));<br>                            newVersion = obj.getString("verName");<br>                            updateInfo = "";<br>                            Log.i("newVerCode", newVersionCode<br>                                    + "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");<br>                            Log.i("newVerName", newVersion<br>                                    + "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");<br>                            <span style="color: #0000ff;">if</span> (newVersionCode &gt; curVersionCode) {<br>                                hasNewVersion = <span style="color: #0000ff;">true</span>;<br>                            }<br>                        } <span style="color: #0000ff;">catch</span> (Exception e) {<br>                            newVersionCode = -1;<br>                            newVersion = "";<br>                            updateInfo = "";<br>                            <br>                        }<br>                    }<br>                } <span style="color: #0000ff;">catch</span> (Exception e) {<br>                    Log.e("update", e.getMessage());<br>                }<br>                updateHandler.sendEmptyMessage(UPDATE_CHECKCOMPLETED);<br>            };<br>            <span style="color: #008000;">//</span><span style="color: #008000;"> ***************************************************************</span><span style="color: #008000;"><br></span>        }.start();<br><br>    }<br><br>    <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span> update() {<br>        Intent intent = <span style="color: #0000ff;">new</span> Intent(Intent.ACTION_VIEW);<br>        <br>        intent.setDataAndType(<br>                Uri.fromFile(<span style="color: #0000ff;">new</span> File(savefolder, UPDATE_SAVENAME)),<br>                "application/vnd.android.package-archive");<br>        ctx.startActivity(intent);<br>    }<br><br>    <span style="color: #008000;">//</span><span style="color: #008000;"> +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++</span><span style="color: #008000;"><br></span><br>    <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span> downloadPackage() <br>    {<br>        <br>        <br>        <span style="color: #0000ff;">new</span> Thread() {            <br>             @Override  <br>                <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span> run() {  <br>                    <span style="color: #0000ff;">try</span> {  <br>                        URL url = <span style="color: #0000ff;">new</span> URL(UPDATE_DOWNURL);  <br>                      <br>                        HttpURLConnection conn = (HttpURLConnection)url.openConnection();  <br>                        conn.connect();  <br>                        <span style="color: #0000ff;">int</span> length = conn.getContentLength();  <br>                        InputStream is = conn.getInputStream();  <br>                          <br>                       <br>                        File ApkFile = <span style="color: #0000ff;">new</span> File(savefolder,UPDATE_SAVENAME);<br>                        <br>                        <br>                        <span style="color: #0000ff;">if</span>(ApkFile.exists())<br>                        {<br>                            <br>                            ApkFile.delete();<br>                        }<br>                        <br>                        <br>                        FileOutputStream fos = <span style="color: #0000ff;">new</span> FileOutputStream(ApkFile);  <br>                         <br>                        <span style="color: #0000ff;">int</span> count = 0;  <br>                        <span style="color: #0000ff;">byte</span> buf[] = <span style="color: #0000ff;">new</span> <span style="color: #0000ff;">byte</span>[512];  <br>                          <br>                        <span style="color: #0000ff;">do</span>{  <br>                            <br>                            <span style="color: #0000ff;">int</span> numread = is.read(buf);  <br>                            count += numread;  <br>                            progress =(<span style="color: #0000ff;">int</span>)(((<span style="color: #0000ff;">float</span>)count / length) * 100);  <br>                           <br>                            updateHandler.sendMessage(updateHandler.obtainMessage(UPDATE_DOWNLOADING)); <br>                            <span style="color: #0000ff;">if</span>(numread &lt;= 0){      <br>                                <br>                                updateHandler.sendEmptyMessage(UPDATE_DOWNLOAD_COMPLETED);<br>                                <span style="color: #0000ff;">break</span>;  <br>                            }  <br>                            fos.write(buf,0,numread);  <br>                        }<span style="color: #0000ff;">while</span>(!canceled);  <br>                        <span style="color: #0000ff;">if</span>(canceled)<br>                        {<br>                            updateHandler.sendEmptyMessage(UPDATE_DOWNLOAD_CANCELED);<br>                        }<br>                        fos.close();  <br>                        is.close();  <br>                    } <span style="color: #0000ff;">catch</span> (MalformedURLException e) {  <br>                        e.printStackTrace(); <br>                        <br>                        updateHandler.sendMessage(updateHandler.obtainMessage(UPDATE_DOWNLOAD_ERROR,e.getMessage()));<br>                    } <span style="color: #0000ff;">catch</span>(IOException e){  <br>                        e.printStackTrace();  <br>                        <br>                        updateHandler.sendMessage(updateHandler.obtainMessage(UPDATE_DOWNLOAD_ERROR,e.getMessage()));<br>                    }  <br>                      <br>                } <br>        }.start();<br>    }<br><br>    <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span> cancelDownload()<br>    {<br>        canceled = <span style="color: #0000ff;">true</span>;<br>    }<br>    <br>    Handler updateHandler = <span style="color: #0000ff;">new</span> Handler() <br>    {<br>        @Override<br>        <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span> handleMessage(Message msg) {<br><br>            <span style="color: #0000ff;">switch</span> (msg.what) {<br>            <span style="color: #0000ff;">case</span> UPDATE_CHECKCOMPLETED:<br>                <br>                callback.checkUpdateCompleted(hasNewVersion, newVersion);<br>                <span style="color: #0000ff;">break</span>;<br>            <span style="color: #0000ff;">case</span> UPDATE_DOWNLOADING:<br>                <br>                callback.downloadProgressChanged(progress);<br>                <span style="color: #0000ff;">break</span>;<br>            <span style="color: #0000ff;">case</span> UPDATE_DOWNLOAD_ERROR:<br>                <br>                callback.downloadCompleted(<span style="color: #0000ff;">false</span>, msg.obj.toString());<br>                <span style="color: #0000ff;">break</span>;<br>            <span style="color: #0000ff;">case</span> UPDATE_DOWNLOAD_COMPLETED:<br>                <br>                callback.downloadCompleted(<span style="color: #0000ff;">true</span>, "");<br>                <span style="color: #0000ff;">break</span>;<br>            <span style="color: #0000ff;">case</span> UPDATE_DOWNLOAD_CANCELED:<br>                <br>                callback.downloadCanceled();<br>            <span style="color: #0000ff;">default</span>:<br>                <span style="color: #0000ff;">break</span>;<br>            }<br>        }<br>    };<br><br>    <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">interface</span> UpdateCallback {<br>        <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span> checkUpdateCompleted(Boolean hasUpdate,<br>                CharSequence updateInfo);<br><br>        <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span> downloadProgressChanged(<span style="color: #0000ff;">int</span> progress);<br>        <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span> downloadCanceled();<br>        <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span> downloadCompleted(Boolean sucess, CharSequence errorMsg);<br>    }<br><br>}

复制代码

需要连接服务器模块:

复制代码

<span style="color: #0000ff;">package</span> com.trinet.util;<br><br><span style="color: #0000ff;">import</span> java.io.BufferedReader;<br><span style="color: #0000ff;">import</span> java.io.IOException;<br><span style="color: #0000ff;">import</span> java.io.InputStream;<br><span style="color: #0000ff;">import</span> java.io.InputStreamReader;<br><span style="color: #0000ff;">import</span> java.net.MalformedURLException;<br><span style="color: #0000ff;">import</span> java.net.URI;<br><span style="color: #0000ff;">import</span> java.net.URL;<br><br><span style="color: #0000ff;">import</span> org.apache.http.HttpResponse;<br><span style="color: #0000ff;">import</span> org.apache.http.client.HttpClient;<br><span style="color: #0000ff;">import</span> org.apache.http.client.methods.HttpGet;<br><span style="color: #0000ff;">import</span> org.apache.http.impl.client.DefaultHttpClient;<br><span style="color: #0000ff;">import</span> org.apache.http.params.CoreProtocolPNames;<br><span style="color: #0000ff;">import</span> org.apache.http.params.HttpConnectionParams;<br><span style="color: #0000ff;">import</span> org.apache.http.params.HttpParams;<br><br><span style="color: #0000ff;">import</span> android.content.Context;<br><span style="color: #0000ff;">import</span> android.graphics.drawable.Drawable;<br><span style="color: #0000ff;">import</span> android.net.ConnectivityManager;<br><span style="color: #0000ff;">import</span> android.net.NetworkInfo;<br><span style="color: #0000ff;">import</span> android.util.Log;<br><br><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">class</span> NetHelper {<br><br>    <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> String httpStringGet(String url) <span style="color: #0000ff;">throws</span> Exception {<br>        <span style="color: #0000ff;">return</span> httpStringGet(url, "utf-8");<br>    }<br><br>    <span style="color: #008000;">/**</span><span style="color: #008000;"><br>     * <br>     * <br>     * </span><span style="color: #808080;">@param</span><span style="color: #008000;"> url<br>     * </span><span style="color: #808080;">@return</span><span style="color: #008000;"><br>     </span><span style="color: #008000;">*/</span><br>    <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> Drawable loadImage(String url) {<br>        <span style="color: #0000ff;">try</span> {<br>            <span style="color: #0000ff;">return</span> Drawable.createFromStream(<br>                    (InputStream) <span style="color: #0000ff;">new</span> URL(url).getContent(), "test");<br>        } <span style="color: #0000ff;">catch</span> (MalformedURLException e) {<br>            Log.e("exception", e.getMessage());<br>        } <span style="color: #0000ff;">catch</span> (IOException e) {<br>            Log.e("exception", e.getMessage());<br>        }<br>        <span style="color: #0000ff;">return</span> <span style="color: #0000ff;">null</span>;<br>    }<br><br>    <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> String httpStringGet(String url, String enc) <span style="color: #0000ff;">throws</span> Exception {<br>        <span style="color: #008000;">//</span><span style="color: #008000;"> This method for HttpConnection</span><span style="color: #008000;"><br></span>        String page = "";<br>        BufferedReader bufferedReader = <span style="color: #0000ff;">null</span>;<br>        <span style="color: #0000ff;">try</span> {<br>            HttpClient client = <span style="color: #0000ff;">new</span> DefaultHttpClient();<br>            client.getParams().setParameter(CoreProtocolPNames.USER_AGENT,<br>                    "android");<br>            <br>             HttpParams httpParams = client.getParams();<br>             HttpConnectionParams.setConnectionTimeout(httpParams, 3000);<br>             HttpConnectionParams.setSoTimeout(httpParams, 5000);<br>             <br>            HttpGet request = <span style="color: #0000ff;">new</span> HttpGet();<br>            request.setHeader("Content-Type", "text/plain; charset=utf-8");<br>            request.setURI(<span style="color: #0000ff;">new</span> URI(url));<br>            HttpResponse response = client.execute(request);<br>            bufferedReader = <span style="color: #0000ff;">new</span> BufferedReader(<span style="color: #0000ff;">new</span> InputStreamReader(response<br>                    .getEntity().getContent(), enc));<br><br>            StringBuffer stringBuffer = <span style="color: #0000ff;">new</span> StringBuffer("");<br>            String line = "";<br><br>            String NL = System.getProperty("line.separator");<br>            <span style="color: #0000ff;">while</span> ((line = bufferedReader.readLine()) != <span style="color: #0000ff;">null</span>) {<br>                stringBuffer.append(line + NL);<br>            }<br>            bufferedReader.close();<br>            page = stringBuffer.toString();<br>            Log.i("page", page);<br>            System.out.println(page + "page");<br>            <span style="color: #0000ff;">return</span> page;<br>        } <span style="color: #0000ff;">finally</span> {<br>            <span style="color: #0000ff;">if</span> (bufferedReader != <span style="color: #0000ff;">null</span>) {<br>                <span style="color: #0000ff;">try</span> {<br>                    bufferedReader.close();<br>                } <span style="color: #0000ff;">catch</span> (IOException e) {<br>                    Log.d("BBB", e.toString());<br>                }<br>            }<br>        }<br>    }<br><br>    <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">boolean</span> checkNetWorkStatus(Context context) {<br>        <span style="color: #0000ff;">boolean</span> result;<br>        ConnectivityManager cm = (ConnectivityManager) context<br>                .getSystemService(Context.CONNECTIVITY_SERVICE);<br>        NetworkInfo netinfo = cm.getActiveNetworkInfo();<br>        <span style="color: #0000ff;">if</span> (netinfo != <span style="color: #0000ff;">null</span> &amp;&amp; netinfo.isConnected()) {<br>            result = <span style="color: #0000ff;">true</span>;<br>            Log.i("NetStatus", "The net was connected");<br>        } <span style="color: #0000ff;">else</span> {<br>            result = <span style="color: #0000ff;">false</span>;<br>            Log.i("NetStatus", "The net was bad!");<br>        }<br>        <span style="color: #0000ff;">return</span> result;<br>    }<br>}

复制代码

以及提示对话框:

复制代码

<span style="color: #0000ff;">package</span> com.trinet.util;<br><br><span style="color: #0000ff;">import</span> java.lang.reflect.Field;<br><br><span style="color: #0000ff;">import</span> android.app.AlertDialog;<br><span style="color: #0000ff;">import</span> android.app.AlertDialog.Builder;<br><span style="color: #0000ff;">import</span> android.content.Context;<br><span style="color: #0000ff;">import</span> android.content.DialogInterface;<br><span style="color: #0000ff;">import</span> android.content.DialogInterface.OnClickListener;<br><span style="color: #0000ff;">import</span> android.view.View;<br><br><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">class</span> DialogHelper {<br><br>    <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">void</span> Alert(Context ctx, CharSequence title, CharSequence message,<br>            CharSequence okText, OnClickListener oklistener) {<br>        AlertDialog.Builder builder = createDialog(ctx, title, message);<br>        builder.setPositiveButton(okText, oklistener);<br>        builder.create().show();<br>    }<br>    <br>    <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">void</span> Alert(Context ctx, <span style="color: #0000ff;">int</span> titleId, <span style="color: #0000ff;">int</span> messageId,<br>            <span style="color: #0000ff;">int</span> okTextId, OnClickListener oklistener) {<br>        Alert(ctx, ctx.getText(titleId), ctx.getText(messageId), ctx.getText(okTextId), oklistener);<br>    }<br><br>    <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">void</span> Confirm(Context ctx, CharSequence title, CharSequence message,<br>            CharSequence okText, OnClickListener oklistener, CharSequence cancelText,<br>            OnClickListener cancellistener) {<br>        AlertDialog.Builder builder = createDialog(ctx, title, message);<br>        builder.setPositiveButton(okText, oklistener);<br>        builder.setNegativeButton(cancelText, cancellistener);<br>        builder.create().show();<br>    }<br>    <br>    <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">void</span> Confirm(Context ctx, <span style="color: #0000ff;">int</span> titleId, <span style="color: #0000ff;">int</span> messageId,<br>            <span style="color: #0000ff;">int</span> okTextId, OnClickListener oklistener,  <span style="color: #0000ff;">int</span> cancelTextId,<br>            OnClickListener cancellistener) {<br>        Confirm(ctx, ctx.getText(titleId), ctx.getText(messageId), ctx.getText(okTextId), oklistener, ctx.getText(cancelTextId), cancellistener);<br>    }<br><br>    <span style="color: #0000ff;">private</span> <span style="color: #0000ff;">static</span> AlertDialog.Builder createDialog(Context ctx, CharSequence title,<br>            CharSequence message) {<br>        AlertDialog.Builder builder = <span style="color: #0000ff;">new</span> Builder(ctx);<br>        builder.setMessage(message);<br>        <span style="color: #0000ff;">if</span>(title!=<span style="color: #0000ff;">null</span>)<br>        {<br>            builder.setTitle(title);<br>        }<br>        <span style="color: #0000ff;">return</span> builder;<br>    }    <br>    <br>    @SuppressWarnings("unused")<br>    <span style="color: #0000ff;">private</span> <span style="color: #0000ff;">static</span> AlertDialog.Builder createDialog(Context ctx,<span style="color: #0000ff;">int</span> titleId, <span style="color: #0000ff;">int</span> messageId) {<br>        AlertDialog.Builder builder = <span style="color: #0000ff;">new</span> Builder(ctx);<br>        builder.setMessage(messageId);<br>        builder.setTitle(titleId);<br>        <span style="color: #0000ff;">return</span> builder;<br>    }<br><br>    <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">void</span> ViewDialog(Context ctx, CharSequence title, View view,<br>            CharSequence okText, OnClickListener oklistener, CharSequence cancelText,<br>            OnClickListener cancellistener) {<br><br>    }<br>    <br>    <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">void</span> ViewDialog(Context ctx, <span style="color: #0000ff;">int</span> titleId, View view,<br>            <span style="color: #0000ff;">int</span> okTextId, OnClickListener oklistener, <span style="color: #0000ff;">int</span> cancelTextId,<br>            OnClickListener cancellistener) {<br>        <br>        ViewDialog(ctx, ctx.getText(titleId), view, ctx.getText(okTextId), oklistener, ctx.getText(cancelTextId), cancellistener);<br><br>    }<br>    <br>    <span style="color: #008000;">//</span><span style="color: #008000;"><br></span>    <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">void</span> SetDialogShowing(DialogInterface dialog, <span style="color: #0000ff;">boolean</span> showing)<br>    {<br>        <span style="color: #0000ff;">try</span> {<br>            Field field = dialog.getClass().getSuperclass().getDeclaredField("mShowing");<br>            field.setAccessible(<span style="color: #0000ff;">true</span>);<br>            field.set(dialog, showing);<br>        } <span style="color: #0000ff;">catch</span> (Exception e) {<br>            e.printStackTrace();<br>        }<br>    }<br><br>}

复制代码

下面是又更新的话执行回调函数提示用户:

复制代码

<span style="color: #008000;">//</span><span style="color: #008000;"> 自动更新回调函数</span><span style="color: #008000;"><br></span>    UpdateManager.UpdateCallback appUpdateCb = <span style="color: #0000ff;">new</span> UpdateManager.UpdateCallback() <br>    {<br><br>        <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span> downloadProgressChanged(<span style="color: #0000ff;">int</span> progress) {<br>            <span style="color: #0000ff;">if</span> (updateProgressDialog != <span style="color: #0000ff;">null</span><br>                    &amp;&amp; updateProgressDialog.isShowing()) {<br>                updateProgressDialog.setProgress(progress);<br>            }<br><br>        }<br><br>        <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span> downloadCompleted(Boolean sucess, CharSequence errorMsg) {<br>            <span style="color: #0000ff;">if</span> (updateProgressDialog != <span style="color: #0000ff;">null</span><br>                    &amp;&amp; updateProgressDialog.isShowing()) {<br>                updateProgressDialog.dismiss();<br>            }<br>            <span style="color: #0000ff;">if</span> (sucess) {<br>                updateMan.update();<br>            } <span style="color: #0000ff;">else</span> {<br>                DialogHelper.Confirm(Update_TestActivity.<span style="color: #0000ff;">this</span>,<br>                        R.string.dialog_error_title,<br>                        R.string.dialog_downfailed_msg,<br>                        R.string.dialog_downfailed_btnnext,<br>                        <span style="color: #0000ff;">new</span> DialogInterface.OnClickListener() {<br><br>                            <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span> onClick(DialogInterface dialog,<br>                                    <span style="color: #0000ff;">int</span> which) {<br>                                updateMan.downloadPackage();<br><br>                            }<br>                        }, R.string.dialog_downfailed_btnnext, <span style="color: #0000ff;">null</span>);<br>            }<br>        }<br><br>        <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span> downloadCanceled() <br>        {<br>            <span style="color: #008000;">//</span><span style="color: #008000;"> TODO Auto-generated method stub</span><span style="color: #008000;"><br></span><br>        }<br><br>        <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span> checkUpdateCompleted(Boolean hasUpdate,<br>                CharSequence updateInfo) {<br>            <span style="color: #0000ff;">if</span> (hasUpdate) {<br>                DialogHelper.Confirm(Update_TestActivity.<span style="color: #0000ff;">this</span>,<br>                        getText(R.string.dialog_update_title),<br>                        getText(R.string.dialog_update_msg).toString()<br>                        +updateInfo+<br>                        getText(R.string.dialog_update_msg2).toString(),<br>                                getText(R.string.dialog_update_btnupdate),<br>                        <span style="color: #0000ff;">new</span> DialogInterface.OnClickListener() {<br><br>                            <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span> onClick(DialogInterface dialog,<br>                                    <span style="color: #0000ff;">int</span> which) {<br>                                updateProgressDialog = <span style="color: #0000ff;">new</span> ProgressDialog(<br>                                        Update_TestActivity.<span style="color: #0000ff;">this</span>);<br>                                updateProgressDialog<br>                                        .setMessage(getText(R.string.dialog_downloading_msg));<br>                                updateProgressDialog.setIndeterminate(<span style="color: #0000ff;">false</span>);<br>                                updateProgressDialog<br>                                        .setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);<br>                                updateProgressDialog.setMax(100);<br>                                updateProgressDialog.setProgress(0);<br>                                updateProgressDialog.show();<br><br>                                updateMan.downloadPackage();<br>                            }<br>                        },getText( R.string.dialog_update_btnnext), <span style="color: #0000ff;">null</span>);<br>            }<br><br>        }<br>    };

复制代码

 

要记得给程序添加权限:

&lt;uses-permission android:name="android.permission.INTERNET"&gt;&lt;/uses-permission&gt;<br>&lt;uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /&gt; 

测试源码下载:

http://download.csdn.net/detail/wain163/7307463

来源URL:http://www.cnblogs.com/wainiwann/archive/2012/03/12/2391810.html