今天在学习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>
然后在activity中用如下代码绑定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时会不会也有类似的问题,待续。

另一种简单点的解决方法可参考另一篇日志。

更多相关文章

  1. Android(安卓)AIDL基本步骤
  2. Android(安卓)databinding 双向绑定(Demo)
  3. Android(安卓)listview与adapter用法
  4. android 绑定arp
  5. Android(安卓)Service的onRebind方法调用时机
  6. bindService
  7. Android(安卓)TV Input Framework(TIF)--显示Tv Input
  8. Android之XUtils的框架总结
  9. 【Android(安卓)界面效果34】Android里Service的bindService()和

随机推荐

  1. Android(安卓)NDK开发基础篇(二)
  2. S3C6410(M8用的) 移植Android(安卓)内核
  3. Android性能调优
  4. Android判断当前应用程序处于前台还是后
  5. Android—— ubuntu下【CTS】测试TV真机
  6. Android的Menu
  7. Android的两种数据存储方式分析(一)
  8. 更新Activity的几个方法
  9. Android系统信息获取 之九:TelephonyManag
  10. Android(安卓)时间显示控件 TextClock