我使用系统的下载管理器从服务器获取文件,但是某些用户可能会在他/她的手机中禁用下载管理器的功能。那么如何在我的应用程序中使用程序检查这种情况,然后给出提示以帮助用户启用它?感谢。
这是我程序的一些示例代码:
private long downloadFile(Context context, String downloadUrl, String destFilename, boolean showNotify) {
// ...
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(downloadUrl.trim()));
request.setTitle(destFilename);
request.setDestinationInExternalFilesDir(context, null, destFilename);
request.setNotificationVisibility(showNotify ? DownloadManager.Request.VISIBILITY_VISIBLE :
DownloadManager.Request.VISIBILITY_HIDDEN);
// get the downloadManager
mDownloadManager = (DownloadManager) getAppContext().getSystemService(Context.DOWNLOAD_SERVICE);
long downloadId = mDownloadManager.enqueue(request);
VLog.r(TAG, " downloadFile id : " + downloadId + " url : " + downloadUrl);
downloadIds.put(downloadId, downloadUrl);
return downloadId;
}
答案 0 :(得分:0)
阅读完源代码后,我明白了一下这种情况。由于DownloadManager是基于ContentProvider实现的,因此我尝试获取DownloadManager的“ContentProviderClient”。如果失败,则表示DownloadManager有问题。这里是示例代码。
public static boolean isDownloadManagerEnable() {
ContentProviderClient client = Util.getApplicationContext().getContentResolver().acquireContentProviderClient(Uri.parse("content://downloads/my_downloads").getAuthority());
return client != null;
}