关于Android5.0当中的Service启动问题--- Service Intent must be explicit: Intent

来源:互联网 发布:小白管理器家庭网络 编辑:程序博客网 时间:2024/05/21 22:45

早上在调试程序的时候发生了这样的一个错误:  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.

也就是说,在5.0以后不允许使用隐式方式来启动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(需要科学上网)



0 0
原创粉丝点击