Android实战系列(一)---版本更新

1、在启动页面里,在启动登录页面的同时,使用handler延迟0.2发送一个更新的广播(确保登录页面启动起来了啥)
LauncherGuide.java

AbsUI.startUI(context, LoginUI.class);
 handler.postDelayed(new Runnable() {

                    @Override
                    public void run() {
                        EventBus.getDefault().post(new Stype(Constant.LOGIN_Urage));
                    }
                }, 200);

2、Constant.LOGIN_Urage,被Login.java注册了

Login{
...
onCreate(){
EventBus.getDefault().register(this);
}
...
 @Subscribe
    public void onEventMainThread(Stype ev) {
    ...
       if (ev.getMsg() == Constant.LOGIN_Urage) {
            checkVersion();
        } 
     ...
    }
}

如果使用onEventMainThread作为订阅函数,那么不论事件是在哪个线程中发布出来的,onEventMainThread都会在UI线程中执行.继续往下看:版本更新的工具类。

UpgradeBean bean = new UpgradeBean();
        bean.setTypeID("1");    
        callBackHandler=new AsyncCallBackHandler(ui,showText,isShowDialog,null) {

            @Override
            public void mySuccess(int arg0, Header[] arg1, String result) {
                Log.e(TAG, "onPostExecute():result:" + result);
                // 调用接口
                if (adapter != null) {
                    // 完成接口
                    adapter.onQueryCompleted(result);
                    // 设置新版本
                    setNewVersion(adapter.getVersionNO());
                    //设置文件大小
                    setSize(adapter.getSize());
                    // 远程下载地址
                    setDownloadRemoteUri( adapter.getDownloadRemoteUri() );
                    Log.i(TAG, "adapter.getVersionNO()"+adapter.getVersionNO());
                    boolean bUpgrade = isUpgrade(appInfo.getVersionName()+"."+appInfo.getVersionCode(),adapter.getVersionNO());

                    // 更新事件接口
                    adapter.onUpgrade(bUpgrade, appInfo.getVersionName(),adapter.getVersionNO(), getDownloadRemoteUri(), getDownloadLocalPath(),getSize());
                }

            }

            @Override
            public void myFailure(int arg0, Header[] arg1, String arg2, Throwable arg3) {
                Toast.makeText(ui, "网络异常", Toast.LENGTH_SHORT).show();

            }
        };

        try {
            AsyncHttpUtil.post(context, queryUri, bean, "application/json", callBackHandler);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

实际上是调用后台的版本更新的接口,使用AsyncHttpUtil封装的一步http网络post请求。

UpgradeBean会传递apk类型后台

public class UpgradeBean {

    private String typeID ;//APP类型1安卓2IOS

    public String getTypeID() {
        return typeID;
    }

    public void setTypeID(String typeID) {
        this.typeID = typeID;
    }
}

Adapter是个接口

public interface Adapter {
        // 请求完成
        abstract void onQueryCompleted(String result);
        // http错误
        abstract void onHttpFailed(Error error, java.lang.Exception exception, int responseCode, byte[] out);
        // 获取最新版本号
        abstract String getVersionNO();
        //文件大小
        abstract String getSize();
        // 更新事件
        abstract void onUpgrade(boolean upgrade, String oldVersion, String newVersion, String downloadRemoteUri, String downloadLocalPath,String size);
        // 获取下载地址
        abstract String getDownloadRemoteUri();
        void onHttpFailed(java.lang.Error error, Exception exception,
                int responseCode, byte[] out);
    }

重点关心于是否更新,更新的实现,先看下服务端返回的数据结构

public class UpgradeMessageBean {
    private String msg;
    private int statu;
    private UpgradeMessage model;
    public class UpgradeMessage{
        private String VersionLink; 
        private String  VersionNo;//    V1.1.0.0
    }
public void onUpgrade(boolean upgrade, String oldVersion, String newVersion, String downloadRemoteUri, String downloadLocalPath,String size) {
                if (upgrade) {//比较手机和接口返回的版本

                    UpgradeNotificationBean notificationBean = new UpgradeNotificationBean();
                    notificationBean.setTitle(context.getResources().getString(R.string.app_name));

                    notificationBean.setOldVersion(oldVersion);
                    notificationBean.setNewVersion(newVersion);
                    notificationBean.setDownloadRemoteUri(downloadRemoteUri);
                    notificationBean.setDownloadLocalPath(downloadLocalPath);
                    notificationBean.setSize(size);
                    UpgradeDialog upgradeDialogF = new UpgradeDialog(ui);//这里的ui不能改为context,必须为界面
                    upgradeDialogF.setTitle("版本更新");
                    upgradeDialogF.setContent(context.getResources().getString(R.string.app_name)+"新版发布(v"+newVersion+")");


                    upgradeDialogF.setTaskBean(taskBean);
                    upgradeDialogF.setNotificationBean(notificationBean);
                    if (!ui.isFinishing()) {
                        upgradeDialogF.show();
                    }else {
                        Log.i(TAG, "ui.isFinishing()="+ui.isFinishing());
                    }

                }else{
                }
            }

upgradeDialogF对话框弹出后(版本不一致),有确定取消两个按钮

    btn_upgrade_sure.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if (notificationBean != null) {
                    if (new NetworkState(context).isConnected() == false) {
                        Toast.makeText(context, "无网络连接", Toast.LENGTH_SHORT).show();
                        return;
                    }
                    if (!UpgradeTaskUtil.isCanDownload) {
                        Toast.makeText(context, "没有存储卡,不能下载新版本", Toast.LENGTH_SHORT).show();
                        UpgradeDialog.this.dismiss();
                        return;
                    }
                    // 下载并且更新应用
                    UpgradeTask task = new UpgradeTask((android.support.v4.app.FragmentActivity)context);
                    if (notificationBean != null) {
                        task.setTitle( notificationBean.getTitle() );
                        task.setDownloadRemoteUri(notificationBean.getDownloadRemoteUri());
                        task.setDownloadLocalPath(notificationBean.getDownloadLocalPath());
                        task.download();
                    }
                    // 关闭自身窗口
                    UpgradeDialog.this.dismiss();
                }else{
                    Log.e(TAG, "notificationBean == null");
                }
            }
        });

        //下次再说
        btn_upgrade_cancle.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                EventBus.getDefault().post(new Stype(Constant.LOGIN_UI));
                UpgradeDialog.this.dismiss();
            }
        });

