APK下載完成後,點選提示無法開啟,如何解決
近期在做一個專案時,實現了掃碼下載的功能,其基本流程如下:
1.集成了zxing的掃描庫,當識別了二維碼後,跳轉到一個WebView頁面。
2.設定此WebView的的下載監聽器,如下。
3.呼叫Android系統的DownloadManager進行下載動作,並在通知欄顯示下載進度。
// 設定WebView下載監聽器 wvDetail.setDownloadListener(new DownloadListener() { @Override public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimeType, long contentLength) { showWillDownloadDialog(url, contentDisposition, mimeType); } });
做了以上三步後,掃碼下載的功能基本實現了。
我通過應用寶,百度手機助手等獲取一些App的下載地址,並通過草料二維碼網站生成相應的二維碼,再通過我們的掃碼下載功能進行掃描下載。經過測試發現,這些Android市場的下載連結都能正常下載,下載完成後,點選通知欄,都能正常彈出安裝頁面,但是,在公司一些平臺上託管的App,我們掃描相應的二維碼,也能正常下載,並在通知欄顯示下載進度,下載完成,點選通知欄,彈出“無法開啟”的提示語。
這是怎麼回事?為什麼應用寶,百度手機助手的App,掃碼下載完成後點選能正常彈出安裝頁面,有些就不行呢?
沒關係!有問題並可怕,我們都能解決掉。
手機連上電腦,復現“無法開啟”的問題,仔細檢視Android Studio控制檯列印的系統log,發現有下面的log:
I/ActivityManager: START u0 {act=android.intent.action.VIEW dat=content://com.android.providers.downloads.documents/document/21 typ=application/octet-stream flg=0x10000003} from uid 10017 08-14 11:13:19.394 16343-16362/android.process.media W/DownloadManager: Failed to start Intent { act=android.intent.action.VIEW dat=content://com.android.providers.downloads.documents/document/21 typ=application/octet-stream flg=0x10000003 }: android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=content://com.android.providers.downloads.documents/document/21 typ=application/octet-stream flg=0x10000003 }
上面錯誤log的大體意思是:type=application/octet-stream,action=android.intent.action.VIEW,dat=ofollow,noindex">content://com.android.providers.downloads.documents/document/21 ,無法找到相應的系統Activity來進行相應的操作。
找到了問題的癥結所在,接下來就要想辦法解決。
因為我們下載的apk檔案,要想能點選彈出安裝頁面,就需要將mimeType設定為mimeType = "application/vnd.android.package-archive",所以,我們可以在呼叫系統DownloadManager進行下載時,進行相應的判斷和處理,具體實現如下:
/** * 呼叫系統下載器進行下載 * * @param url * @param contentDisposition * @param mimeType */ private void downloadBySystem(String url, String contentDisposition, String mimeType) { // 指定下載地址 DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url)); // 允許媒體掃描,根據下載的檔案型別被加入相簿、音樂等媒體庫 request.allowScanningByMediaScanner(); // 設定通知的顯示型別,下載進行時和完成後顯示通知 request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED); // 允許在計費流量下下載 //request.setAllowedOverMetered(false); // 允許該記錄在下載管理介面可見 request.setVisibleInDownloadsUi(false); // 允許漫遊時下載 request.setAllowedOverRoaming(true); // 允許下載的網路型別 //request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI); // 設定下載檔案儲存的路徑和檔名 String fileName= URLUtil.guessFileName(url, contentDisposition, mimeType); LogUtil.w("fileName:{}" + fileName); if (fileName != null && fileName.endsWith(ConstantUtil.FILE_EXTENSION_NAME) && SPECIAL_MIME_TYPE.equals(mimeType)) { mimeType = "application/vnd.android.package-archive"; } request.setMimeType(mimeType); request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, fileName); //另外可選一下方法,自定義下載路徑 //request.setDestinationUri() //request.setDestinationInExternalFilesDir(ContextUtil.getAppContext(), ContextUtil.getAppContext().getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS).getAbsolutePath(), fileName); final DownloadManager downloadManager = (DownloadManager) getSystemService(DOWNLOAD_SERVICE); // 新增一個下載任務 long downloadId = downloadManager.enqueue(request); LogUtil.w("downloadId:{}" + downloadId); }
經過在Android7.1,Android8.1反覆驗證,問題解決。
熱烈歡迎大家積極留言反饋,小編會為大家不斷提供更加優質的技術乾貨。