Service Intent must be explicit: Intent 解决

java.lang.IllegalArgumentException: Service Intent must be explicit: Intent { act=com.google.android.gms.analytics.service.START (has extras) }

在Activity中启动Service的时候报错: 服务意图必须是显性声明。 这是为了防止造成冲突(i.e. 有多个Service用同样的intent-filter的情况)

这是Android 5.0 (Lollipop) 之后的规定。 不能用包名的方式定义Service Intent, 而要用显性声明:   new Intent(context, xxxService.class);

详细介绍可以看stackoverflow上的介绍。

解决办法如下:

1、通过显示意图启动Service(直接用类名);

  1. Intent intent = new Intent(com.yulore.test.AppService.class);  
  2. context.startService(intent);  

2、如果想继续使用隐式意图的话,加上包名信息即可;

  1. Intent intent = new Intent();  
  2. intent.setAction("com.yulore.recognize.android");  
  3. intent.setPackage(context.getPackageName());    //兼容Android 5.0  
  4. context.startService(intent);  

参考:

http://blog.csdn.net/shenzhonglaoxu/article/details/42675287

https://my.oschina.net/u/269663/blog/396826

http://blog.csdn.net/redarmy_chen/article/details/47832615

http://blog.csdn.net/ouyang_peng/article/details/50727693

http://blog.csdn.net/top_code/article/details/45719919

猜你喜欢

转载自blog.csdn.net/a872822645/article/details/84972610