LedPedometer(运动计步器)开发笔记-1.升级app功能

来源:互联网 发布:公司网络管理需求分析 编辑:程序博客网 时间:2024/04/29 00:50

本文章,用于自己做笔记使用

简介

  • 主要功能,采集,处理,展示数据,向后端推送数据,同步数据。

升级app功能

1.查看服务器上是否有版本更新

1.获取自身版本号private String getVersionName() {        // TODO Auto-generated method stub        try {            PackageManager pm = getPackageManager();            PackageInfo info = pm.getPackageInfo(getPackageName(), 0);            return info.versionName;        } catch (NameNotFoundException e) {            // TODO Auto-generated catch block            e.printStackTrace();        }        return "";    }2.查看服务器版本号URL url = new URL(getString(R.string.server_url));                    HttpURLConnection conn = (HttpURLConnection) url.openConnection();                    conn.setRequestMethod("GET");                    conn.setConnectTimeout(4000);                    int code=conn.getResponseCode();                    if(code==200){                        InputStream is = conn.getInputStream();                        String result = StreamTools.readFromStream(is);                        Log.i(TAG, result);                        JSONObject obj=new JSONObject(result);                        Version = (String) obj.get("version");                        Description = (String) obj.get("description");                        Apkurl = (String) obj.get("apkurl");                        if(getVersionName().equals(Version)){                            mes.what = ENTER_HOME;                        }else{                            mes.what = SHOW_UPDATE_DIALOG;                        }                    }3.下载apk并安装  使用afinal_0.5_bin.jar包if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){                    //affinal                    FinalHttp finalHttp = new FinalHttp();                    finalHttp.download(Apkurl, Environment.getExternalStorageDirectory().getAbsolutePath()+"/mobilesafe2.0.apk", new AjaxCallBack<File>() {                        @Override                        public void onFailure(Throwable t, int errorNo,                                String strMsg) {                            // TODO Auto-generated method stub                            t.printStackTrace();                            Toast.makeText(getApplicationContext(), "下载失败", 0);                            super.onFailure(t, errorNo, strMsg);                        }                        @Override                        public void onLoading(long count, long current) {                            // TODO Auto-generated method stub                            super.onLoading(count, current);                            tv_update_info.setVisibility(View.VISIBLE);                            int progress = (int) (current*100/count);                            tv_update_info.setText("下载进度:"+progress+"%");                        }                        @Override                        public void onSuccess(File t) {                            // TODO Auto-generated method stub                            super.onSuccess(t);                            installAPK(t);                        }                        private void installAPK(File t) {                            // TODO Auto-generated method stub                            Intent intent =new Intent();                            intent.setAction("android.intent.action.VIEW");                            intent.addCategory("android.intent.category.DEFAULT");                            intent.setDataAndType(Uri.fromFile(t), "application/vnd.android.package-archive");                            startActivity(intent);                        }                    });                }else{                    Toast.makeText(getApplicationContext(), "没有sdcard,请安装上在试",Toast.LENGTH_SHORT).show();                }
0 0
原创粉丝点击