android 軟件下載更新,簡單的實(shí)現(xiàn)多種更新方式

android7.0下載更新失敗后,抽時間就封裝整理了一下,簡單的實(shí)現(xiàn)了多種更新的方式。反正總是要用到的就簡單的記錄一下,方便以后的用的時候也好折騰。


功能界面.png

更新提示界面.png
可以通過type參數(shù)指定更新方式,方便更改.可以通過return 結(jié)果進(jìn)行動態(tài)權(quán)限的判斷。接著調(diào)用start就可以了.
UpdateManager.getInstance().checkUpdate(this, 2, url, mssage, 0);
UpdateManager.getInstance().start();
    /**
     * @param versionCode   最新的版本號
     * @param url           apk下載鏈接
     * @param updateMessage 更新內(nèi)容
     * @param type          0:引導(dǎo)更新,1:靜默更新,2:強(qiáng)制更新:3:顯示Notification進(jìn)度更新
     */
    public boolean checkUpdate(Context context, int versionCode, String url, String updateMessage, int type) {
        this.context = context;
        this.newVersionCode = versionCode;
        this.type = type;
        this.url = url;
        if (!TextUtils.isEmpty(updateMessage)) {
            this.updateMessage = updateMessage;
        }
        return haveNewVersion = versionCode > getVersionCode();
    }

    public void start() {
        if (!haveNewVersion) {
            return;
        }
        init();
        //檢測是否已下載
        if (checkDownload(newVersionCode)) {
            isDownload = true;
        } else {
            isDownload = false;
        }
        if (type == NO_PROGRESS_MODE && !isDownload) {
            downloadFile();
        } else {
            showDialog();
        }
    }

默認(rèn)的話只實(shí)現(xiàn)了系統(tǒng)DownloadManager,和okhttp的下載方式??梢灾苯痈腄ownLoadRunnable里面自己定義需要的下載方式。

加入了Android7.0 FileProvider的判斷,記得AndroidManifest.xml加入provider的配置。如果其他三方也是用了FileProvider,可以三方的合并配置到一起。當(dāng)然不嫌麻煩也可以自定義FileProvider,新添加一個配置。

   /**
     * 安裝apk
     *
     * @param context 上下文
     * @param file    APK文件
     */
    private void installApk(Context context, File file) {

        Intent intent = new Intent();
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.addCategory(Intent.CATEGORY_DEFAULT);
        intent.setAction(Intent.ACTION_VIEW);
        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.M) {
            intent.setDataAndType(FileProvider.getUriForFile(context, context.getPackageName() + ".file.provider", file), "application/vnd.android.package-archive");
            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        } else {
            intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");
        }
        context.startActivity(intent);
    }
  <provider
            android:name="android.support.v4.content.FileProvider"
            android:authorities="${applicationId}.file.provider"
            android:exported="false"
            android:grantUriPermissions="true">

            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/download_file_provider" />

        </provider>
<paths>
    <cache-path
        name="internal"
        path="boxing" />
<!--外部內(nèi)存卡根目錄-->
    <external-path
        name="external"
        path="DCIM/bili/boxing" />
    <cache-path
        name="cacheUpdata"
        path="updata" />
    <external-path
        name="externalUpdata"
        path="updata" />
    <external-files-path
        name="externalFilesUpdata"
        path="updata" />
</paths>
路徑間的簡單對應(yīng)關(guān)系
    <root-path/> 代表設(shè)備的根目錄new File("/");
    <files-path/> 代表context.getFilesDir()//內(nèi)部存儲空間//data/data/<package name >/files/
    <cache-path/> 代表context.getCacheDir()//內(nèi)部存儲空間//data/data/<package name >/cach/
    <external-path/> 代表Environment.getExternalStorageDirectory()//外部存儲 /mnt/sdcard/
    <external-files-path>代表context.getExternalFilesDirs()//外部存儲 /mnt/sdcard/Android/data/<packge name>/files/
    <external-cache-path>代表getExternalCacheDirs()//外部存儲 /mnt/sdcard/Android/data/<packge name>/cach/

還有為了兼容android 8.0需要添加未知來源安裝權(quán)限

  <uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />

最后附上github地址:https://github.com/starrysky0/UpdateApkUtils

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容