java.lang.NoSuchMethodError: android.app.DownloadManager$Request.setNotificationVisibility

StackOverflow https://stackoverflow.com/questions/15887830

  •  02-04-2022
  •  | 
  •  

質問

Trying to use DownloadManager like so

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);

Added the following permission in Android Manifest

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

Getting the following error at runtime

java.lang.NoSuchMethodError: android.app.DownloadManager$Request.setNotificationVisibility

Why this error? How to make DownloadManager work?

役に立ちましたか?

解決

Do I need to use two separate DownloadManager.Request one for API 9 and another for API 11?

No, but you do need to use a Java guard block:

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);
}

And, you will have to settle for the fact that your download will be visible for API Level 9 and 10 devices.

他のヒント

NoSuchMethodError occurs when you are using a method that isn't available in that device's API. You can test if this is the case by running your code on an emulator running the latest version of Android. You can still keep the newer method (and you probably should), but put it in a try statement. If you get a NoSuchMethodError, then your code is being run on an older device, and you need to have your catch statement use a work around.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top