今天在学习aidl通信的时候,使用的是两个应用,一个充当客户端,一个充当服务端,在使用客户端绑定服务端的服务的时候一直绑定不成功。原来是使用的魅蓝机型的问题。上代码:
服务端AndroidManifest.xml部分代码:(没啥说的)
<service android:name=".SinaSsoAuthService"
android:exported="true"
android:enabled="true"
android:process=":remote">
<intent-filter>
<action android:name="com.store.myservice.SinaSsoAuthService"/>
</intent-filter>
</service>
客户端代码示例1:(魅蓝和华为都可以绑定成功)
private void bindSsoAuthService() {
Intent intent = new Intent();
intent.setAction("com.store.myservice.SinaSsoAuthService");
//设置包名(方法一)
//intent.setPackage("com.store.myservice");
//设置包名(方法二)
ComponentName mComponentName = new ComponentName("com.store.myservice", "com.store.myservice.SinaSsoAuthService");
intent.setComponent(mComponentName);
//启动服务
startService(intent);
//绑定服务
bindService(intent, mCommection, Context.BIND_AUTO_CREATE);
}
客户端代码示例2:(注释掉startService,采用设置包名(方法二),魅蓝无法绑定,华为正常)
private void bindSsoAuthService() {
Intent intent = new Intent();
intent.setAction("com.store.myservice.SinaSsoAuthService");
//设置包名(方法一)
//intent.setPackage("com.store.myservice");
//设置包名(方法二)
ComponentName mComponentName = new ComponentName("com.store.myservice", "com.store.myservice.SinaSsoAuthService");
intent.setComponent(mComponentName);
//先启动服务
//startService(intent);
//绑定服务
bindService(intent, mCommection, Context.BIND_AUTO_CREATE);
}
客户端代码示例3:(采用设置包名(方法一),魅蓝无法绑定,华为正常)
private void bindSsoAuthService() {
Intent intent = new Intent();
intent.setAction("com.store.myservice.SinaSsoAuthService");
//设置包名(方法一)
intent.setPackage("com.store.myservice");
//设置包名(方法二)
//ComponentName mComponentName = new ComponentName("com.store.myservice", "com.store.myservice.SinaSsoAuthService");
//intent.setComponent(mComponentName);
//先启动服务
startService(intent);
//绑定服务
bindService(intent, mCommection, Context.BIND_AUTO_CREATE);
}
有点乱,总结一下,魅蓝跨进程启动服务,需要采用这种方式设置包名,
ComponentName mComponentName = new ComponentName("com.store.myservice", "com.store.myservice.SinaSsoAuthService");
intent.setComponent(mComponentName);
并且,在bindService()之前,需要调用startService()先启动服务,坑爹
来源:CSDN
作者:hellolengyue
链接:https://blog.csdn.net/hellolengyue/article/details/103925756