启动 Service 出现 Service Intent must be explicit 三种解决方法
来源:互联网 发布:java字符串前后去空格 编辑:程序博客网 时间:2024/06/07 08:56
我们使用Service的时需要采用隐私启动的方式,但是Android 5.0一出来后,其中有个特性就是Service Intent must be explitict,也就是说从Lollipop开始,service服务必须采用显示方式启动。
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));
- }
- }
- }
参考代码如下:
- Intent mIntent = new Intent();
- mIntent.setAction("XXX.XXX.XXX");//你定义的service的action
- mIntent.setPackage(getPackageName());//这里你需要设置你应用的包名
- context.startService(mIntent);
2、将隐式启动转换为显示启动:
参考代码如下:
- Intent mIntent = new Intent();
- mIntent.setAction("XXX.XXX.XXX");
- Intent eintent = new Intent(getExplicitIntent(mContext,mIntent));
- context.startService(eintent);
3、根据源码:getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.LOLLIPOP,可将build.gradle文件里的targetSdkVersion版本改低点<21即可
阅读全文
0 0
- 启动 Service 出现 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报错:Service Intent must be explicit
- 使用aidl隐式调用service出现service intent must be explicit 问题的一种解决方法
- Service异常: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 原因和解决方法
- Android Studio之maven Central,JCenter
- The Basis
- 使用httpclient发送get或post请求
- Window10+Ubuntu14.04双系统安装教程
- 纯CSS3动画之左右翻转
- 启动 Service 出现 Service Intent must be explicit 三种解决方法
- 集合总结
- Linux 学习 (六) (CentOS7)共享文件夹找不到怎么办
- 自定义控件那些事儿 ----- 四【布局】
- Spring 事务异常处理
- fixed针对于其他元素定位、父级
- Toast,popupWindow,AlertDialog
- phpstorm 配置 PHP_CodeSniffer
- 移动端相册开发