DownloadManager.Request.setNotificationVisibility fails with jSecurityException: invalid value for visibility: 1

社会主义新天地 提交于 2019-12-01 16:49:24

Here's my hack to overcome this bug in Honeycomb tablets (Version: 3.2 or API Level: 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);
}

Ah... the fun with Android!

if you want to use 'VISIBILITY_HIDDEN',you should add this permission in androidManifest.xml

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

I just faced this error with a similar app (same code for dm) to Marc's. Never encountered it during development, and I don't have Honeycomb users. I have a code similar to the above one but for Gingerbread and above.

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
        request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
        }
    else {
        request.setShowRunningNotification(true);
        }   

The previous "hack" is aimed for Honeycomb, but as I have no Honeycomb users, I can confirm the bug is present in >4.0 which are +80% of my users. The issue appeared on developer console, and I'm not able to recreate it with my devices. Will update my answer to the conditions for the error when the users start complaining.

EDIT:

I love my users. We got to test the code with a user who had this issue. The app crashed when he started the download (that created the notification VISIBILITY_VISIBLE_NOTIFY_COMPLETED). He was indeed using android 4.0.3.

How to fix

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
        request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE);
        }
    else {
        request.setShowRunningNotification(true);
        }   

Basically the same as the previous answer, but we can confirm the issue is present in api 15, so just make the adjustment to affect all versions api > 11, and don't worry about api 16 and 17 to suffer the same issue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!