Service Intent must be explicit
来源:互联网 发布:淘宝的旺旺名怎么修改 编辑:程序博客网 时间:2024/04/29 14:54
Android5.0中service的intent一定要显性声明
- final Intent intent = new Intent(this,BindService.class);
- bindService(intent,coon,Service.BIND_AUTO_CREATE)
可以将隐性调用变成显性调用。先定义一个函数:
- /***
- * Android L (lollipop, API 21) introduced a new problem when trying to invoke implicit intent,
- * "java.lang.IllegalArgumentException: Service Intent must be explicit"
- *
- * If you are using an implicit intent, and know only 1 target would answer this intent,
- * This method will help you turn the implicit intent into the explicit form.
- *
- * Inspired from SO answer: http://stackoverflow.com/a/26318757/1446466
- * @param context
- * @param implicitIntent - The original implicit intent
- * @return Explicit Intent created from the implicit original intent
- */
- 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;
- }
然后调用
- final Intent intent = new Intent();
- intent.setAction("com.example.user.firstapp.FIRST_SERVICE");
- final Intent eintent = new Intent(createExplicitFromImplicitIntent(this,intent));
- bindService(eintent,conn, Service.BIND_AUTO_CREATE);
阅读全文
0 0
- Service Intent must be explicit
- Service Intent must be explicit
- Service Intent must be explicit
- Service Intent must be explicit: Intent
- Service Intent must be explicit: Intent 解决
- Service异常:Service Intent must be explicit
- Service Intent must be explicit的解决方法
- Android 5.0 Service Intent must be explicit
- IllegalArgumentException:Service Intent must be explicit
- Android 5.0 Service Intent must be explicit
- Android 5.0 Service Intent must be explicit
- Service Intent must be explicit的解决方法
- Service Intent must be explicit解决方案
- Service Intent must be explicit错误
- Service Intent must be explicit的解决方法
- Service Intent must be explicit的解决方法
- Service Intent must be explicit的解决方法
- Android “Service Intent must be explicit”解决办法
- 关于分段均分计算
- C# 读取Excel时读取不到第一行的问题。
- Java文件IO学习笔记(三)--创建文件和目录
- 查看 SELinux状态及关闭SELinux
- SQL 基础 总结(10) SQL语句
- Service Intent must be explicit
- 深入浅出JMS(四)--ActiveMQ[消费者]实时持久化数据到HDFS
- Eclipse常见问题(maven 和 jetty的使用)
- 1.3 开始第一幅“码绘”——开始使用p5.js,画一个懵逼脸
- new与malloc的10点区别
- CentOS6.5 CKAN 安装记录
- ABAP弹出框案例
- 上下拉刷新列表总结
- 《C++ Primer》第五版课后习题解答_第五章(2)(09-14)