[英]java.lang.NoSuchMethodError: android.app.DownloadManager$Request.setNotificationVisibility
试着像这样使用DownloadManager
DownloadManager.Request request = new DownloadManager.Request(uri)
.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_MOBILE)
.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI)
.setAllowedOverRoaming(true)
.setDestinationInExternalFilesDir(this, null,String.valueOf(mPathAndFolder))
.setVisibleInDownloadsUi(false)
.setNotificationVisibility(DownloadManager.Request.VISIBILITY_HIDDEN);
long downloadID = downloadManager.enqueue(request);
在Android Manifest中添加了以下权限
<uses-permission android:name="android.permission.DOWNLOAD_WITHOUT_NOTIFICATION"/>
在运行时获取以下错误
java.lang.NoSuchMethodError:android.app.DownloadManager $ Request.setNotificationVisibility
为什么这个错误? 如何使DownloadManager工作?
我是否需要为API 9使用两个单独的DownloadManager.Request,为API 11使用另一个?
不,但您确实需要使用Java保护块:
DownloadManager.Request request = new DownloadManager.Request(uri)
.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_MOBILE)
.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI)
.setAllowedOverRoaming(true)
.setDestinationInExternalFilesDir(this, null,String.valueOf(mPathAndFolder))
.setVisibleInDownloadsUi(false);
if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.HONEYCOMB) {
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_HIDDEN);
}
并且,您必须满足于下载对于API级别9和10设备可见的事实。
当您使用该设备的API中不可用的方法时,会发生NoSuchMethodError。 您可以通过在运行最新版本Android的模拟器上运行代码来测试是否是这种情况。 您仍然可以保留较新的方法(您可能应该这样做),但将其放在try语句中。 如果您收到NoSuchMethodError,那么您的代码将在旧设备上运行,并且您需要让catch语句使用解决方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.