报错原因:
(摘自:http://blog.youkuaiyun.com/vrix/article/details/45289207)
『有些时候我们使用Service的时需要采用隐私启动的方式,但是Android 5.0一出来后,其中有个特性就是Service
Intent must be explitict,也就是说从Lollipop开始,service服务必须采用显示方式启动。
而android源码是这样写的(源码位置:sdk/sources/android-21/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));
}
}
}
解决方案:
(From:http://stackoverflow.com/questions/24480069/google-in-app-billing-illegalargumentexception-service-intent-must-be-explicit/26318757#26318757)
Intent intent = new Intent("com.a.b.cservice");
// This is the key line that fixed everything for me
intent.setPackage("com.a.b");或者更优雅地链式调用:
Intent intent = new Intent("com.a.b.cservice").setPackage("com.a.b");

文章详细介绍了在Android5.0及以上版本中,Service服务必须采用显示方式启动的原因,并提供了通过设置包名来解决该问题的方法。解决方案包括直接设置包名或使用链式调用方式实现。
1505

被折叠的 条评论
为什么被折叠?



