解决Service Intent must be explicit错误
来源:互联网 发布:网络教育学历学费贵 编辑:程序博客网 时间:2024/05/18 23:53
在启动或绑定Service的时候,在一个手机上使用的好好的,而换了一个手机则出现Service Intent must be explicit错误,导致整个程序崩溃。这个错误多半是系统版本不同而引起的。
Android5.0以上的系统启动或绑定Service的时候,必须是显性启动,而通常我们直接使用Action来启动。在Android5.0及以上的手机中,如还使用此方式的话,就会出现Service Intent must be explicit的错误。
使用显性启动即可解决。
隐性启动:
Intent intent = new Intent(service_action_name);// startService(intent); bindService(intent, conn, Service.BIND_AUTO_CREATE);
显性启动方法一:
Intent intent = new Intent(service_action_name); intent.setPackage(service_packageName);// startService(intent); bindService(service, conn, Service.BIND_AUTO_CREATE);
显性启动方法二:
1.增加以下方法
/*** * 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; }
2.使用createExplicitFromImplicitIntent方法来实例化Intent
Intent intent = new Intent(service_action_name); Intent intent2 = new Intent(createExplicitFromImplicitIntent(this,intent));// startService(intent2); bindService(intent2, conn, Service.BIND_AUTO_CREATE);
0 0
- 解决Service Intent must be explicit错误
- Service Intent must be explicit: Intent 解决
- Service Intent must be explicit错误
- service intent must be explicit:intent问题的解决
- Service Intent must be explicit
- Service Intent must be explicit
- Service Intent must be explicit
- android L java.lang.IllegalArgumentException: Service Intent must be explicit隐式意图找不到错误解决
- 解决Android 5.0中出现的错误:Service Intent must be explicit
- (错误处理)Android 5.0 Service Intent must be explicit
- java.lang.IllegalArgumentException: Service Intent must be explicit 异常解决
- Service Intent must be explicit 解决及详解
- 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
- java文件读写操作大全
- Python代码的人脸识别
- Opencv3.2 + mingw5.3+ win10编译
- Java 基础知识
- 在Ubuntu14.04中编译源码rgbdslam_V2
- 解决Service Intent must be explicit错误
- Your app, extension, and/or linked framework appears to contain code designed explicitly with the ca
- QNX开发环境的版本问题
- Codis的安装与使用2
- 解决IE8 不支持getDate
- C#中构造函数的作用
- Hibernate各种主键生成策略与配置详解
- 普通用户登录和管理员登录区分
- 【精简版】windows + CUDA + Anaconda + tensorflow