取消的话会给LoginUI发个广播,说哥们不更新直接登录就好了。
确定下载的话
则:

    new Thread(){
            public void run(){

                try {
                    URL url = new URL(downloadRemoteUri);
                    HttpURLConnection connection = (HttpURLConnection)url.openConnection();             connection.setConnectTimeout(5*1000);               
                    Message msg = Message.obtain();
                    msg.arg1 = connection.getResponseCode();
                    msg.what = MSG_RESPONDECODE;
                    handler.sendMessage(msg);

                } catch (Exception e) {
                    Log.i(TAG, "url 不可用=="+downloadRemoteUri);
                    handler.sendEmptyMessage(MSG_NO_FILE);

                }
            }
        }.start();

如果url不可用(404)则发MSG_NO_FILE,可用则把网络请求的的返回code保存起来,同时发送MSG_RESPONDECODE广播,

if(msg.arg1==200){
                downloadManager
                    }

通过内置的DownloadManager下载。

总结:
1、在软件启动后发送版本更新的广播,设定soap请求的参数。
2、对返回的版本号、url进行核对,版本一致,不提示用户下载,版本不一致,弹框提示用户。
3、用户决定下载,如果url正确可用(httpUrlconnect),则用Android内置的DownloadManager下载,否则提示文件不存在。(ACTION_PACKAGE_REPLACED、ACTION_DOWNLOAD_COMPLETE、ACTION_PACKAGE_ADDED)可以监听

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值