一、問題描述java
今天使用Android系統的DownloadManager進行下載操做時,爆了以下所示的錯誤:android
java.lang.RuntimeException: Unable to start service com.xtc.fm.qingting.services.QingtingFmDownloadService2@1b97b060 with Intent { act=com.xtc.fm.qingting.FM_DOWNLOAD_SERVICE_START pkg=com.xtc.fm.qingting cmp=com.xtc.fm.qingting/.services.QingtingFmDownloadService2 (has extras) }: java.lang.SecurityException: Invalid value for visibility: 2 at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2911) at android.app.ActivityThread.access$2100(ActivityThread.java:151) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1401) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5254) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:955) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:750) Caused by: java.lang.SecurityException: Invalid value for visibility: 2 at android.os.Parcel.readException(Parcel.java:1592) at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:190) at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:142) at android.content.ContentProviderProxy.insert(ContentProviderNative.java:476) at android.content.ContentResolver.insert(ContentResolver.java:1207) at android.app.DownloadManager.enqueue(DownloadManager.java:946) at com.xtc.fm.qingting.services.QingtingFmDownloadService2.initDownManager(QingtingFmDownloadService2.java:195) at com.xtc.fm.qingting.services.QingtingFmDownloadService2.onStartCommand(QingtingFmDownloadService2.java:120) at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2894) ... 9 more java.lang.SecurityException: Invalid value for visibility: 2 at android.os.Parcel.readException(Parcel.java:1592) at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:190) at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:142) at android.content.ContentProviderProxy.insert(ContentProviderNative.java:476) at android.content.ContentResolver.insert(ContentResolver.java:1207) at android.app.DownloadManager.enqueue(DownloadManager.java:946) at com.xtc.fm.qingting.services.QingtingFmDownloadService2.initDownManager(QingtingFmDownloadService2.java:195) at com.xtc.fm.qingting.services.QingtingFmDownloadService2.onStartCommand(QingtingFmDownloadService2.java:120) at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2894) at android.app.ActivityThread.access$2100(ActivityThread.java:151) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1401) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5254) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:955) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:750)
其中相關的部分代碼以下:網絡
//設置下載地址 DownloadManager.Request downRequest = new DownloadManager.Request(Uri.parse(downloadUrl)); // 設置容許使用的網絡類型 (| DownloadManager.Request.NETWORK_MOBILE) downRequest.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI); // 下載時,不顯示通知欄 downRequest.setNotificationVisibility(DownloadManager.Request.VISIBILITY_HIDDEN); // 顯示下載界面 downRequest.setVisibleInDownloadsUi(true); // 保存的文件名 String fileName = downloadInfo.getProgramTitle() + ".m4a"; // 設置下載路徑和文件名 downRequest.setDestinationInExternalPublicDir(Environment.DIRECTORY_MUSIC, fileName); //設置文件類型 MimeTypeMap mimeTypeMap = MimeTypeMap.getSingleton(); String mimeString = mimeTypeMap.getMimeTypeFromExtension(MimeTypeMap.getFileExtensionFromUrl(downloadUrl)); downRequest.setMimeType(mimeString); // 設置爲可被媒體掃描器找到 downRequest.allowScanningByMediaScanner(); // 將下載請求放入隊列 final long downloadId = manager.enqueue(downRequest);
原來其中的一段代碼是須要相應的權限 android.permission.DOWNLOAD_WITHOUT_NOTIFICATIONapp
// 下載時,不顯示通知欄 downRequest.setNotificationVisibility(DownloadManager.Request.VISIBILITY_HIDDEN);
所以在AndroidManifest.xml文件中加入上面所須要的權限便可。ide
<!--DownloadManager下載時不顯示Notification--> <uses-permission android:name="android.permission.DOWNLOAD_WITHOUT_NOTIFICATION" />
本文轉自:https://blog.csdn.net/ouyang_peng/article/details/52047149