Android app 在線更新那點事兒(適配Android6.0、7.0、8.0)


一、前言

app在線更新是一個比較常見需求,新版本發布時,用戶進入我們的app,就會彈出更新提示框,第一時間更新新版本app。在線更新分為以下幾個步驟:

 1, 通過接口獲取線上版本號,versionCode
 2, 比較線上的versionCode 和本地的versionCode,彈出更新窗口
 3, 下載APK文件(文件下載)
 4,安裝APK

在線更新就上面幾個步驟,前2步比較簡單,重要的就是后2個步驟,而由於Android 各個版本對權限和隱私的收歸和保護,因此,會出現各種的適配問題,因此本文就總結一下app 在線更新方法和遇到的一些適配問題。

二、apk 下載

apk下載其實就是文件下載,而文件下載有很多方式:

  1,很多三方框架都有文件上傳下載功能,可以借助三方框架(比如Volley,OkHttp)
  2,也可以開啟一個線程去下載,(可以用IntentService)
  3,最簡單的一種方式:Android SDK 其實給我們提供了下載類DownloadManager,只需要簡單的配置項設置,就能輕松實現下載功能。

本文就用第三種方式,用 DownloadManager 來下載apk。

1. 使用DownloadManager 下載apk

DownloadManager 是SDK 自帶的,大概流程如下:

(1)創建一個Request,進行簡單的配置(下載地址,和文件保存地址等)
(2)下載完成后,系統會發送一個下載完成的廣播,我們需要監聽廣播。
(3)監聽到下載完成的廣播后,根據id查找下載的apk文件
(4)在代碼中執行apk安裝。

