android app內部更新適配到8.0

app 內部跟新是app中必需要有的功能,在app出現改變時,app內部更新能以最快的速度將應用提高到最新版本。android

步驟:服務器

一、獲取本地app的版本號app

int versionCode = 0;
try {
// 獲取軟件版本號,
versionCode = this.getPackageManager().getPackageInfo(
getPackageName(), 0).versionCode;
} catch (NameNotFoundException e) {
e.printStackTrace();
}
二、上傳服務器檢查是否爲最新app,對返回的數據進行操做。若是不是最新,則作更新操做。ide

三、調用android手機的DownLoadManager進行apk的下載ui

request = new DownloadManager.Request(Uri.parse(url));
// 設置通知欄標題
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
request.setTitle("一儒快遞");//
request.setDescription("一儒快遞開始下載");
request.setAllowedOverRoaming(false);
request.allowScanningByMediaScanner();// 可被媒體掃描到
request.setVisibleInDownloadsUi(true);// 可見和管理
File dir = getApplication().getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS);
filePath = dir.toString() + "/" + "insurework" + getVersionCode() + ".apk";
// 設置文件存放目錄
request.setDestinationInExternalFilesDir(this, Environment.DIRECTORY_DOWNLOADS, "insurework" + getVersionCode() + ".apk");
did = downManager.enqueue(request);
須要獲取下載的狀態,因此須要在初始化的時候註冊廣播監聽系統下載完成ACTION,注意:在銷燬的時候this

IntentFilter filter = new IntentFilter();
filter.addAction(DownloadManager.ACTION_DOWNLOAD_COMPLETE);
filter.addAction(DownloadManager.ACTION_NOTIFICATION_CLICKED);
receiver = new DownLoadCompleteReceiver();
registerReceiver(receiver, filter);
@Override
protected void onDestroy() {
super.onDestroy();
unregisterReceiver(receiver);
}
自定義的廣播接收者,在接收者中做版本適配
private class DownLoadCompleteReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction() != null && intent.getAction().equals(DownloadManager.ACTION_DOWNLOAD_COMPLETE)) {
long downid = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1);
if (did == downid) {// 若是下載完畢,
try {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
//判讀版本是否在7.0以上(7.0及以後的自動安裝須要ContentPrivide來獲取到下載的文件)
installApk();
} else if (VERSION.SDK_INT >= VERSION_CODES.O) {
boolean b = getPackageManager().canRequestPackageInstalls();
if (b) {
installApk();
} else {
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.REQUEST_INSTALL_PACKAGES}, Constent.NEW_8_0_REQUEST);
}
} else {
//7.0之前的啓動方法
Intent install = new Intent(Intent.ACTION_VIEW);
install.setDataAndType(downManager.getUriForDownloadedFile(downid), "application/vnd.android.package-archive");
install.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(install);
}
} catch (Exception e) {
System.out.println(e.getMessage().toString().trim());
}
}
} else if (intent.getAction().equals(
DownloadManager.ACTION_NOTIFICATION_CLICKED)) {
// 點擊事件
System.out.println("ACTION_NOTIFICATION_CLICKED");
}
}
}
其中8.0系統須要申請權限url

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == Constent.NEW_8_0_REQUEST) {
installApk();
}
}
7.0以上自動安裝邏輯
private void installApk() {
Uri apkUri = FileProvider.getUriForFile(context, "你的包名.fileprovider", new File(filePath));//在AndroidManifest中的android:authorities值
Intent install = new Intent(Intent.ACTION_VIEW);
install.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
install.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
install.setDataAndType(apkUri, "application/vnd.android.package-archive");
startActivity(install);
}
內容提供者:res下新建xml文件夾,新建file_paths.xml文件.net

<?xml version="1.0" encoding="utf-8"?>
<resources>
<paths>
<external-path
name="download"
path=""/>
</paths>
</resources>
在AndroidManifest.xml設置Providexml

<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="你的包名.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths"/>
</provider>
結束。
---------------------
做者:han_gao
來源:CSDN
原文:https://blog.csdn.net/yikunhan/article/details/80527884
版權聲明:本文爲博主原創文章,轉載請附上博文連接!blog

相關文章
相關標籤/搜索