DownloadManager.Request.setNotificationVisibility 失败并出现 SecurityException:可见性值无效:2

发布于 2025-01-07 03:29:55 字数 1801 浏览 1 评论 0原文

我正在尝试在我的 Service 类中使用 DownloadManager

    DownloadManager downloadManager = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
    //imageUri is a valid Uri
    Request downloadRequest= new Request(imageUri); 
    //without this line, it works
    downloadRequest.setNotificationVisibility(DownloadManager.Request.VISIBILITY_HIDDEN);
    //subpath is valid
    downloadRequest.setDestinationInExternalFilesDir(this, null, subPath);
    downloadManager.enqueue(downloadRequest);

如果没有 setNotificationVisibility,此代码可以正常工作。但它显示了一个我不想要的通知,因为我使用此代码在 ListView 中延迟加载图像。

据我了解,可以隐藏此通知,

downloadRequest.setNotificationVisibility(DownloadManager.Request.VISIBILITY_HIDDEN);

但在这种情况下,我的应用程序会崩溃:

02-19 02:24:24.055: E/AndroidRuntime(2572): java.lang.SecurityException:可见性值无效:2 02-19 02:24:24.055: E/AndroidRuntime(2572): 在 android.os.Parcel.readException(Parcel.java:1327) 02-19 02:24:24.055: E/Android运行时(2572):位于 android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:181) 02-19 02:24:24.055: E/AndroidRuntime(2572): 在 android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:135) 02-19 02:24:24.055: E/AndroidRuntime(2572): 在 android.content.ContentProviderProxy.insert(ContentProviderNative.java:415) 02-19 02:24:24.055: E/AndroidRuntime(2572): 在 android.content.ContentResolver.insert(ContentResolver.java:730) 02-19 02:24:24.055: E/AndroidRuntime(2572): 在 android.app.DownloadManager.enqueue(DownloadManager.java:885) 02-19 02:24:24.055: E/AndroidRuntime(2572): 在 MyService.getImage(MyService.java:112)

setNotificationVisibility 有什么问题吗?我怎样才能收到通知?

I'm trying to use DownloadManager in a Service class of mine:

    DownloadManager downloadManager = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
    //imageUri is a valid Uri
    Request downloadRequest= new Request(imageUri); 
    //without this line, it works
    downloadRequest.setNotificationVisibility(DownloadManager.Request.VISIBILITY_HIDDEN);
    //subpath is valid
    downloadRequest.setDestinationInExternalFilesDir(this, null, subPath);
    downloadManager.enqueue(downloadRequest);

Without setNotificationVisibility, this code works well. But it displays a notification, that I don't want, because I use this code for lazy-loading images in a ListView.

I understand, this notification can be hidden with

downloadRequest.setNotificationVisibility(DownloadManager.Request.VISIBILITY_HIDDEN);

But in that case, my application crashes with:

02-19 02:24:24.055: E/AndroidRuntime(2572):
java.lang.SecurityException: Invalid value for visibility: 2 02-19
02:24:24.055: E/AndroidRuntime(2572): at
android.os.Parcel.readException(Parcel.java:1327) 02-19 02:24:24.055:
E/AndroidRuntime(2572): at
android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:181)
02-19 02:24:24.055: E/AndroidRuntime(2572): at
android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:135)
02-19 02:24:24.055: E/AndroidRuntime(2572): at
android.content.ContentProviderProxy.insert(ContentProviderNative.java:415)
02-19 02:24:24.055: E/AndroidRuntime(2572): at
android.content.ContentResolver.insert(ContentResolver.java:730) 02-19
02:24:24.055: E/AndroidRuntime(2572): at
android.app.DownloadManager.enqueue(DownloadManager.java:885) 02-19
02:24:24.055: E/AndroidRuntime(2572): at
MyService.getImage(MyService.java:112)

Whats' wrong with setNotificationVisibility? How can I had the notification?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦魇绽荼蘼 2025-01-14 03:29:55

根据文档,您需要清单中的以下权限:

<uses-permission android:name="android.permission.DOWNLOAD_WITHOUT_NOTIFICATION" />

注意
大多数清单权限将由 Android Studio 自动完成,但这不会自动完成。

You need the following permission in the manifest as per documentation:

<uses-permission android:name="android.permission.DOWNLOAD_WITHOUT_NOTIFICATION" />

Note
Most of the manifest permissions will be autocompleted by Android Studio, but this will not be auto-completed.

天涯离梦残月幽梦 2025-01-14 03:29:55

但还需要将该权限添加到 rn-fetch-blob 的清单中,但其中未给出。

<uses-permission android:name="android.permission.DOWNLOAD_WITHOUT_NOTIFICATION" />

but also need to add that permission to rn-fetch-blob's manifest that is not given in it.

<uses-permission android:name="android.permission.DOWNLOAD_WITHOUT_NOTIFICATION" />
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文