Android的L(API 21) - java.lang.IllegalArgumentException异常:服务意向一定要明确 [英] Android L (API 21) - java.lang.IllegalArgumentException: Service Intent must be explicit

查看:357
本文介绍了Android的L(API 21) - java.lang.IllegalArgumentException异常:服务意向一定要明确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Android的新版本 - 棒棒糖(API 21),给它带来了不少变化,但如果你希望你的应用程序定位到API它带有一定的价格

当我们开始适应我们的应用程序,以新的API,我们已经遇到的第一个问题是抛出:IllegalArgumentException:服务意向必须明确

如果您遇到了问题,你实际上是打算使用明确的方式你的意图(即启动该服务时,你期望击中正好1个服务行动),这里有一个快速修复转向隐 - >明确的:

 公共静态意向createExplicitFromImplicitIntent(上下文的背景下,意图implicitIntent){
        //检索所有服务,可以匹配给定的意图
        软件包管理系统下午= context.getPackageManager();
        清单< ResolveInfo> resolveInfo = pm.queryIntentServices(implicitIntent,0);        //确保只有一个匹配被发现
        如果(resolveInfo == NULL || resolveInfo.size()!= 1){
            返回null;
        }        //获取部件的信息并创建组件名
        ResolveInfo serviceInfo = resolveInfo.get(0);
        字符串的packageName = serviceInfo.serviceInfo.packageName;
        字符串的className = serviceInfo.serviceInfo.name;
        组件名组件=新的组件名(的packageName,className)已过时;        //创建一个新的意图。使用旧的一个演员和再利用等
        意图explicitIntent =新意图(implicitIntent);        //设置组件要明确
        explicitIntent.setComponent(组分);        返回explicitIntent;
    }

这应该这样做。请随意对这个新问题的其他见解发表评论。


解决方案

启动服务明确地

  =意向意向书新(背景下,Service.class);

或周围的工作限制使用软件包

  =意图意图新(com.example.intent.ACTION);
intent.setPackage(com。示例)

Android new version - "Lollipop" (API 21) brings quite a few changes with it, but it comes with some price if you wish to target your app to that API.

When we started adapting our apps to the new API, one of the first problems we've encountered is the IllegalArgumentException: Service Intent must be explicit

If you have encountered the problem, and you are actually intending to use your intent in explicit way (meaning that when starting the service you are expecting to hit exactly 1 service action), here's a quick fix for turning implicit --> explicit:

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;
    }

That should do it. Please feel free to comment for additional insights on this new issue.

解决方案

Start the service explicitely

intent = new Intent(context, Service.class);

OR Work around the limitation with a package

intent = new Intent("com.example.intent.ACTION");
intent.setPackage("com.example")

这篇关于Android的L(API 21) - java.lang.IllegalArgumentException异常:服务意向一定要明确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