早上在调试程序的时候发生了这样的一个错误: Service Intent must be explicit: Intent
日志如下:
Unable to start receiver com.duk3r.eortologio2.MyStartupIntentReceiver: java.lang.IllegalArgumentException: Service Intent must be explicit: Intent { act=com.duk3r.eortologio2.MyService }
好吧,现在来看代码:
Intent serviceIntent = new Intent(); serviceIntent.setAction("com.duk3r.eortologio2.MyService"); context.startService(serviceIntent);很简单的隐式启动Service操作,为何就出错了哪?
在官网发现了这样一个问题:
Binding to a Service
The Context.bindService() method now requires an explicit Intent, and throws an exception if given an implicit intent. To ensure your app is secure, use an explicit intent when starting or binding your Service, and do not declare intent filters for the service.
解决方法也很简单,既然不让使用隐式启动,那就直接启动了,上代码:
Intent serviceIntent = new Intent(context,MyService.class);
context.startService(serviceIntent);
到此,问题解决,所以应该多去关注官方关于每个Android版本更新的描述!
参考链接:http://stackoverflow.com/questions/27842430/service-intent-must-be-explicit-intent
https://developer.android.com/about/versions/android-5.0-changes.html#BindService(需要科学上网)
本文讨论了在Android 5.0及以上版本中,使用隐式方式启动Service引发的问题及解决方法。通过分析错误日志和官方文档,解释了在新版本中不允许隐式启动Service的原因,并提供了正确的显式启动Service的代码示例。

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



