/** * 获取apk保存的uri * @param downloadId * @return */ public Uri getDownloadUri(long downloadId) { return downloadManager.getUriForDownloadedFile(downloadId); }
@Override public void onReceive(Context context, Intent intent) { if (intent.getAction().equals(DownloadManager.ACTION_DOWNLOAD_COMPLETE)) { Uri uri = mDownloadManager.getUriForDownloadedFile(id); imageView.setImageURI(uri); } else if (intent.getAction().equals(DownloadManager.ACTION_NOTIFICATION_CLICKED)) { Toast.makeText(context, "Clicked", Toast.LENGTH_SHORT).show(); } } }
private void installApk(Context context, long downloadId) { DownloadManager downloadManager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE); Intent intent = new Intent(Intent.ACTION_VIEW); Uri fileUri = downloadManager.getUriForDownloadedFile(downloadId); if (null != fileUri) { LogUtils.d(TAG, fileUri.toString()); intent.setDataAndType(fileUri, "application/vnd.android.package-archive"); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.startActivity(intent); } else { LogUtils.d(TAG, "无APK,下载失败"); } } }
@Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(action)) { if (mDownloadManager != null) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { Uri downloadedUri = mDownloadManager.getUriForDownloadedFile(mLastDownloadId); String mimeType = mDownloadManager.getMimeTypeForDownloadedFile(mLastDownloadId); new NotifyDownloadedTask().execute(downloadedUri.toString(), mimeType); } } } else if (DownloadManager.ACTION_NOTIFICATION_CLICKED.equals(action)) { Intent notiIntent = new Intent(DownloadManager.ACTION_VIEW_DOWNLOADS); notiIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.startActivity(notiIntent); } } };
/** * Return the intent to launch for a given download item. * * @param context Context of the app. * @param downloadId ID of the download item in DownloadManager. * @return the intent to launch for the given download item. */ private static Intent getLaunchIntentFromDownloadId(Context context, long downloadId) { assert !ThreadUtils.runningOnUiThread(); DownloadManager manager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE); Uri uri = manager.getUriForDownloadedFile(downloadId); if (uri == null) return null; Intent launchIntent = new Intent(Intent.ACTION_VIEW); launchIntent.setDataAndType(uri, manager.getMimeTypeForDownloadedFile(downloadId)); launchIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_GRANT_READ_URI_PERMISSION); return launchIntent; }
@Override @TargetApi(Build.VERSION_CODES.HONEYCOMB) public void onReceive(Context context, Intent intent) { try { DilbertPreferences preferences = new DilbertPreferences(context); DownloadManager dm = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE); Uri downloadedFilePath = dm.getUriForDownloadedFile(intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, 0)); if (downloadedFilePath == null) { Log.d("DlManagerBReceiver", "No Uri for downloaded file from DownloadManager"); return; } File downloadedFile = new File(downloadedFilePath.getPath()); if (downloadedFilePath.getLastPathSegment() == null) { return; } String scheduledTargetPath = preferences.getScheduledTargetPath(downloadedFilePath.getLastPathSegment().substring(0, 10)); if (scheduledTargetPath != null) { if (!downloadedFile.renameTo(new File(scheduledTargetPath))) { Toast.makeText(context, "Couldn't move picture to selected Folder", Toast.LENGTH_SHORT).show(); } } } catch (Throwable t) { Log.e("DlManagerBReceiver", "Error while moving downloaded file to desired target folder", t); } } }
@Override public ParcelFileDescriptor openDocument(String docId, String mode, CancellationSignal signal) throws FileNotFoundException { // Delegate to real provider final long token = Binder.clearCallingIdentity(); try { final long id = Long.parseLong(docId); final ContentResolver resolver = getContext().getContentResolver(); return resolver.openFileDescriptor(mDm.getUriForDownloadedFile(id), mode); } finally { Binder.restoreCallingIdentity(token); } }
Intent installIntent = new Intent(Intent.ACTION_VIEW); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { installIntent.setDataAndType(downloadManager.getUriForDownloadedFile(id), "application/vnd.android.package-archive"); } else {
/** * Get uri by download id. * * @param completeDownLoadId download id */ public void findDownloadFileUri(long completeDownLoadId) { Uri uri; if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) { // 6.0以下 DownloadManager downloadManager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE); assert downloadManager != null; uri = downloadManager.getUriForDownloadedFile(completeDownLoadId); } else { File file = queryDownloadedFile(completeDownLoadId); uri = FileProviderCompat.getUriForFile(context, file); } if (downloadListener != null) downloadListener.onDownloadCompleted(uri); }
Uri fileUri = downloadManager.getUriForDownloadedFile(fileId);
/** * Called to open a given download item that is downloaded by the android DownloadManager. * @param context Context of the receiver. * @param intent Intent from the android DownloadManager. */ private void openDownload(final Context context, Intent intent) { long ids[] = intent.getLongArrayExtra(DownloadManager.EXTRA_NOTIFICATION_CLICK_DOWNLOAD_IDS); if (ids == null || ids.length == 0) { DownloadManagerService.openDownloadsPage(context); return; } long id = ids[0]; DownloadManager manager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE); Uri uri = manager.getUriForDownloadedFile(id); if (uri == null) { // Open the downloads page DownloadManagerService.openDownloadsPage(context); } else { Intent launchIntent = new Intent(Intent.ACTION_VIEW); launchIntent.setDataAndType(uri, manager.getMimeTypeForDownloadedFile(id)); launchIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); try { context.startActivity(launchIntent); } catch (ActivityNotFoundException e) { DownloadManagerService.openDownloadsPage(context); } } }
@Override public void onReceive(Context context, Intent intent) { long downId = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1); switch (intent.getAction()) { case DownloadManager.ACTION_DOWNLOAD_COMPLETE: if (downloadId == downId && downId != -1 && downloadManager != null) { Uri downIdUri = downloadManager.getUriForDownloadedFile(downloadId); close(); if (downIdUri != null) { LogUtil.i(TAG, "广播监听下载完成,APK存储路径为 :" + downIdUri.getPath()); SPUtil.put(Constant.SP_DOWNLOAD_PATH, downIdUri.getPath()); APPUtil.installApk(context, downIdUri); } if (onProgressListener != null) { onProgressListener.onProgress(UNBIND_SERVICE); } } break; default: break; } } }
mDownloadManager.getUriForDownloadedFile(mDownloadedFileID);
Toast.makeText(cxt.getApplicationContext(), "Updating!", Toast.LENGTH_LONG).show(); Intent intent = new Intent(Intent.ACTION_INSTALL_PACKAGE); String lastDownloaded = mgr.getUriForDownloadedFile(lastDownload).toString();