android7.0下載更新失敗后,抽時間就封裝整理了一下,簡單的實現(xiàn)了多種更新的方式。反正總是要用到的就簡單的記錄一下,方便以后的用的時候也好折騰型雳。
可以通過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)的話只實現(xiàn)了系統(tǒng)DownloadManager,和okhttp的下載方式嘉涌。可以直接更改DownLoadRunnable里面自己定義需要的下載方式夸浅。
加入了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