安卓Service Intent must be explicit的解决方法
来源:互联网 发布:如何删除wifi连接网络 编辑:程序博客网 时间:2024/05/16 07:56
转载至:http://blog.csdn.net/shenzhonglaoxu/article/details/42675287
今天在学习Android的Service组件的时候,在AndroidMainfest.xml中定义了
- <service
- android:name=".BindService"
- android:enabled="true"
- android:exported="true" >
- <intent-filter>
- <action android:name="com.example.user.firstapp.FIRST_SERVICE"/>
- </intent-filter>
- </service>
- final Intent intent = new Intent();
- intent.setAction("com.example.user.firstapp.FIRST_SERVICE");
- bindService(intent,coon,Service.BIND_AUTO_CREATE);
IllegalArgumentException: Service Intent must be explicit
经过查找相关资料,发现是因为Android5.0中service的intent一定要显性声明,当这样绑定的时候不会报错。
- final Intent intent = new Intent(this,BindService.class);
- bindService(intent,coon,Service.BIND_AUTO_CREATE)
在http://blog.android-develop.com/2014/10/android-l-api-21-javalangillegalargumen.html上看到一个解决方法,可以将隐性调用变成显性调用。先定义一个函数:
- /***
- * 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);
这样也可以解决问题。
PS:调用本地service是这样的,不知道其他程序隐性调用service时会不会也有类似的问题,待续。
另一种简单点的解决方法可参考另一篇日志。
0 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的解决方法
- 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
- java.lang.IllegalArgumentException: Service Intent must be explicit解决方法
- java.lang.IllegalArgumentException: Service Intent must be explicit解决方法
- Service Intent must be explicit 原因和解决方法
- 使用aidl隐式调用service出现service intent must be explicit 问题的一种解决方法
- service intent must be explicit:intent问题的解决
- 20170218.am js
- 期望dp(HDU - 4405)
- 【bzoj1968】[Ahoi2005]COMMON 约数研究
- php对xml数据循环遍历
- config 配置
- 安卓Service Intent must be explicit的解决方法
- python环境搭建之jupyter
- 设计模式-工厂模式
- 8086的取指过程
- eclpse运行项目出现errors exist in required project(s)
- 【codevs1904】[网络流24题]最小路径覆盖问题
- Linux中task_struct结构体字段分析
- php结合redis实现高并发下的抢购、秒杀功能
- 包管理器Bower