我正在尝试使用DownloadManager
从我的应用下载大型PDF文件。我希望在下载期间以及下载完成时显示通知。但是,设置可见性会导致上述异常。
另一篇文章是在为清单中需要权限的VISIBILITY_HIDDEN
设置可见性时寻求帮助。我正在尝试将可见性设置为DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED
,如此:
public class DMnotifyTestActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
DownloadManager mgr = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
long downloadID = mgr
.enqueue(new DownloadManager.Request(Uri.parse("http://www.adobe.com/content/dam/Adobe/en/devnet/acrobat/pdfs/pdf_reference_1-7.pdf"))
.setNotificationVisibility(
DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED)
.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "hello.pdf")
.setDescription("my.test.pack Doc"));
}
}
这导致了这个堆栈跟踪:
E/AndroidRuntime(24794): Caused by: java.lang.SecurityException: Invalid value for visibility: 1
E/AndroidRuntime(24794): at android.os.Parcel.readException(Parcel.java:1321)
E/AndroidRuntime(24794): at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:182)
E/AndroidRuntime(24794): at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:136)
E/AndroidRuntime(24794): at android.content.ContentProviderProxy.insert(ContentProviderNative.java:447)
E/AndroidRuntime(24794): at android.content.ContentResolver.insert(ContentResolver.java:721)
E/AndroidRuntime(24794): at android.app.DownloadManager.enqueue(DownloadManager.java:877)
E/AndroidRuntime(24794): at my.test.pack.DMnotifyTestActivity.onCreate(DMnotifyTestActivity.java:18)
如果没有设置可见性,代码工作正常。我已经尝试向清单添加各种权限,但仍然没有。这是针对11级,所以蜂窝状和向上。我尝试过的权限是:
答案 0 :(得分:3)
这是我克服Honeycomb平板电脑中的这个错误(版本:3.2或API等级:13):
Request req = new Request(Uri.parse(url));
if (android.os.Build.VERSION.SDK_INT > android.os.Build.VERSION_CODES.HONEYCOMB_MR2)
{
req.setNotificationVisibility(Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
}
else
{
req.setNotificationVisibility(Request.VISIBILITY_VISIBLE);
}
啊...... Android的乐趣!
答案 1 :(得分:2)
如果您想使用' VISIBILITY_HIDDEN',您应该在andoroidManifest.xml中添加此权限
<uses-permission android:name="android.permission.DOWNLOAD_WITHOUT_NOTIFICATION"/>
答案 2 :(得分:0)
我刚刚与Marc的类似应用程序(dm的代码相同)面临此错误。在开发期间从未遇到过,我没有Honeycomb用户。我有一个类似于上面的代码,但对于Gingerbread及以上代码。
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
}
else {
request.setShowRunningNotification(true);
}
之前的“hack”是针对Honeycomb的,但由于我没有Honeycomb用户,我可以确认&gt; 4.0中存在的错误是+ 80%的用户。该问题出现在开发者控制台上,我无法使用我的设备重新创建它。当用户开始抱怨时,会更新我对错误条件的回答。
修改强>
我爱我的用户。我们必须与遇到此问题的用户一起测试代码。应用程序在开始下载时崩溃(创建了通知VISIBILITY_VISIBLE_NOTIFY_COMPLETED)。他确实使用的是android 4.0.3。
如何修复
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE);
}
else {
request.setShowRunningNotification(true);
}
基本上与上一个答案相同,但我们可以确认api 15中存在问题,因此只需进行调整即可影响所有版本api&gt; 11,不要担心api 16和17遭遇同样的问题