我现在有一个应用程序,其中我通过广播接收器(MyStartupIntentReceiver)调用服务。广播接收器中的代码以便调用服务是:
public void onReceive(Context context, Intent intent) {
Intent serviceIntent = new Intent();
serviceIntent.setAction("com.duk3r.eortologio2.MyService");
context.startService(serviceIntent);
}
在Android 5.0 Lollipop中,我遇到了以下错误(在之前的Android版本中,一切正常):
问题是在Android 5.0 Lollipop中,我会遇到以下错误(在之前的Android版本中,一切都运行良好):
Unable to start receiver com.duk3r.eortologio2.MyStartupIntentReceiver: java.lang.IllegalArgumentException: Service Intent must be explicit: Intent { act=com.duk3r.eortologio2.MyService }
我需要做哪些更改才能将服务声明为明确的并正常启动? 尝试了其他类似主题中的一些答案,虽然我已经摆脱了错误消息,但服务仍无法启动。
<intent-filter>
。您似乎陷入了一个误解,认为您需要在所有地方都使用<intent-filter>
--实际上,您很少需要使用<intent-filter>
。显式Intent
是指在Intent
本身中指定要通信的组件,通常使用以Java 'Class'对象作为第二个参数的构造函数。对于应用程序内部的组件,您应该使用显式Intent
,而不是隐式的Intent
和<intent-filter>
。 - CommonsWare