7

我正在尝试使用DownloadManager从我的应用程序下载大型 PDF 文件。我希望在下载期间以及下载完成时显示通知。但是设置可见性会导致上述异常。

此错误与此帖子不同DownloadManager.Request.setNotificationVisibility failed with jSecurityException: invalid value for visibility: 2

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 级的,所以蜂窝和更高。我尝试过的权限是:

  • android.permission.DOWNLOAD_WITHOUT_NOTIFICATION
  • android.permission.SEND_DOWNLOAD_COMPLETED_INTENTS
  • android.permission.ACCESS_DOWNLOAD_MANAGER
  • android.permission.ACCESS_DOWNLOAD_MANAGER_ADVANCED
4

3 回答 3

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 的乐趣!

于 2013-01-19T00:19:41.580 回答
3

如果你想使用 'VISIBILITY_HIDDEN',你应该在 androidManifest.xml 中添加这个权限

<uses-permission android:name="android.permission.DOWNLOAD_WITHOUT_NOTIFICATION"/>
于 2015-11-17T09:15:30.277 回答
0

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

于 2013-05-25T23:31:26.453 回答