ElainePhoneSafe总结笔记1

1、JSON解析

例如:checkVersion()方法,用于检查版本更新

注意:这段代码的运行需要添加INTENT的权限支持

private void checkVersion(){
    new Thread(){
        @Override
        public void run() {
            Message message = Message.obtain();
            try {
                HttpURLConnection conn = (HttpURLConnection)new URL("http://10.0.2.2:8089/update630.json").openConnection();
                conn.setRequestMethod("GET");
                conn.setConnectTimeout(2000);//连接超时
                conn.setReadTimeout(2000);//读取超时
                conn.connect();

                int responseCode = conn.getResponseCode();
                if(responseCode == 200){
                    InputStream in = conn.getInputStream();
                    String result = StreamUtils.stream2String(in);

                    //解析json
                    JSONObject json = new JSONObject(result);
                    //获取json中的元素值
                    mVersionName = json.getString("versionName");
                    mVersionCode = json.getInt("versionCode");
                    mDesc = json.getString("desc");
                    mUrl = json.getString("url");

                    if(mVersionCode > getVersionCode()){
                        message.what = CODE_UPDATE_DIALOG;
                    }
                    else{
                        message.what = CODE_ENTER_HOME;
                    }
                }

            } catch (MalformedURLException e) {
                .....
            }finally{
                handler.sendMessage(message);
            }
        }
    }.start();
}

2、将InputStream转化为String的代码

public static String stream2String(InputStream in ) throws IOException{
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    int len = 0;
    byte[] buffer = new byte[1024];
    while((len = in.read(buffer))!= -1){
        out.write(buffer,0,len);
    }
    in.close();
    out.close();
    return out.toString();
}

3、开源框架Xutils的使用

Xutils相关资料的下载可以去github中去找

这次ElainePhoneSafe项目中暂时用到的是Xutil中HttpUtils的功能,使用它的download方法,解决在线下载的功能,
值得注意的是:HttpUtils中的方法是在主线程中进行的!所以可以在里面对UI进行修改操作!

protected void downloadAPK() {
    if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
        //判断SD卡是否存在
        tv_progress.setVisibility(View.VISIBLE);
        String path = Environment.getExternalStorageDirectory().getAbsolutePath();
        path += "/****.apk";
        //Xutils
        HttpUtils utils = new HttpUtils();
        utils.download(mUrl, path, new RequestCallBack<File>() {
            // 在主线程运行
            @Override
            public void onLoading(long total, long current,
                    boolean isUploading) {
                super.onLoading(total, current, isUploading);
                // 下载进度
                int percent = (int) (100 * current / total);
                System.out.println("下载进度:" + percent + "%");
                tv_progress.setText("下载进度:" + percent + "%");
            }

            //主线程运行
            @Override
            public void onSuccess(ResponseInfo<File> responseInfo) {
                String path2 = responseInfo.result.getAbsolutePath();
                System.out.println("成功下载,路径为:"+path2);
                //跳转到安装页面
                Intent intent = new Intent();
                intent.setAction(Intent.ACTION_VIEW);
                intent.addCategory(Intent.CATEGORY_DEFAULT);
                intent.setDataAndType(Uri.fromFile(responseInfo.result), "application/vnd.android.package-archive");
                startActivityForResult(intent, 0);
            }

            @Override
            public void onFailure(HttpException error, String msg) {
                error.printStackTrace();
                ToastUtlis.showMessage(getApplicationContext(), msg);
            }
        });

    }
    else{
        ToastUtlis.showMessage(getApplicationContext(), "没有找到SD卡");
    }
}

4、Activity跳转到安装页面的代码

                //跳转到安装页面
                Intent intent = new Intent();
                intent.setAction(Intent.ACTION_VIEW);
                intent.addCategory(Intent.CATEGORY_DEFAULT);
                intent.setDataAndType(Uri.fromFile(responseInfo.result), "application/vnd.android.package-archive");
                startActivityForResult(intent, 0);

5、Apk签名打包

eclipse中使用的测试签名为:

  • 测试签名文件 debug.keystore Prefrence-Android-Build,有效期1年,
  • 别名:androiddebugkey,
  • 密码: android, 从eclipse直接运行项目,采用这个签名文件

然而,当一个应用要真正上线的话,就需要正式签名,正式签名打包中的签名秘钥就要注意保存备份,防止丢失。
正式签名打包的步骤如下:

  • 如图打开后,再按照系统给的提示一步步填写即可。

注意:有些时候,打包总会出现下图所示的错误:

解决方法如下图:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值