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

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

简介

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

升级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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值