Android 检查更新库 CheckUpdateLibrary 使用教程二

816

上一篇文章介绍了CheckUpdateLibrary的基本使用,还没看的请戳这个链接look,look:(blog.csdn.net/qiang_xi/ar…)
当时是以1.0.2版本的Library为基础进行介绍的,中间有说到CheckUpdateLibrary虽然可以自定义实体类,但是前提必须得有newAppVersionCode字段,所以扩展性做的还不够好,并且1.0.2版本的Library不支持自定义Dialog,这就限制了很多有想法的开发者的发挥空间.

而现在,CheckUpdateLibrary发布了1.0.3版本,以上的这些限制都被释放了,现在开发者可以很自由了,可以自定义任意的实体类,又可以自定义任意的Dialog,而只需要借助CheckUpdateLibrary的下载和通知展示功能,就轻松的实现自己的检查更新功能.

那么具体如何自定义呢?
1,自定义任意实体类:
为了符合开闭原则,更是为了兼容以前的版本,所以在之前的基础上新增了一种检查更新的方式和回调接口CheckUpdateCallback2,使用方式如下:

Q.checkUpdate("post", "checkUpdateUrl", new CheckUpdateCallback2() {
    @Override
    public void onCheckUpdateSuccess(String result) {
        
    }
    @Override
    public void onCheckUpdateFailure(String failureMessage) {
    }
});  

使用这个方法返回的result可以是json格式或者xml格式,需要自己解析并判断有无更新.
2,自定义任意Dialog:
虽然CheckUpdateLibrary中提供了两种Dialog:ForceUpdateDialog和UpdateDialg用来展示强制更新和非强制更新对话框,但还是会有很多开发者想使用自己自定义的Dialog,然后借助于CheckUpdateLibrary中的后台下载功能和通知展示功能来实现后台下载,而CheckUpdateLibrary也考虑到开发者的这种需求,开发者只需要继承CheckUpdateLibrary中的BaseService,然后实现相关方法即可实现自己的需求,具体的使用方式如下:

对于强制更新方式,只需两步:

step1:自定义自己的Dialog,这个就不贴代码了,大家应该都会
step2:在自定义的dialog中,比如button1是用来下载文件的,那么可以使用HttpRequest类中提供的download方法,然后在各个回调中实现自己的逻辑,比如进度条监听,下载完成后安装等,示例代码如下:

button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                
                if (System.currentTimeMillis() - timeRange < 500) {
                    return;
                }
                timeRange = System.currentTimeMillis();
                setNetWorkState();
                if (!NetWorkUtil.hasNetConnection(context)) {
                    Toast.makeText(context, "当前无网络连接", Toast.LENGTH_SHORT).show();
                    return;
                }
                if ("点击安装".equals(button1.getText().toString().trim())) {
                    File file = new File(mFilePath, mFileName);
                    if (file.exists()) {
                        ApplicationUtil.installApk(context, file);
                    } else {
                        download();
                    }
                    return;
                }
                download();
            }
        });  
private void download() {
        forceUpdateProgress.setVisibility(View.VISIBLE);
        HttpRequest.download(mDownloadUrl, mFilePath, mFileName, new DownloadCallback() {
            @Override
            public void onDownloadSuccess(File file) {
                button1.setEnabled(true);
                button1.setText("点击安装");
                ApplicationUtil.installApk(context, file);
            }

            @Override
            public void onProgress(long currentProgress, long totalProgress) {
                button1.setEnabled(false);
                button1.setText("正在下载");
                forceUpdateProgress.setProgress((int) (currentProgress));
                forceUpdateProgress.setMax((int) (totalProgress));
            }

            @Override
            public void onDownloadFailure(String failureMessage) {
                button1.setEnabled(true);
                button1.setText("重新下载");
            }
        });
    }  

对于非强制更新方式:

step1:自定义自己的Dialog,这个就不贴代码了,大家应该都会
step2:继承CheckUpdateLibrary中的BaseService,实现相关方法,其中Service中自带的方法onStartCommand必须重写,一会说为什么.
示例代码如下:

public class DownloadService extends BaseService {
    private int iconResId;
    private String appName;
    private Intent mIntent;

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        if (null == intent) {
            return START_NOT_STICKY;
        }
        mIntent = intent;
        appName = intent.getStringExtra("appName");
        iconResId = intent.getIntExtra("iconResId", -1);
        if (iconResId == -1) {
            iconResId = R.drawable.icon_downloading;
        }
        download(intent.getStringExtra("downloadUrl"), intent.getStringExtra("filePath"), intent.getStringExtra("fileName"), true);
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public void downloadFailure(String failureMessage) {
        NotificationUtil.showDownloadFailureNotification(this, mIntent, iconResId, appName, "下载失败,点击重新下载", true);
    }

    @Override
    public void downloadSuccess(File file) {
        NotificationUtil.showDownloadSuccessNotification(this, file, iconResId, appName, "下载完成,点击安装", false);
    }

    @Override
    public void downloading(int currentProgress, int totalProgress) {
        NotificationUtil.showDownloadingNotification(this, currentProgress, totalProgress, iconResId, appName, false);
    }
}

step3:在自定义的dialog中,比如button1是用来下载文件的,那么可以这么写:

button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //防抖动,两次点击间隔小于1s都return
                if (System.currentTimeMillis() - timeRange < 1000) {
                    return
                }
                timeRange = System.currentTimeMillis()
                setNetWorkState()
                if (!NetWorkUtil.hasNetConnection(context)) {
                    Toast.makeText(context, "当前无网络连接", Toast.LENGTH_SHORT).show()
                    return
                }
                Intent intent = new Intent(context, DownloadService.class)
                intent.putExtra("downloadUrl", mDownloadUrl)
                intent.putExtra("filePath", mFilePath)
                intent.putExtra("fileName", mFileName)
                intent.putExtra("iconResId", mIconResId)
                intent.putExtra("isShowProgress", isShowProgress)
                intent.putExtra("appName", mAppName)
                context.startService(intent)
                dismiss()
                Toast.makeText(context, "正在后台为您下载", Toast.LENGTH_SHORT).show()
            }
        })

ok,到此,自定义非强制更新Dialog的后台下载功能就实现了.

下面说一说为什么必须要重写onStartCommand方法,主要有两点原因:

1,其实从上面的代码也可以看出,启动Service采用的是context.startService(intent);方法,而不是bind方法,所以在Service中接收intent必须重写onStartCommand方法才行.
2,如果下载失败时,点击通知是可以重新下载应用的,其中应用到PendingIntent.getService()方法,而该方法启动Service就是通过context.startService(intent);方式启动的,所以从这一点出发,仍然必须重写onStartCommand方法才行.

最后再说几句:

1.0.3版本的CheckUpdateLibrary完全兼容以前的版本,所以可以放心使用.
小伙伴们在使用的过程中如果有任何问题,意见或建议欢迎在评论区留言,当然更希望大家去github上提交issue.CheckUpdateLibrary的github地址如下:
(github.com/qiangxi/Che…)
最后的最后,跪求Star或Fork,这是对开发者最好的激励!!!