public void downloadApk(String apkUrl, String title, String desc) { // fix bug : 裝不了新版本,在下載之前應該刪除已有文件 File apkFile = new File(weakReference.get().getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS), "test.apk"); if (apkFile != null && apkFile.exists()) { apkFile.delete(); } DownloadManager.Request request = new DownloadManager.Request(Uri.parse(apkUrl)); //設置title request.setTitle(title); // 設置描述 request.setDescription(desc); // 完成后顯示通知欄 request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED); request.setDestinationInExternalFilesDir(weakReference.get(), Environment.DIRECTORY_DOWNLOADS, "test.apk"); //在手機SD卡上創建一個download文件夾 // Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).mkdir() ; //指定下載到SD卡的/download/my/目錄下 // request.setDestinationInExternalPublicDir("/codoon/","test.apk"); request.setMimeType("application/vnd.android.package-archive"); //記住reqId mReqId = mDownloadManager.enqueue(request); } 

如上代碼所示,首先構建一個Request,設置下載地址,標題、描述、apk存放目錄等,最后,調用mDownloadManager.enqueue(request) 開始下載。

注意:這里我們需要記住這個mReqId,因為下載完成之后,我們需要根據這個ID 去查找apk文件,然后安裝apk.

2.更新下載進度

下載文件,我們一般需要知道下載的進度,在界面給用戶一個友好的提示,app 更新也是一樣,我們需要在界面上顯示當前下載進度和總進度,讓用戶知道大概會等待多久。那么如果獲取下載進度呢?

在下載之前,我們需要在Activity 中注冊一個Observer,就是一個觀察者,當下載進度變化的時候,就會通知觀察者,從而更新進度。步驟如下:

1, 首先我們先定義一個觀察者DownloadChangeObserver來觀察下載進度
2,在DownloadChangeObserver 中更新UI進度,給用戶提示
3,下載之前,在Activity 中注冊Observer

具體代碼如下:
DownloadChangeObserver.class:

 class DownloadChangeObserver extends ContentObserver { /** * Creates a content observer. * * @param handler The handler to run {@link #onChange} on, or null if none. */ public DownloadChangeObserver(Handler handler) { super(handler); } @Override public void onChange(boolean selfChange) { super.onChange(selfChange); updateView(); } } 

updateView()方法中,查詢下載進度。

 private void updateView() { int[] bytesAndStatus = new int[]{0, 0, 0}; DownloadManager.Query query = new DownloadManager.Query().setFilterById(mReqId); Cursor c = null; try { c = mDownloadManager.query(query); if (c != null && c.moveToFirst()) { //已經下載的字節數 bytesAndStatus[0] = c.getInt(c.getColumnIndexOrThrow(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR)); //總需下載的字節數 bytesAndStatus[1] = c.getInt(c.getColumnIndexOrThrow(DownloadManager.COLUMN_TOTAL_SIZE_BYTES)); //狀態所在的列索引 bytesAndStatus[2] = c.getInt(c.getColumnIndex(DownloadManager.COLUMN_STATUS)); } } finally { if (c != null) { c.close(); } } if (mUpdateListener != null) { mUpdateListener.update(bytesAndStatus[0], bytesAndStatus[1]); } Log.i(TAG, "下載進度:" + bytesAndStatus[0] + "/" + bytesAndStatus[1] + ""); } 

根據前面我們記錄的ID去查詢進度,代碼中已經注釋了,不再多講。

要想獲取到進度,在下載之前,還得先注冊DownloadChangeObserver,代碼如下:

weakReference.get().getContentResolver().registerContentObserver(Uri.parse("content://downloads/my_downloads"), true, mDownLoadChangeObserver); 

3. 獲取下載結果

DownloadManager在下載完成之后,會發送一個下載完成的廣播DownloadManager.ACTION_DOWNLOAD_COMPLETE,我們只需要監聽這個廣播,收到廣播后, 獲取apk文件安裝。

定義一個廣播DownloadReceiver

class DownloadReceiver extends BroadcastReceiver { @Override public void onReceive(final Context context, final Intent intent) { // 安裝APK long completeDownLoadId = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1); Logger.e(TAG, "收到廣播"); Uri uri; Intent intentInstall = new Intent(); intentInstall.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); intentInstall.setAction(Intent.ACTION_VIEW); if (completeDownLoadId == mReqId) { uri = mDownloadManager.getUriForDownloadedFile(completeDownLoadId); } intentInstall.setDataAndType(uri, "application/vnd.android.package-archive"); context.startActivity(intentInstall); } } 

在下載之前注冊廣播

 // 注冊廣播,監聽APK是否下載完成 weakReference.get().registerReceiver(mDownloadReceiver, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE)); 

通過上面的幾個步驟,基本上就完成app在線更新功能,在Android 6.0以下可以正常運行。但是別忙,本文還沒有結束,Android每一個版本都有一些改動,導致我們需要適配不同的版本,不然的話,就會出問題,結下來就看一下Android 6.0,7.0,8.0 的相關適配。

三、適配Android 6.0

通過前面講的幾個步驟,app 在線更新在6.0以下已經可以正常運行,在Android6.0上,安裝的時候會報出以下錯誤:


 
Caused by:
5 android.content.ActivityNotFoundException:No Activity found to handle Intent { act=android.intent.action.VIEW typ=application/vnd.android.package-archive flg=0x10000000 } 

為什么會報上面的錯誤,經過debug發現,在Android6.0以下和Android6.0上,通過DownloadManager 獲取到的Uri不一樣。

區別如下:(1)Android 6.0,getUriForDownloadedFile得到 值為: content://downloads/my_downloads/10
(2) Android6.0以下,getUriForDownloadedFile得到的值為:file:///storage/emulated/0/Android/data/packgeName/files/Download/xxx.apk

可以看到,Android6.0得到的apk地址為:content:// 開頭的一個地址,安裝的時候就會報上面的錯誤。怎么解決呢?經過查找資料找到了解決辦法:

   //通過downLoadId查詢下載的apk,解決6.0以后安裝的問題 public static File queryDownloadedApk(Context context, long downloadId) { File targetApkFile = null; DownloadManager downloader = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE); if (downloadId != -1) { DownloadManager.Query query = new DownloadManager.Query(); query.setFilterById(downloadId); query.setFilterByStatus(DownloadManager.STATUS_SUCCESSFUL); Cursor cur = downloader.query(query); if (cur != null) { if (cur.moveToFirst()) { String uriString = cur.getString(cur.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI)); if (!TextUtils.isEmpty(uriString)) { targetApkFile = new File(Uri.parse(uriString).getPath()); } } cur.close(); } } return targetApkFile; } 

代碼如上所示,不通過getUriForDownloadedFile去獲取Uri,通過DownloadManager.COLUMN_LOCAL_URI 這個字段去獲取apk地址。

適配Android 6.0后,安裝apk 的代碼如下:

 /** * @param context * @param intent */ private void installApk(Context context, Intent intent) { long completeDownLoadId = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1); Logger.e(TAG, "收到廣播"); Uri uri; Intent intentInstall = new Intent(); intentInstall.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); intentInstall.setAction(Intent.ACTION_VIEW); if (completeDownLoadId == mReqId) { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) { // 6.0以下 uri = mDownloadManager.getUriForDownloadedFile(completeDownLoadId); } else if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) { // 6.0 - 7.0 File apkFile = queryDownloadedApk(context, completeDownLoadId); uri = Uri.fromFile(apkFile); } // 安裝應用 Logger.e("zhouwei", "下載完成了"); intentInstall.setDataAndType(uri, "application/vnd.android.package-archive"); context.startActivity(intentInstall); } } 
四、適配Android 7.0

剛適配完6.0,在7.0以上的機子上又出問題了,為什么呢?因為在Android 7.0上,對文件的訪問權限作出了修改,不能在使用file://格式的Uri 訪問文件 ,Android 7.0提供 FileProvider,應該使用這個來獲取apk地址,然后安裝apk。如下進行簡單的適配:

(1) 在res 目錄下,新建一個xml 文件夾,在xml 下面創建一個文件provider_paths文件:

<?xml version="1.0" encoding="utf-8"?> <paths> <external-path name="external" path="" /> <external-files-path name="Download" path="" /> </paths> 

(2) 在AndroidManifest.xml清單文件中申明Provider:

<!-- Android 7.0 照片、APK下載保存路徑--> <provider android:name="android.support.v4.content.FileProvider" android:authorities="packgeName.fileProvider" android:exported="false" android:grantUriPermissions="true"> <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/provider_paths" /> </provider> 

(3) Android 7.0上的文件地址獲取:

 uri = FileProvider.getUriForFile(context,
                        "packageNam.fileProvider", new File(context.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS), "xxx.apk")); 

好了,就這樣7.0適配工作就完成了,適配后的安裝代碼如下:

 /** * @param context * @param intent */ private void installApk(Context context, Intent intent) { long completeDownLoadId = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1); Logger.e(TAG, "收到廣播"); Uri uri; Intent intentInstall = new Intent(); intentInstall.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); intentInstall.setAction(Intent.ACTION_VIEW); if (completeDownLoadId == mReqId) { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) { // 6.0以下 uri = mDownloadManager.getUriForDownloadedFile(completeDownLoadId); } else if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) { // 6.0 - 7.0 File apkFile = queryDownloadedApk(context, completeDownLoadId); uri = Uri.fromFile(apkFile); } else { // Android 7.0 以上 uri = FileProvider.getUriForFile(context, "packgeName.fileProvider", new File(context.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS), "xxx.apk")); intentInstall.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION); } // 安裝應用 Logger.e("zhouwei", "下載完成了"); intentInstall.setDataAndType(uri, "application/vnd.android.package-archive"); context.startActivity(intentInstall); } } 

注意:把上面的packageNam 換成你自己的包名,把xxx.apk 換成你自己的apk的名字。
關於更多FileProvider的東西,這兒就不展開講了,想要了解的可以看一下鴻洋的文章:Android 7.0 行為變更 通過FileProvider在應用間共享文件吧
,講的很清楚。

五、適配Android 8.0:未知來源的應用權限

好特么累,繼續適配Android 8.0, 由於沒有Android 8.0的手機,一直沒有注意,前些天一個華為用戶反饋在線更新不了新版本,具體表現就是:apk下載完成,一閃而過,沒有跳轉到apk安裝界面。經過排查,確定了是Android 8.0權限問題。

Android8.0以上,未知來源的應用是不可以通過代碼來執行安裝的(在sd卡中找找到apk,手動安裝是可以的),未知應用安裝權限的開關被除掉,取而代之的是未知來源應用的管理列表,需要列表里面開啟你的應用的未知來源的安裝權限。Google這么做是為了防止一開始正經的應用后來開始通過升級來做一些不合法的事情,侵犯用戶權益。

知道問題了,我們就適配吧:

(1) 在清單文件中申明權限:REQUEST_INSTALL_PACKAGES

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

(2) 在代碼中判斷用戶是否已經受過權限了,如果已經授權,可以直接安裝,如果沒有授權,則跳轉到授權列表,讓用戶開啟未知來源應用安裝權限,開啟后,再安裝應用。

在監聽apk下載狀態的廣播中添加如下代碼:

boolean haveInstallPermission; // 兼容Android 8.0 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { //先獲取是否有安裝未知來源應用的權限 haveInstallPermission = context.getPackageManager().canRequestPackageInstalls(); if (!haveInstallPermission) {//沒有權限 // 彈窗,並去設置頁面授權 final AndroidOInstallPermissionListener listener = new AndroidOInstallPermissionListener() { @Override public void permissionSuccess() { installApk(context, intent); } @Override public void permissionFail() { ToastUtils.shortToast(context, "授權失敗,無法安裝應用"); } }; AndroidOPermissionActivity.sListener = listener; Intent intent1 = new Intent(context, AndroidOPermissionActivity.class); context.startActivity(intent1); } else { installApk(context, intent); } } else { installApk(context, intent); } 

因為授權時需要彈框提示,我們用一個Activity來代理創建了一個Activity:AndroidOPermissionActivity 來申請權限,用戶點擊設置后,跳轉到權限設置界面,然后我們再onActivityResult 里判斷是都授權成功。

AndroidOPermissionActivity 代碼如下:

/** * 兼容Android 8。0 APP 在線更新,權限申請界面 * Created by zhouwei on 2018/3/23. */ public class AndroidOPermissionActivity extends BaseActivity { public static final int INSTALL_PACKAGES_REQUESTCODE = 1; private AlertDialog mAlertDialog; public static AppDownloadManager.AndroidOInstallPermissionListener sListener; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); // 彈窗 if (Build.VERSION.SDK_INT >= 26) { ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.REQUEST_INSTALL_PACKAGES}, INSTALL_PACKAGES_REQUESTCODE); } else { finish(); } } @RequiresApi(api = Build.VERSION_CODES.O) @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { super.onRequestPermissionsResult(requestCode, permissions, grantResults); switch (requestCode) { case INSTALL_PACKAGES_REQUESTCODE: if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { if (sListener != null) { sListener.permissionSuccess(); finish(); } } else { //startInstallPermissionSettingActivity(); showDialog(); } break; } } private void showDialog() { AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle(R.string.app_name); builder.setMessage("為了正常升級 xxx APP,請點擊設置按鈕,允許安裝未知來源應用,本功能只限用於 xxx APP版本升級"); builder.setPositiveButton("設置", new DialogInterface.OnClickListener() { @RequiresApi(api = Build.VERSION_CODES.O) @Override public void onClick(DialogInterface dialogInterface, int i) { startInstallPermissionSettingActivity(); mAlertDialog.dismiss(); } }); builder.setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { if (sListener != null) { sListener.permissionFail(); } mAlertDialog.dismiss(); finish(); } }); mAlertDialog = builder.create(); mAlertDialog.show(); } @RequiresApi(api = Build.VERSION_CODES.O) private void startInstallPermissionSettingActivity() { //注意這個是8.0新API Intent intent = new Intent(Settings.ACTION_MANAGE_UNKNOWN_APP_SOURCES, Uri.parse("package:" + getPackageName())); startActivityForResult(intent, 1); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == 1 && resultCode == RESULT_OK) { // 授權成功 if (sListener != null) { sListener.permissionSuccess(); } } else { // 授權失敗 if (sListener != null) { sListener.permissionFail(); } } finish(); } @Override protected void onDestroy() { super.onDestroy(); sListener = null; } } 

注意:當通過Intent 跳轉到未知應用授權列表的時候,一定要加上包名,這樣就能直接跳轉到你的app下,不然只能跳轉到列表。


 

好了,這樣Android 8.0 上也可以在線更新了。

六、完整代碼,封裝了一個類AppDownloadManager

為了不依賴於某個Activity ,因此封裝了一個AppDownloadManager
,少量幾行代碼就可以實現在線更新,給出完整代碼:

public class AppDownloadManager { public static final String TAG = "AppDownloadManager"; private WeakReference<Activity> weakReference; private DownloadManager mDownloadManager; private DownloadChangeObserver mDownLoadChangeObserver; private DownloadReceiver mDownloadReceiver; private long mReqId; private OnUpdateListener mUpdateListener; public AppDownloadManager(Activity activity) { weakReference = new WeakReference<Activity>(activity); mDownloadManager = (DownloadManager) weakReference.get().getSystemService(Context.DOWNLOAD_SERVICE); mDownLoadChangeObserver = new DownloadChangeObserver(new Handler()); mDownloadReceiver = new DownloadReceiver(); } public void setUpdateListener(OnUpdateListener mUpdateListener) { this.mUpdateListener = mUpdateListener; } public void downloadApk(String apkUrl, String title, String desc) { // fix bug : 裝不了新版本,在下載之前應該刪除已有文件 File apkFile = new File(weakReference.get().getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS), "app_name.apk"); if (apkFile != null && apkFile.exists()) { apkFile.delete(); } DownloadManager.Request request = new DownloadManager.Request(Uri.parse(apkUrl)); //設置title request.setTitle(title); // 設置描述 request.setDescription(desc); // 完成后顯示通知欄 request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED); request.setDestinationInExternalFilesDir(weakReference.get(), Environment.DIRECTORY_DOWNLOADS, "app_name.apk"); //在手機SD卡上創建一個download文件夾 // Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).mkdir() ; //指定下載到SD卡的/download/my/目錄下 // request.setDestinationInExternalPublicDir("/codoon/","codoon_health.apk"); request.setMimeType("application/vnd.android.package-archive"); // mReqId = mDownloadManager.enqueue(request); } /** * 取消下載 */ public void cancel() { mDownloadManager.remove(mReqId); } /** * 對應 {@link Activity } */ public void resume() { //設置監聽Uri.parse("content://downloads/my_downloads") weakReference.get().getContentResolver().registerContentObserver(Uri.parse("content://downloads/my_downloads"), true, mDownLoadChangeObserver); // 注冊廣播,監聽APK是否下載完成 weakReference.get().registerReceiver(mDownloadReceiver, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE)); } /** * 對應{@link Activity#onPause()} ()} */ public void onPause() { weakReference.get().getContentResolver().unregisterContentObserver(mDownLoadChangeObserver); weakReference.get().unregisterReceiver(mDownloadReceiver); } private void updateView() { int[] bytesAndStatus = new int[]{0, 0, 0}; DownloadManager.Query query = new DownloadManager.Query().setFilterById(mReqId); Cursor c = null; try { c = mDownloadManager.query(query); if (c != null && c.moveToFirst()) { //已經下載的字節數 bytesAndStatus[0] = c.getInt(c.getColumnIndexOrThrow(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR)); //總需下載的字節數 bytesAndStatus[1] = c.getInt(c.getColumnIndexOrThrow(DownloadManager.COLUMN_TOTAL_SIZE_BYTES)); //狀態所在的列索引 bytesAndStatus[2] = c.getInt(c.getColumnIndex(DownloadManager.COLUMN_STATUS)); } } finally { if (c != null) { c.close(); } } if (mUpdateListener != null) { mUpdateListener.update(bytesAndStatus[0], bytesAndStatus[1]); } Log.i(TAG, "下載進度:" + bytesAndStatus[0] + "/" + bytesAndStatus[1] + ""); } class DownloadChangeObserver extends ContentObserver { /** * Creates a content observer. * * @param handler The handler to run {@link #onChange} on, or null if none. */ public DownloadChangeObserver(Handler handler) { super(handler); } @Override public void onChange(boolean selfChange) { super.onChange(selfChange); updateView(); } } class DownloadReceiver extends BroadcastReceiver { @Override public void onReceive(final Context context, final Intent intent) { boolean haveInstallPermission; // 兼容Android 8.0 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { //先獲取是否有安裝未知來源應用的權限 haveInstallPermission = context.getPackageManager().canRequestPackageInstalls(); if (!haveInstallPermission) {//沒有權限 // 彈窗,並去設置頁面授權 final AndroidOInstallPermissionListener listener = new AndroidOInstallPermissionListener() { @Override public void permissionSuccess() { installApk(context, intent); } @Override public void permissionFail() { ToastUtils.shortToast(context, "授權失敗,無法安裝應用"); } }; AndroidOPermissionActivity.sListener = listener; Intent intent1 = new Intent(context, AndroidOPermissionActivity.class); context.startActivity(intent1); } else { installApk(context, intent); } } else { installApk(context, intent); } } } /** * @param context * @param intent */ private void installApk(Context context, Intent intent) { long completeDownLoadId = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1); Logger.e(TAG, "收到廣播"); Uri uri; Intent intentInstall = new Intent(); intentInstall.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); intentInstall.setAction(Intent.ACTION_VIEW); if (completeDownLoadId == mReqId) { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) { // 6.0以下 uri = mDownloadManager.getUriForDownloadedFile(completeDownLoadId); } else if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) { // 6.0 - 7.0 File apkFile = queryDownloadedApk(context, completeDownLoadId); uri = Uri.fromFile(apkFile); } else { // Android 7.0 以上 uri = FileProvider.getUriForFile(context, "package_name.fileProvider", new File(context.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS), "app_name.apk")); intentInstall.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION); } // 安裝應用 Logger.e("zhouwei", "下載完成了"); intentInstall.setDataAndType(uri, "application/vnd.android.package-archive"); context.startActivity(intentInstall); } } //通過downLoadId查詢下載的apk,解決6.0以后安裝的問題 public static File queryDownloadedApk(Context context, long downloadId) { File targetApkFile = null; DownloadManager downloader = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE); if (downloadId != -1) { DownloadManager.Query query = new DownloadManager.Query(); query.setFilterById(downloadId); query.setFilterByStatus(DownloadManager.STATUS_SUCCESSFUL); Cursor cur = downloader.query(query); if (cur != null) { if (cur.moveToFirst()) { String uriString = cur.getString(cur.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI)); if (!TextUtils.isEmpty(uriString)) { targetApkFile = new File(Uri.parse(uriString).getPath()); } } cur.close(); } } return targetApkFile; } public interface OnUpdateListener { void update(int currentByte, int totalByte); } public interface AndroidOInstallPermissionListener { void permissionSuccess(); void permissionFail(); } } 

使用就很簡單了,如下:

(1) 彈出更新提示框:提示用戶更新

 private void showUpdateDialog(final AppUpdateInfo updateInfo) { AppUpdateDialog dialog = new AppUpdateDialog(getContext()); dialog.setAppUpdateInfo(updateInfo); dialog.setOnUpdateClickListener(new AppUpdateDialog.OnUpdateClickListener() { @Override public void update(final AppUpdateDialog updateDialog) { String title = "app name"; String desc = "版本更新"; mDownloadManager.setUpdateListener(new AppDownloadManager.OnUpdateListener() { @Override public void update(int currentByte, int totalByte) { updateDialog.setProgress(currentByte, totalByte); if ((currentByte == totalByte) && totalByte != 0) { updateDialog.dismiss(); } } }); mDownloadManager.downloadApk(updateInfo.download_url, title, desc); } }); dialog.setCanceledOnTouchOutside(false); dialog.setCancelable(false); dialog.show(); } 

(2) 注意在 onResume 和 onPause 調用對應方法:

 @Override public void onResume() { super.onResume(); if (mDownloadManager != null) { mDownloadManager.resume(); } } 
 @Override public void onPause() { super.onPause(); if (mDownloadManager != null) { mDownloadManager.onPause(); } } 

[圖片上傳失敗...(image-d8b11a-1524880080337)]

七、總結

本文總結了項目中app在線更新遇到的一些適配問題,關於Android 6.0 的適配,如果你沒有使用DownloadManager,可能不會遇到這個問題。7.0 和 8.0 的適配不管用哪種方式,都會有。

關於app在線更新版本適配就此結束,如果有啥問題,歡迎指出。

鏈接:https://www.jianshu.com/p/85913ed97af5 ,轉載請注明來源

如果對技術開發的可以參考下面的文章

微信小程序開發<一>

微信小程序開發<二>

這個時代,作為程序員,我什么要學習小程序

來開發一個wanandroid快應用吧

“大話架構”阿里架構師分享的Java程序員需要突破的技術要點

 

技術


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM