Android App內部自動更新Library的使用

AutoUpdateProject

最新版本已經到1.1.4

最新版本及說明請關注GitHub,歡迎Star。
github地址:https://github.com/MZCretin/AutoUpdateProject

最新版本使用及說明:http://blog.csdn.net/u010998327/article/details/70586322

1.0版本

版本更新library,提供兩種模式的版本更新,一種是對話框顯示下載進度,一種是通知欄顯示後臺默默下載形式。
特點概述

一、:可從後臺主動控制本地app強制更新,主要適用場合是某個版本有bug,會嚴重影響用戶的使用,此時用這種模式,只要用戶打開app,提醒強制更新,否則不能進入app;

二、:根據後臺返回受影響的版本號,可控制多個版本同時被強制更新;

三、:後臺返回最新安裝包大小,本地判斷安裝包是否下載,防止多次下載;

四、:內部處理,忽略此版本更新提示

五、:library採用無第三方工具類,下載使用HttpURLConnextion,本地存儲使用SharedPrefference,以免使用此library帶來第三方插件衝突


使用方式:

Step 1. Add the JitPack repository to your build file Add it in your root build.gradle at the end of repositories:

allprojects { repositories { ... maven { url 'https://jitpack.io' } } }

Step 2. Add the dependency

dependencies {compile 'com.github.MZCretin:AutoUpdateProject:v1.0'}
dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:25.2.0'
    //this code
    compile 'com.github.MZCretin:AutoUpdateProject:v1.0'
    testCompile 'junit:junit:4.12'
}

Step 3. Init it in BaseApplication or MainActivity before using it.And then register BaseApplication in AndroidManifest(Don’t forget it).There are two ways you can chose.

//第一種形式 自定義參數 
CretinAutoUpdateUtils.Builder builder = 
        new CretinAutoUpdateUtils.Builder() 
        //設置更新api 
        .setBaseUrl("http://120.24.5.102/weixin/app/getversion") 
        //設置是否顯示忽略此版本 
        .setIgnoreThisVersion(true) 
        //設置下載顯示形式 對話框或者通知欄顯示 二選一 
        .setShowType(CretinAutoUpdateUtils.Builder.TYPE_DIALOG) 
        //設置下載時展示的圖標 
        .setIconRes(R.mipmap.ic_launcher) 
        //設置下載時展示的應用嗎 
        .setAppName("測試應用") 
        .build(); 
CretinAutoUpdateUtils.init(builder); 

//第二種模式 
//CretinAutoUpdateUtils.init("http://120.24.5.102/weixin/app/getversion");

Step 4. Add below codes to your app module’s AndroidManifest file where under tags.

<application
        android:name=".BaseApp"
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>

                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
    <!--these codes-->
        <service android:name="com.cretin.www.cretinautoupdatelibrary.utils.DownloadService"/>
    </application>

Step 5. Start using it wherever you want as below.

CretinAutoUpdateUtils.getInstance(MainActivity.this).check();

使用說明


此library的使用需要與後臺聯動配合,下面是後臺需要返回給我們使用的字段說明:

/**
 * Created by cretin on 2017/3/8.
 */

public class UpdateEntity {
    public int versionCode = 0;
    //是否強制更新 0 不強制更新 1 hasAffectCodes擁有字段強制更新 2 所有版本強制更新
    public int isForceUpdate = 0;
    //上一個版本版本號
    public int preBaselineCode = 0;
    //版本號 描述作用
    public String versionName = "";
    //新安裝包的下載地址
    public String downurl = "";
    //更新日誌
    public String updateLog = "";
    //安裝包大小 單位字節
    public String size = "";
    //受影響的版本號 如果開啓強制更新 那麼這個字段包含的所有版本都會被強制更新 格式 2|3|4
    public String hasAffectCodes = "";

    public UpdateEntity(String json) throws JSONException {
        JSONObject jsonObject = new JSONObject(json);
        this.versionCode = jsonObject.getInt("versionCode");
        this.versionName = jsonObject.getString("versionName");
        this.isForceUpdate = jsonObject.getInt("isForceUpdate");
        this.downurl = jsonObject.getString("downurl");
        this.preBaselineCode = jsonObject.getInt("preBaselineCode");
        this.updateLog = jsonObject.getString("updateLog");
        this.size = jsonObject.getString("size");
        this.hasAffectCodes = jsonObject.getString("hasAffectCodes");
    }
}java

所以需要後臺返回給我們這些字段,這些字段都是必須的,相關說明請看註釋,下面是一個參考

{
    "versionCode": "18", 
    "isForceUpdate": "1", 
    "preBaselineCode": "0", 
    "versionName": "2.1.1", 
    "downurl": "http://120.24.5.102/Webconfig/frj01_211_jiagu_sign.apk", 
    "hasAffectCodes": "11|12|13|14|15|16|17", 
    "updateLog": "1、修復bug
            2、完善部分功能點
            3、系統升級,強制更新
            ", 
    "size": 10291218
}

有什麼意見或者建議歡迎與我交流,覺得不錯歡迎Star

github地址:https://github.com/MZCretin/AutoUpdateProject


如果幫到您了,請打賞我一杯咖啡的錢!

如果幫到您了,請打賞我一杯咖啡的錢!

發佈了61 篇原創文章 · 獲贊 65 · 訪問量 20萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章