1、问题描述

今天使用Android系统的DownloadManager进行下载操作时,爆了如下所示的错误:

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:
at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:)
at android.app.ActivityThread.access$(ActivityThread.java:)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:)
at android.os.Handler.dispatchMessage(Handler.java:)
at android.os.Looper.loop(Looper.java:)
at android.app.ActivityThread.main(ActivityThread.java:)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:)
Caused by: java.lang.SecurityException: Invalid value for visibility:
at android.os.Parcel.readException(Parcel.java:)
at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:)
at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:)
at android.content.ContentProviderProxy.insert(ContentProviderNative.java:)
at android.content.ContentResolver.insert(ContentResolver.java:)
at android.app.DownloadManager.enqueue(DownloadManager.java:)
at com.xtc.fm.qingting.services.QingtingFmDownloadService2.initDownManager(QingtingFmDownloadService2.java:)
at com.xtc.fm.qingting.services.QingtingFmDownloadService2.onStartCommand(QingtingFmDownloadService2.java:)
at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:)
... more
java.lang.SecurityException: Invalid value for visibility:
at android.os.Parcel.readException(Parcel.java:)
at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:)
at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:)
at android.content.ContentProviderProxy.insert(ContentProviderNative.java:)
at android.content.ContentResolver.insert(ContentResolver.java:)
at android.app.DownloadManager.enqueue(DownloadManager.java:)
at com.xtc.fm.qingting.services.QingtingFmDownloadService2.initDownManager(QingtingFmDownloadService2.java:)
at com.xtc.fm.qingting.services.QingtingFmDownloadService2.onStartCommand(QingtingFmDownloadService2.java:)
at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:)
at android.app.ActivityThread.access$(ActivityThread.java:)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:)
at android.os.Handler.dispatchMessage(Handler.java:)
at android.os.Looper.loop(Looper.java:)
at android.app.ActivityThread.main(ActivityThread.java:)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:)

其中相关的部分代码如下:

 //设置下载地址
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);

2、解决方法

原来其中的一段代码是需要相应的权限 android.permission.DOWNLOAD_WITHOUT_NOTIFICATION

  // 下载时,不显示通知栏
downRequest.setNotificationVisibility(DownloadManager.Request.VISIBILITY_HIDDEN);

因此在AndroidManifest.xml文件中加入上面所需要的权限即可。

<!--DownloadManager下载时不显示Notification-->
<uses-permission android:name="android.permission.DOWNLOAD_WITHOUT_NOTIFICATION" />
本文转自:https://blog.csdn.net/ouyang_peng/article/details/52047149

转:android DownloadManager: java.lang.SecurityException: Invalid value for visibility: 2的更多相关文章

  1. 我的Android进阶之旅------>解决DownloadManager报错java.lang.SecurityException: Invalid value for visibility: 2

    1.问题描述 今天使用Android系统的DownloadManager进行下载操作时,爆了如下所示的错误: java.lang.RuntimeException: Unable to start s ...

  2. 执行打的maven jar包时出现“Exception in thread "main" java.lang.SecurityException: Invalid signature file digest for Manifest main attributes”

    Exception in thread "main" java.lang.SecurityException: Invalid signature file digest for ...

  3. Intellij打包jar文件,“java.lang.SecurityException: Invalid signature file digest for Manifest main attrib

    下面是使用Intellij 打包jar文件的步骤,之后会有运行jar文件时遇到的错误. 打包完成. ================================================== ...

  4. spark提交任务报错: java.lang.SecurityException: Invalid signature file digest for Manifest main attributes

    spark提交任务报错: java.lang.SecurityException: Invalid signature file digest for Manifest main attributes ...

  5. 解决java.lang.SecurityException: Invalid signature file digest for Manifest main attributes

    解决java.lang.SecurityException: Invalid signature file digest for Manifest main attributes 当项目依赖其他jar ...

  6. Ubuntu环境下java.lang.SecurityException: Invalid signature file digest for Manifest main attributes

    一.问题描述 Ubuntu环境中,将maven项目打包成jar包传到服务器中后运行以下指令: $ java -cp my.jar com.myproject.Main 遇到报错: java.lang. ...

  7. 封装jar问题java.lang.SecurityException: Invalid signature file digest for Manifest main attributes以及maven依赖重提解决

    1.jar包封装完成后,其他项目引用jar,启动时报错java.lang.SecurityException: Invalid signature file digest for Manifest m ...

  8. idea的jar文件,“java.lang.SecurityException: Invalid signature file digest for Manifest main attribute

    感谢大佬:https://blog.csdn.net/mingyuli/article/details/84674483 命令行运行jar出现问题: 1.找不到主类.打开jar文件包,在MANIFES ...

  9. 【Android】java.lang.SecurityException: getDeviceId: Neither user 10065 nor current process has android.permission.READ_PHONE_STATE

    RT, 异常信息如下: java.lang.SecurityException: getDeviceId: Neither user 10065 nor current process has and ...

随机推荐

  1. Powershell-加域脚本

    $domain = "abc" $password = "mima" | ConvertTo-SecureString -asPlainText -Force ...

  2. cdh的web管理界面503

    503 Service Unavailable No server is available to handle this request.       重启 agent  以及 server   

  3. nginx编译安装和功能介绍

    nginx介绍 nginx: engine X,是由1994年毕业于俄罗斯国立莫斯科鲍曼科技大学的同学为俄罗斯rambler.ru公司开发的,开发工作最早从2002年开始,第一次公开发布时间是2004 ...

  4. 快上车,react 入门拾遗

    最近朋友圈和微博都刷了一波杰伦的回忆杀–说好不哭,想想都9012了,在学习react如火如荼的路上,也不妨停下脚步来总结总结,朝花夕拾一下. 为了便于阐述,我们还是来段小明和禅师的故事吧. 小明在学习 ...

  5. 列表推导式中的各个元素的id并不一样

    列表推导式中的各个元素在不同的内存中,id不一样 # 列表推导式的不同id值 ----------------------------------------------------- T = [] ...

  6. phpstorm快捷键使用

  7. The 2019 China Collegiate Programming Contest Harbin Site F. Fixing Banners

    链接: https://codeforces.com/gym/102394/problem/F 题意: Harbin, whose name was originally a Manchu word ...

  8. jmeter HTTP请求之content-type

    对于初次接触接口的同学来说,自己在发送一个http请求时,总会遇到这样那样的问题,比如必传参数不存在啊 出现这样类似问题的问题首先排除的应该是content-type是否正确,那什么是content- ...

  9. Linux系统出现hung_task_timeout_secs和blocked for more than 120 seconds的解决方法

    Linux系统出现系统没有响应. 在/var/log/message日志中出现大量的 “echo 0 > /proc/sys/kernel/hung_task_timeout_secs" ...

  10. oracle的jdbc.properties文件配置

    ----------Oracle #do Oracle JDBC jdbc.driverClassName=oracle.jdbc.driver.OracleDriverjdbc.url=jdbc:o ...