Git Product home page Git Product logo

appupdatedemo's Introduction

App更新工具

目录

功能介绍

  • 实现app版本更新
  • 支持get,post请求
  • 支持进度显示,对话框进度条,和通知栏进度条展示
  • 支持后台下载
  • 支持强制更新
  • 支持简单主题色配置
  • 完美支持android7.0

效果图与示例 apk

点击下载 Demo.apk 或扫描下面的二维码安装

Demo apk文件二维

使用

Gradle 依赖

dependencies {
    compile 'com.qianwen:update-app:3.0.0'
}

Download API License

接口说明

1,和服务器交互请求参数

1,appkey app的唯一标志
appkey可以在manifest文件中配置,也可以在代码中添加
xml配置如下:
  <meta-data
            android:name="UPDATE_APP_KEY"
            android:value="ab55ce55Ac4bcP408cPb8c1Aaeac179c5f6f"/>
2,version 版本号,工具自动添加(服务器判断客户端传过来的version和服务器存的最新的version,决定是否更新)
3, 服务器app后台管理界面(下次放出服务器的代码)

2, 服务器的返回json格式

1,有新版本
{
  "update": "Yes",//有新版本
  "new_version": "0.8.3",//新版本号
  "apk_file_url": "https://raw.githubusercontent.com/WVector/AppUpdateDemo/master/apk/app-debug.apk", //apk下载地址
  "update_log": "1,添加删除信用卡接口\r\n2,添加vip认证\r\n3,区分自定义消费,一个小时不限制。\r\n4,添加放弃任务接口,小时内不生成。\r\n5,消费任务手动生成。",//更新内容
  "target_size": "5M",//apk大小
  "constraint": false,//是否强制更新
  "status": "success",
  "msg": "ok",
  "timestamp": 1498785412690
}
2,没有新版本
{
  "update": "No",//没有新版本
  "status": "success",
  "msg": "ok",
  "timestamp": 1498785412690
}

1,根据自己项目使用的网络框架,自己实现HttpManager接口,

	class UpdateAppHttpUtil implements HttpManager {
	    /**
	     * 异步get
	     *
	     * @param url      get请求地址
	     * @param params   get参数
	     * @param callBack 回调
	     */
	    @Override
	    public void asyncGet(@NonNull String url, @NonNull Map<String, String> params, @NonNull final Callback callBack) {
	        OkHttpUtils.get()
	                .url(url)
	                .params(params)
	                .build()
	                .execute(new StringCallback() {
	                    @Override
	                    public void onError(Call call, Response response, Exception e, int id) {
	                        callBack.onError(validateError(e, response));
	                    }
	
	                    @Override
	                    public void onResponse(String response, int id) {
	                        callBack.onResponse(response);
	                    }
	                });
	    }
	
	    /**
	     * 异步post
	     *
	     * @param url      post请求地址
	     * @param params   post请求参数
	     * @param callBack 回调
	     */
	    @Override
	    public void asyncPost(@NonNull String url, @NonNull Map<String, String> params, @NonNull final Callback callBack) {
	        OkHttpUtils.post()
	                .url(url)
	                .params(params)
	                .build()
	                .execute(new StringCallback() {
	                    @Override
	                    public void onError(Call call, Response response, Exception e, int id) {
	                        callBack.onError(validateError(e, response));
	                    }
	
	                    @Override
	                    public void onResponse(String response, int id) {
	                        callBack.onResponse(response);
	                    }
	                });
	
	    }
	
	    /**
	     * 下载
	     *
	     * @param url      下载地址
	     * @param path     文件保存路径
	     * @param fileName 文件名称
	     * @param callback 回调
	     */
	    @Override
	    public void download(@NonNull String url, @NonNull String path, @NonNull String fileName, @NonNull final FileCallback callback) {
	        OkHttpUtils.get()
	                .url(url)
	                .build()
	                .execute(new FileCallBack(path, fileName) {
	                    @Override
	                    public void inProgress(float progress, long total, int id) {
	                        super.inProgress(progress, total, id);
	                        callback.onProgress(progress, total);
	                    }
	
	                    @Override
	                    public void onError(Call call, Response response, Exception e, int id) {
	                        callback.onError(validateError(e, response));
	                    }
	
	                    @Override
	                    public void onResponse(File response, int id) {
	                        callback.onResponse(response);
	
	                    }
	
	                    @Override
	                    public void onBefore(Request request, int id) {
	                        super.onBefore(request, id);
	                        callback.onBefore();
	                    }
	                });
	
	    }
	}

3,客户端检测是否有新版本,并且更新下载

	String updateUrl = "https://raw.githubusercontent.com/WVector/AppUpdateDemo/master/json/json.txt";
	new UpdateAppManager
	        .Builder()
	        //当前Activity
	        .setActivity(this)
	        //实现httpManager接口的对象
	        .setHttpManager(new UpdateAppHttpUtil())
	        //更新地址
	        .setUpdateUrl(updateUrl)
	        .build()
	        //检测是否有新版本
	        .checkNewApp(new UpdateCallback() {
	            /**
	             * 有新版本
	             * @param updateApp 新版本信息
	             * @param updateAppManager app更新管理器
	             */
	            @Override
	            public void hasNewApp(UpdateAppBean updateApp, UpdateAppManager updateAppManager) {
	                updateAppManager.showDialog();
	            }
	
	            /**
	             * 网络请求之前
	             */
	            @Override
	            public void onBefore() {
	                CProgressDialogUtils.showProgressDialog(MainActivity.this);
	            }
	
	            /**
	             * 网路请求之后
	             */
	            @Override
	            public void onAfter() {
	                CProgressDialogUtils.cancelProgressDialog(MainActivity.this);
	            }
	
	            /**
	             * 没有新版本
	             */
	
	            @Override
	            public void noNewApp() {
	                Toast.makeText(MainActivity.this, "没有新版本", Toast.LENGTH_SHORT).show();
	            }
	        });

   	            

License

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

appupdatedemo's People

Contributors

wvector avatar

Watchers

James Cloos avatar wuhen avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.