解决Service Intent must be explicit 崩溃问题
Caused by: java.lang.IllegalArgumentException:
Service Intent must be explicit: Intent { act=com.aidl.server.myserver }
今天在写一个app间通信的功能时,出现了些小问题,客户端通过aidl绑定服务端的服务,结果在client端应用启动时出现了崩溃,最初代码是这样写的:
@Override
protected void onCreate(Bundle savedInstanceState) {
...
Intent intent = new Intent("com.aidl.server.myserver");
bindService(intent, conn, Context.BIND_AUTO_CREATE);
}
错误log如下:
Service Intent must be explicit: Intent { act=com.aidl.server.myserver }
跟了一下源码,发现在ContextImpl中有如下的判断:
@Override
public boolean bindService(Intent service, ServiceConnection conn,
int flags) {
warnIfCallingFromSystemProcess();
return bindServiceCommon(service, conn, flags, mMainThread.getHandler(),
Process.myUserHandle());
}
private boolean bindServiceCommon(Intent service, ServiceConnection conn, int flags, Handler
handler, UserHandle user) {
...
validateServiceIntent(service);
...
}
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));
}
}
}
由此可见,在Android5.0中增加了对intent的判断,因为intent是通过设置action得到的,因此没有Component对象的实例,也没有包名,故而报错。原因找到,增加了一个设置包名的步骤,而且需要是App的包名,而不是Service类所在包的包名,即可顺利解决,代码如下:
I
Intent intent = new Intent("com.aidl.server.myserver");
intent.setPackage("com.aidl.server.myserver");
bindService(intent, conn, Context.BIND_AUTO_CREATE);