在Activity中启动Service的时候报错: 服务意图必须是显性声明。 这是为了防止造成冲突(i.e. 有多个Service用同样的intent-filter的情况)

这是Android 5.0 (Lollipop) 之后的规定。 不能用包名的方式定义Service Intent, 而要用显性声明:

new Intent(context, xxxService.class);

如果一定要用隐性声明,可以用下面的方法(Stackoverflow上的回答)

public static Intent createExplicitFromImplicitIntent(Context context, Intent implicitIntent) {
// Retrieve all services that can match the given intent
PackageManager pm = context.getPackageManager();
List<ResolveInfo> resolveInfo = pm.queryIntentServices(implicitIntent, 0); // Make sure only one match was found
if (resolveInfo == null || resolveInfo.size() != 1) {
return null;
} // Get component info and create ComponentName
ResolveInfo serviceInfo = resolveInfo.get(0);
String packageName = serviceInfo.serviceInfo.packageName;
String className = serviceInfo.serviceInfo.name;
ComponentName component = new ComponentName(packageName, className); // Create a new intent. Use the old one for extras and such reuse
Intent explicitIntent = new Intent(implicitIntent); // Set the component to be explicit
explicitIntent.setComponent(component); return explicitIntent;
}

java.lang.IllegalArgumentException: Service Intent must be explicit: Intent的更多相关文章

  1. java.lang.IllegalArgumentException: Service Intent must be explicit 解决办法

    java.lang.IllegalArgumentException: Service Intent must be explicit 意思是服务必须得显式的调用 我之前是这样使用绑定Service的 ...

  2. java.lang.IllegalArgumentException: Service not registered

    java.lang.IllegalArgumentException: Service not registered 首先检查一下,Service是否在AndroidManifest文件中注册.格式如 ...

  3. [Android]Caused by: java.lang.IllegalArgumentException: Service not registered.md

    Caused by: java.lang.IllegalArgumentException: Service not registered: org.diql.aidldemo.MainActivit ...

  4. Android用Intent来启动Service报“java.lang.IllegalArgumentException: Service Intent must be explicit”错误的解决方法

    今天没事来写个播放器,照搬书上的原句,其中一句 //用于启动和停止service的Intent final Intent it = new Intent("android.mu.action ...

  5. Servlet.service() for servlet [jsp] in context with path [/Healthy_manager] threw exception [Unable to compile class for JSP] with root cause java.lang.IllegalArgumentException: Page directive: inval

    严重: Servlet.service() for servlet [jsp] in context with path [/Healthy_manager] threw exception [Una ...

  6. java.lang.IllegalArgumentException: Result Maps collection does not contain value for java.lang.Integer

    今天做springmvc+mybatis+spring的项目的时候发现了一个异常.如下: org.apache.ibatis.builder.IncompleteElementException: C ...

  7. java.lang.IllegalArgumentException 不合法的参数异常

    报错内容: IllegalArgumentException 不合法的参数异常 十二月 06, 2016 10:06:56 上午 org.apache.catalina.core.StandardWr ...

  8. Error querying database. Cause: java.lang.IllegalArgumentException: Mapped Statements collection does not contain value for …

    编译通过并且运行web成功后,访问的页面不需要连接数据库,不牵扯到反射调用实体类就不会报错, 报错内容如下: [WARNING] org.springframework.web.util.Nested ...

  9. java.lang.IllegalArgumentException

    org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.exceptions.Persiste ...

随机推荐

  1. javap 可以打印出用于jni调用的java函数的签名信息

    javap可以打印出java的字节码: -c     Prints out disassembled code, i.e., the instructions that comprise the Ja ...

  2. 见过的最好AWK手册

    原文: http://linuxfire.com.cn/~lily/awk.html 简体中文版由bones7456 (http://li2z.cn)整理. 原文:应该是 http://phi.sin ...

  3. Merge Cells for DataGrid 合并单元格

    只适合不分页的固定行列的表格 <script type="text/javascript"> function onLoadSuccess(data){ var mer ...

  4. 【转】Maven实战(四)---多模块项目---JBOSS部署问题

    原文出自于:http://blog.csdn.net/liutengteng130/article/details/41622681      感谢! 这几天在搭框架中仅仅是JBOSS就遇到了很多问题 ...

  5. Spark生态之Spark Graphx

  6. POJ 3159 Candies(差分约束,最短路)

    Candies Time Limit: 1500MS   Memory Limit: 131072K Total Submissions: 20067   Accepted: 5293 Descrip ...

  7. IMAQ Flatten Image to String VI的参数设置对比

    无压缩 jpeg压缩 无损二元包装 仅JPEG压缩时有效 平化类型(指定字符串中存储什么类型的数据)   None JPEG PACKED BINARY Quality Image Image and ...

  8. Web安全--使用Salt + Hash将密码加密后再存储进数据库

    转载原地址 http://www.bozhiyue.com/mianshiti/_net/2016/0728/314239.html (一) 为什么要用哈希函数来加密密码 如果你需要保存密码(比如网站 ...

  9. UVa 1629 Cake slicing (记忆化搜索)

    题意:一个矩形蛋糕上有好多个樱桃,现在要做的就是切割最少的距离,切出矩形形状的小蛋糕,让每个蛋糕上都有一个樱桃,问最少切割距离是多少. 析:很容易知道是记忆化搜索,我们用dp[u][d][l][r]来 ...

  10. Javascript高级篇-Function对象

    1.引入 1.1Function是基于原型的对象 2.创建Function对象 2.1 var myFun = new Function("参数一","参数二" ...