Android 实现版本更新

 步骤:

1、获取已安装的软件版本号;

View Code
/**
     * 获得软件版本号
     * 
     * @param context
     * @return
     */
    public String getVerName(Context context) {
        String verName = "";
        try {
            verName = context.getPackageManager().getPackageInfo(
                    "com.ichances.zhongyue", 0).versionName;
        } catch (NameNotFoundException e) {
            Log.e("获取版本信息出错", e.getMessage());
        }
        return verName;
    }

2、通过WebService将参数发送给服务端,在服务端进行匹配,返回相应的Map;

3、若需要更新,通过Map中下载Url,将apk下载下来;

View Code
/**
     * 下载版本文件
     */
    private void doNewVersionUpdate() {
        String msg = "发现最新版本" + newVerName + ",是否更新?";
        new AlertDialog.Builder(EvenMoreActivity.this)
                .setTitle("软件更新提示")
                .setMessage(msg)
                // 设置内容
                .setPositiveButton("更新",// 设置确定按钮
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog,
                                    int which) {
                                downFile(downUrl);
                                dialog.dismiss();
                            }
                        })
                .setNegativeButton("暂不更新",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,
                                    int whichButton) {
                                dialog.dismiss();
                            }
                        }).create().show();
    }

    void downFile(final String url) {
        new Thread() {
            public void run() {
                HttpClient client = new DefaultHttpClient();
                HttpGet get = new HttpGet(url);
                HttpResponse response;
                try {
                    response = client.execute(get);
                    HttpEntity entity = response.getEntity();
                    InputStream is = entity.getContent();
                    FileOutputStream fileOutputStream = null;
                    if (is != null) {
                        File storeDir = new File(AppSession.storePath);
                        if (!storeDir.exists()) {
                            storeDir.mkdirs();
                        }
                        File file = new File(AppSession.storePath, packageName);
                        fileOutputStream = new FileOutputStream(file);

                        byte[] buf = new byte[1024];
                        int ch = -1;
                        while ((ch = is.read(buf)) != -1) {
                            fileOutputStream.write(buf, 0, ch);
                        }
                    }
                    fileOutputStream.flush();
                    if (fileOutputStream != null) {
                        fileOutputStream.close();
                    }
                    update();
                } catch (ClientProtocolException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }.start();
    }

4、启动安装Intent,即可

View Code

参考:http://blog.csdn.net/xjanker2/article/details/6303937

posted @ 2012-08-02 14:32  ok_lanyan  阅读(233)  评论(0编辑  收藏  举报