• Android软件更新


    Android软件更新

    //得到当前版本编码和版本名称、
    public static int getVerCode(Context context) {
            int verCode = -1;
            try {
                verCode = context.getPackageManager().getPackageInfo("com.example", 0).versionCode;
            } catch (NameNotFoundException e) {
                Log.e(TAG, e.getMessage());
            }
            return verCode;
        }
        public static String getVerName(Context context) {
            String verName = "";
            try {
                verName = context.getPackageManager().getPackageInfo("com.example", 0).versionName;
            } catch (NameNotFoundException e) {
                Log.e(TAG, e.getMessage());
            }
            return verName;    
        }
        public static String getAppName(Context context) {
            String verName = context.getResources().getText(R.string.app_name).toString();
            return verName;
        }

    // 获取更新版本内容

    public static String getContent(String url) throws Exception{
            StringBuilder sb = new StringBuilder();
            HttpClient client = new DefaultHttpClient();
            HttpParams httpParams = client.getParams();
            //设置网络超时参数
            HttpConnectionParams.setConnectionTimeout(httpParams, 3000);
            HttpConnectionParams.setSoTimeout(httpParams, 5000);
            HttpResponse response = client.execute(new HttpGet(url));
            HttpEntity entity = response.getEntity();
            if (entity != null) {
                BufferedReader reader = new BufferedReader(new InputStreamReader(entity.getContent(), "UTF-8"), 8192);
                String line = null;
                while ((line = reader.readLine())!= null){
                    sb.append(line + "
    ");
                }
                reader.close();
            }
            return sb.toString();
        }
    // 判断是否得到了服务器传来的版本号
    private boolean getServerVerCode() {
            try {
                String verjson = NetworkTool.getContent(Config.UPDATE_SERVER + Config.UPDATE_VERJSON);
                JSONArray array = new JSONArray(verjson);
                if (array.length() > 0) {
                    JSONObject obj = array.getJSONObject(0);
                    try {
                        newVerCode = Integer.parseInt(obj.getString("verCode"));
                        newVerName = obj.getString("verName");
                    } catch (Exception e) {
                        newVerCode = -1;
                        newVerName = "";
                        return false;
                    }
                }
            } catch (Exception e) {
                Log.e(TAG, e.getMessage());
                return false;
            }
            return true;
        }
    // 如果版本号得到了就更新,否则不更新
    if (getServerVerCode()) {
                int vercode = Config.getVerCode(this);
                if (newVerCode > vercode) {
                    doNewVersionUpdate();
                } else {
                    notNewVersionShow();
                }
            }
    // 更新软件的代码
    private void doNewVersionUpdate() {
            int verCode = Config.getVerCode(this);
            String verName = Config.getVerName(this);
            StringBuffer sb = new StringBuffer();
            sb.append("当前版本:");
            sb.append(verName);
            sb.append(" Code:");
            sb.append(verCode);
            sb.append(", 发现新版本:");
            sb.append(newVerName);
            sb.append(" Code:");
            sb.append(newVerCode);
            sb.append(", 是否更新?");
            Dialog dialog = new AlertDialog.Builder(UpdateActivity.this)
                    .setTitle("软件更新")
                    .setMessage(sb.toString())
                    // 设置内容
                    .setPositiveButton("更新",// 设置确定按钮
                            new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    pBar = new ProgressDialog(UpdateActivity.this);
                                    pBar.setTitle("正在下载");
                                    pBar.setMessage("请稍候...");
                                    pBar.setProgressStyle(ProgressDialog.STYLE_SPINNER);
                                    downFile(Config.UPDATE_SERVER + Config.UPDATE_APKNAME);
                                }
                            })
                    .setNegativeButton("暂不更新",
                            new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog,int whichButton) {
                                    // 点击"取消"按钮之后退出程序
                                    finish();
                                }
                            }).create();// 创建
            // 显示对话框
            dialog.show();
        }
    
        void downFile(final String url) {
            pBar.show();
            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();
                        long length = entity.getContentLength();
                        InputStream is = entity.getContent();
                        FileOutputStream fileOutputStream = null;
                        if (is != null) {
                            File file = new File( Environment.getExternalStorageDirectory(),Config.UPDATE_SAVENAME);
                            fileOutputStream = new FileOutputStream(file);
                            byte[] buf = new byte[1024];
                            int ch = -1;
                            int count = 0;
                            while ((ch = is.read(buf)) != -1) {
                                fileOutputStream.write(buf, 0, ch);
                                count += ch;
                                if (length > 0) {
                                }
                            }
                        }
                        fileOutputStream.flush();
                        if (fileOutputStream != null) {
                            fileOutputStream.close();
                        }
                        down();
                    } catch (ClientProtocolException e) {
                        e.printStackTrace();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }.start();
        }
    
        void down() {
            handler.post(new Runnable() {
                public void run() {
                    pBar.cancel();
                    update();
                }
            });
        }
    
        void update() {
            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.setDataAndType(Uri.fromFile(new File(Environment.getExternalStorageDirectory(), Config.UPDATE_SAVENAME)),
                    "application/vnd.android.package-archive");
            startActivity(intent);
        }
    // 不更新软件的代码
    private void notNewVersionShow() {
            int verCode = Config.getVerCode(this);
            String verName = Config.getVerName(this);
            StringBuffer sb = new StringBuffer();
            sb.append("当前版本:");
            sb.append(verName);
            sb.append(" Code:");
            sb.append(verCode);
            sb.append(",
    已是最新版,无需更新!");
            Dialog dialog = new AlertDialog.Builder(UpdateActivity.this)
                .setTitle("软件更新").setMessage(sb.toString())// 设置内容
                .setPositiveButton("确定",// 设置确定按钮
                    new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog,int which) {
                            finish();
                        }
                    }).create();// 创建
            // 显示对话框
            dialog.show();
        }

    代码下载链接:http://www.apkbus.com/android-153953-1-1.html

  • 相关阅读:
    Discuz!X3.2 uc_server密码正确无法登录的解决方法
    手动添加uc应用及其 提示notelist表缺少appX字段的处理方法
    discuz 3 头像显示不成功
    用户名称修改的完美解决方法
    WEB服务器、应用程序服务器、HTTP服务器区别
    小议函数指针
    Difference Between objectForKey and valueForKey in NSDictionary
    valueforkey objectforkey区别,
    budle identifier,
    return,hiding 之前,
  • 原文地址:https://www.cnblogs.com/klcf0220/p/3456525.html
Copyright © 2020-2023  润新知