Android 5.0 禁止使用隐式Intent来启动Service.异常:service intent must be explicit
来源:互联网 发布:淘宝类似图片怎么搜索 编辑:程序博客网 时间:2024/05/22 04:50
Android 5.0 禁止使用隐式Intent来启动Service.异常:service intent must be explicit
最近运行了一下我以前做的一个启动service的demo程序,发现!不!好!用!了!看了一下Log异常:service intent must be explicit.什么鬼?什么鬼?什么鬼!上网查了一下,原来从Android 5.0 开始,google出于安全的角度禁止了隐式声明Intent来启动Service.也禁止使用Intent filter.否则就会抛这个个异常出来.
问题分析
首先比对Android 4.4 与 Android 5.0 的区别:
Android 4.4:链接:(/frameworks/base/core/java/android/app/ContextImpl.java)
private void validateServiceIntent(Intent service) { if (service.getComponent() == null && service.getPackage() == null) { if (true || getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.KITKAT) { Log.w(TAG, "Implicit intents with startService are not safe: " + service + " " + Debug.getCallers(2, 3)); //IllegalArgumentException ex = new IllegalArgumentException( // "Service Intent must be explicit: " + service); //Log.e(TAG, "This will become an error", ex); //throw ex; } } }
Android 5.0:链接:(/frameworks/base/core/java/android/app/ContextImpl.java)
private void validateServiceIntent(Intent service) { if (service.getComponent() == null && service.getPackage() == null) { if (getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.LOLLIPOP) { IllegalArgumentException ex = new IllegalArgumentException( "Service Intent must be explicit: " + service); throw ex; } else { Log.w(TAG, "Implicit intents with startService are not safe: " + service + " " + Debug.getCallers(2, 3)); } } }也就是说在5.0里面,如果targetSdkVersion>=LOLLIPOP 就会报异常:“service intent must be explicit”,这就说明1.targetSdkVersion>=LOLLIPOP 2.手机系统为5.0以上版本 3.程序采用隐式Intent调用Service,这三个条件是问题发生的充分必要条件。
另外一点值得注意的是:盲目将程序targetSdkVersion提高容易导致用户手中的软件无法再升级(因为版本的升级功能通常是采用Service的方式)。
解决方案 参考博文:http://blog.csdn.net/vrix/article/details/45289207
方案一:设置Action和packageName(google官方建议):
Intent mIntent = new Intent(); mIntent.setAction("XXX.XXX.XXX"); mIntent.setPackage(getPackageName()); context.startService(mIntent);
方案二:将隐式启动转换为显示启动
实现转换函数:(参考:http://developer.android.com/goo ... tml#billing-service)
public static Intent getExplicitIntent(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; }调用函数:
Intent mIntent = new Intent(); mIntent.setAction("XXX.XXX.XXX"); Intent eintent = new Intent(getExplicitIntent(mContext,mIntent)); context.startService(eintent);
// 如果觉得不错,记得顶我哦! 顶我!顶我!顶我!
3 0
- Android 5.0 禁止使用隐式Intent来启动Service.异常:service intent must be explicit
- Service异常:Service Intent must be explicit
- Android 5.0 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异常分析
- Android-Service Intent must be explicit异常说明
- Android “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: Intent
- Service Intent must be explicit: Intent 解决
- android 5.0系统 Service Intent must be explicit
- (错误处理)Android 5.0 Service Intent must be explicit
- 启动Service报错:Service Intent must be explicit
- 使用aidl隐式调用service出现service intent must be explicit 问题的一种解决方法
- 解决Android 5.0版本之后出现的异常:Service Intent must be explicit
- NSUserDefaults 简介,使用 NSUserDefaults 存储自定义对象
- Android Studio1.0升级到1.5编译报错的处理
- nginx篇(一)
- Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFString cont
- java时间转换
- Android 5.0 禁止使用隐式Intent来启动Service.异常:service intent must be explicit
- 通过Runtime类启动外部文件
- hibernate之xxx.hbm.xml配置详解
- js jsonp跨域请求实现
- win7 .net加解密模块出错,System.Reflection.TargetInvocationException
- hibernate之hibernate.cfg.xml详解
- 第一行代码2.5-活动的启动模式
- svn分支开发与主干合并(branch & merge)
- 关于SpringAOP的一些事