詳解Android app自動(dòng)更新總結(jié)(已適配9.0)
1.配置:
1.1 AndroidManifest.xml中添加權(quán)限和FileProvider:
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="com.fengzhi.wuyemanagement.fileprovider"
android:grantUriPermissions="true"
android:exported="false">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</provider>
1.2 新建文件(路徑:res\xml\file_paths.xml):
<paths> <external-path path="." name="external_storage_root" /> </paths>
1.3 (app的)build.gradle:
implementation "com.lzy.net:okgo:3.0.4"http://okgo 網(wǎng)絡(luò)請(qǐng)求 implementation 'com.google.code.gson:gson:2.8.2'//gson implementation "org.permissionsdispatcher:permissionsdispatcher:4.3.1"http://權(quán)限 annotationProcessor "org.permissionsdispatcher:permissionsdispatcher-processor:4.3.1"http://權(quán)限
2.這里以點(diǎn)擊按鈕進(jìn)行更新為例:
2.1 核心代碼:
private int version;
/* 更新進(jìn)度條 */
private ProgressBar mProgress;
private AlertDialog mDownloadDialog;
--------------------------------------------------------------------------------------------------------------------
//點(diǎn)擊按鈕,檢查權(quán)限,,,檢查更新的方法
@NeedsPermission({Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.REQUEST_INSTALL_PACKAGES})
protected void checkUpdate() {
showLoadingDialog("檢測(cè)更新中...");
version = AppUpdateUtil.getAppVersionCode(this);//檢查當(dāng)前版本號(hào)
// 調(diào)用方法,,,接口的具體實(shí)現(xiàn),接收傳過來的參數(shù),再調(diào)自己的方法,
requestAppUpdate(version, new DataRequestListener<UpdateAppBean>() {
@Override
public void success(UpdateAppBean data) {
// 返回的json,getStatus為0時(shí),去下載apk文件,這里是下載apk文件的方法
updateApp(data.getData().getApk_url());
}
@Override
public void fail(String msg) {
// 返回的json,getStatus為1時(shí),提示:"已是最新版本!"
SToast(msg);
dismissLoadingDialog();
}
});
}
//檢查版本號(hào),第一次請(qǐng)求(post),,,UpdateAppBean根據(jù)服務(wù)器返回生成
private void requestAppUpdate(int version, final DataRequestListener<UpdateAppBean> listener) {
OkGo.<String>post(Const.HOST_URL + Const.UPDATEAPP).params("version", version).execute(new StringCallback() {
@Override
public void onSuccess(Response<String> response) {
Gson gson = new Gson();
UpdateAppBean updateAppBean = gson.fromJson(response.body(), UpdateAppBean.class);
if (updateAppBean.getStatus() == 0) {
listener.success(updateAppBean);
} else {
listener.fail(updateAppBean.getMsg());
}
}
@Override
public void onError(Response<String> response) {
listener.fail("服務(wù)器連接失敗");
dismissLoadingDialog();
}
});
}
//如果有新版本,提示有新的版本,然后下載apk文件
private void updateApp(String apk_url) {
dismissLoadingDialog();
DialogUtils.getInstance().showDialog(this, "發(fā)現(xiàn)新的版本,是否下載更新?",
new DialogUtils.DialogListener() {
@Override
public void positiveButton() {
downloadApp(apk_url);
}
});
}
//下載apk文件并跳轉(zhuǎn)(第二次請(qǐng)求,get)
private void downloadApp(String apk_url) {
OkGo.<File>get(apk_url).tag(this).execute(new FileCallback() {
@Override
public void onSuccess(Response<File> response) {
String filePath = response.body().getAbsolutePath();
Intent intent = IntentUtil.getInstallAppIntent(mContext, filePath);
// 測(cè)試過這里必須用startactivity,不能用stratactivityforresult
mContext.startActivity(intent);
dismissLoadingDialog();
mDownloadDialog.dismiss();
mDownloadDialog=null;
}
@Override
public void downloadProgress(Progress progress) {
// showDownloadDialog();
// mProgress.setProgress((int) (progress.fraction * 100));
if (mDownloadDialog == null) {
// 構(gòu)造軟件下載對(duì)話框
AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
builder.setTitle("正在更新");
// 給下載對(duì)話框增加進(jìn)度條
final LayoutInflater inflater = LayoutInflater.from(mContext);
View v = inflater.inflate(R.layout.item_progress, null);
mProgress = (ProgressBar) v.findViewById(R.id.update_progress);
builder.setView(v);
mDownloadDialog = builder.create();
mDownloadDialog.setCancelable(false);
mDownloadDialog.show();
}
mProgress.setProgress((int) (progress.fraction * 100));
}
});
}
2.2 DataRequestListener:
public interface DataRequestListener<T> {
//請(qǐng)求成功
void success(T data);
//請(qǐng)求失敗
void fail(String msg);
}
接下來是工具類,來自github,參考, https://github.com/vondear/RxTool
2.3 AppUpdateUtil:
/**
* 獲取App版本碼
*
* @param context 上下文
* @return App版本碼
*/
public static int getAppVersionCode(Context context) {
return getAppVersionCode(context, context.getPackageName());
}
2.4 IntentUtil:
public class IntentUtil {
/**
* 獲取安裝App(支持7.0)的意圖
*
* @param context
* @param filePath
* @return
*/
public static Intent getInstallAppIntent(Context context, String filePath) {
//apk文件的本地路徑
File apkfile = new File(filePath);
if (!apkfile.exists()) {
return null;
}
Intent intent = new Intent(Intent.ACTION_VIEW);
Uri contentUri = FileUtil.getUriForFile(context, apkfile);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
}
intent.setDataAndType(contentUri, "application/vnd.android.package-archive");
return intent;
}
2.5 FileUtil:
/**
* 將文件轉(zhuǎn)換成uri(支持7.0)
*
* @param mContext
* @param file
* @return
*/
public static Uri getUriForFile(Context mContext, File file) {
Uri fileUri = null;
if (Build.VERSION.SDK_INT >= 24) {
fileUri = FileProvider.getUriForFile(mContext, mContext.getPackageName() + ".fileprovider", file);
} else {
fileUri = Uri.fromFile(file);
}
return fileUri;
}
3.遇到的問題
9.0手機(jī)authorities配置出錯(cuò),導(dǎo)致無法安裝, 解決辦法:

1.項(xiàng)目中使用了Androidx,AndroidManifest.xml的配置中就必須使用androidx的fileprovider
2.這里的authorities與FileUtil.java中的要一樣,我就是字母P大寫了導(dǎo)致錯(cuò)誤

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android開發(fā)實(shí)現(xiàn)的ViewPager引導(dǎo)頁功能(動(dòng)態(tài)加載指示器)詳解
這篇文章主要介紹了Android開發(fā)實(shí)現(xiàn)的ViewPager引導(dǎo)頁功能(動(dòng)態(tài)加載指示器),結(jié)合實(shí)例形式詳細(xì)分析了Android使用ViewPager引導(dǎo)頁的具體步驟,相關(guān)布局、功能使用技巧,需要的朋友可以參考下2017-11-11
Android實(shí)現(xiàn)TCP斷點(diǎn)上傳 后臺(tái)C#服務(wù)接收
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)TCP斷點(diǎn)上傳,后臺(tái)C#服務(wù)實(shí)現(xiàn)接收,感興趣的小伙伴們可以參考一下2016-08-08
Android編程自定義搜索框?qū)崿F(xiàn)方法【附demo源碼下載】
這篇文章主要介紹了Android編程自定義搜索框?qū)崿F(xiàn)方法,涉及Android界面布局、數(shù)據(jù)加載、事件響應(yīng)等相關(guān)操作技巧,并附帶完整demo源碼供讀者下載參考,需要的朋友可以參考下2017-12-12
屏蔽RecyclerView單邊滑動(dòng)到頭陰影(fadingEdge)的方法
這篇文章主要給大家介紹了如何屏蔽RecyclerView單邊滑動(dòng)到頭陰影(fadingEdge)的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2018-04-04
Android響應(yīng)事件onClick方法的五種實(shí)現(xiàn)方式小結(jié)
本篇文章主要介紹了Android響應(yīng)onClick方法的五種實(shí)現(xiàn)方式小結(jié),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2017-03-03
Android中使用Notification實(shí)現(xiàn)狀態(tài)欄的通知
本文主要介紹了android利用Notification實(shí)現(xiàn)狀態(tài)欄的通知的示例代碼。具有很好的參考價(jià)值。下面跟著小編一起來看下吧2017-04-04
android 使用 IJKPlayer 播放視頻流的實(shí)現(xiàn)代碼
這篇文章主要介紹了android 使用 IJKPlayer 播放視頻流,這需要借助 IAndroidIO 這個(gè)接口,也可以用于播放本地文件,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-11-11

