Android自动更新

1.在后台维护一个Android的版本号,当每次进入APP的时候,在欢迎界面时,都去查询这个最新的版本号.和当前APP的版本对比.
2.将最新的APP(最新版本号)放在服务器上,并且提供一个下载功能的url(可以在后台维护一个上传最新的APP的一个功能).

    /**
     * 下载app
     * 
     * @param url
     * @return
     */
    public static Object downloadPdaLookUp(String action, final Context mContext) {

        String result = "[{\"note\":\"网络异常!请检查网络!\",\"networkflag\":true}]";
        if (!NetworkUtil.getNetworkStatus()) {
            return result;
        }

        String realurl = s_url_lookup + action;
        LogUtil.i(TAG, "" + realurl);

        try {
            HttpClient httpClient = new HttpClient();
            UsernamePasswordCredentials creds = new UsernamePasswordCredentials(
                    ActivityUtil.userName_c, ActivityUtil.password_c);
            LogUtil.i(TAG + "用户", ActivityUtil.userName_c
                    + ActivityUtil.password_c);
            httpClient.getState().setCredentials(AuthScope.ANY, creds);
            // 网络连接时间10秒
            httpClient.getParams().setParameter(
                    CoreConnectionPNames.CONNECTION_TIMEOUT, 20 * 1000);
            // server端返回数据时间10秒
            httpClient.getParams().setParameter(
                    CoreConnectionPNames.SO_TIMEOUT, 50 * 1000);

            PostMethod method = new PostMethod(realurl);
            method.setDoAuthentication(true);
            method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
                    new DefaultHttpMethodRetryHandler(3, false));
            int statusCode = httpClient.executeMethod(method);
            if (statusCode == 200) {
                InputStream is;
                is = method.getResponseBodyAsStream();
                String SDCard = Environment.getExternalStorageDirectory() + "";
                String pathName = "/sdcard" + "/pda.apk";// 文件存储路径
                // /storage/emulated/0/pda.apk
                File file = new File(pathName);
                FileOutputStream fos = new FileOutputStream(file);
                byte[] buf = new byte[1024];
                int count = 0;
                while ((count = is.read(buf)) != -1) {
                    fos.write(buf, 0, count);
                }
                fos.close();
                is.close();
                openFile(file, mContext);
                result = "[{\"note\":\"下载完成!\",\"status\":true}]";
                return result;
            }
        } catch (ClientProtocolException e) {
            // e.printStackTrace();
            result = "[{\"note\":\"网络协议异常(ClientProtocolException)!\",\"networkflag\":true}]";
            return result;
        } catch (IOException e) {
            // e.printStackTrace();
            result = "[{\"note\":\"后台或网络(IOException)异常!\",\"networkflag\":true}]";
            return result;
        }
        result = "[{\"note\":\"未知错误!请联系开发人员!\",\"networkflag\":true}]";
        return result;
    }

/**
     * 下载完pda后,自动打开安装程序.
     * 
     * @param file
     * @param mContext
     */
    private static void openFile(File file, final Context mContext) {
        // TODO Auto-generated method stub
        LogUtil.i("OpenFile", file.getName());
        Intent intent = new Intent();
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.setAction(android.content.Intent.ACTION_VIEW);
        intent.setDataAndType(Uri.fromFile(file),
                "application/vnd.android.package-archive");
        mContext.startActivity(intent);
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值