Return 与已绑定服务不同的 Binder
Return a different Binder from already bound service
我有一个服务已经通过 AIDL
.
被外部应用程序绑定
但是,有些服务请求需要启动 Activity
。
由于我无法从服务中调用 startActivityForResult
,因此我决定也将我的本地活动绑定到该服务。
(伪代码)看起来像这样:
class MyService extends Service{
public IBinder onBind(Intent intent){
if (intent.hasExtra("LocalBindingRequest")){
return getLocalBinder();
else {
return getAidlBinder();
}
}
}
class ExternalApp extends Activity{
void someFunc(){
Intent i = new Intent(new ComponentName("com.my.pkg", "com.my.pkg.MyService");
bindService(i, myServiceConnection, Context.BIND_AUTO_CREATE);
}
}
class InternalApp extends Activity{
MyService mService;
void someFunc(){
Intent i = new Intent(new ComponentName("com.my.pkg", "com.my.pkg.MyService")
.putExtra("LocalBindingRequest", true);
bindService(i, myServiceConnection, Context.BIND_AUTO_CREATE);
}
public void onServiceConnected(ComponentName cn, IBinder service){
InternalBinder ib = (LocalBinder)service;
mService = ib.getService();
}
}
流程是这样的:
- ExternalApp 绑定到 AidlBinder
- ExternalApp 调用函数,需要服务启动 Activity
- 服务开始Activity
- 内部Activity尝试绑定
- 我得到一个异常(显然没有在
onBind
或 onServiceConnected
中遇到断点)
java.lan.ClassCastException: AidlService cannot be cast to InternalBinder
Service 不能 return 不同的 Binder 吗?
如果没有,我能做什么,将结果传播回已绑定的 MyService?
好的,我应该阅读 onBind(Intent)
中说明的文档
Intent: The Intent that was used to bind to this service, as given to
Context.bindService. Note that any extras that were included with the
Intent at that point will not be seen here.
这就是我获得 Aidl 服务的原因。修复方法是:
class InternalApp extends Activity{
MyService mService;
void someFunc(){
Intent i = new Intent(new ComponentName("com.my.pkg", "com.my.pkg.MyService");
i.setAction("LocalBindingRequest");
bindService(i, myServiceConnection, Context.BIND_AUTO_CREATE);
}
public void onServiceConnected(ComponentName cn, IBinder service){
InternalBinder ib = (LocalBinder)service;
mService = ib.getService();
}
}
class MyService extends Service{
public IBinder onBind(Intent intent){
if ("LocalBindingRequest".equals(intent.getAction()){
return getLocalBinder();
else {
return getAidlBinder();
}
}
}
而且我们可以为每个绑定请求设置单独的绑定器
我有一个服务已经通过 AIDL
.
但是,有些服务请求需要启动 Activity
。
由于我无法从服务中调用 startActivityForResult
,因此我决定也将我的本地活动绑定到该服务。
(伪代码)看起来像这样:
class MyService extends Service{
public IBinder onBind(Intent intent){
if (intent.hasExtra("LocalBindingRequest")){
return getLocalBinder();
else {
return getAidlBinder();
}
}
}
class ExternalApp extends Activity{
void someFunc(){
Intent i = new Intent(new ComponentName("com.my.pkg", "com.my.pkg.MyService");
bindService(i, myServiceConnection, Context.BIND_AUTO_CREATE);
}
}
class InternalApp extends Activity{
MyService mService;
void someFunc(){
Intent i = new Intent(new ComponentName("com.my.pkg", "com.my.pkg.MyService")
.putExtra("LocalBindingRequest", true);
bindService(i, myServiceConnection, Context.BIND_AUTO_CREATE);
}
public void onServiceConnected(ComponentName cn, IBinder service){
InternalBinder ib = (LocalBinder)service;
mService = ib.getService();
}
}
流程是这样的:
- ExternalApp 绑定到 AidlBinder
- ExternalApp 调用函数,需要服务启动 Activity
- 服务开始Activity
- 内部Activity尝试绑定
- 我得到一个异常(显然没有在
onBind
或onServiceConnected
中遇到断点)
java.lan.ClassCastException: AidlService cannot be cast to InternalBinder
Service 不能 return 不同的 Binder 吗?
如果没有,我能做什么,将结果传播回已绑定的 MyService?
好的,我应该阅读 onBind(Intent)
Intent: The Intent that was used to bind to this service, as given to Context.bindService. Note that any extras that were included with the Intent at that point will not be seen here.
这就是我获得 Aidl 服务的原因。修复方法是:
class InternalApp extends Activity{
MyService mService;
void someFunc(){
Intent i = new Intent(new ComponentName("com.my.pkg", "com.my.pkg.MyService");
i.setAction("LocalBindingRequest");
bindService(i, myServiceConnection, Context.BIND_AUTO_CREATE);
}
public void onServiceConnected(ComponentName cn, IBinder service){
InternalBinder ib = (LocalBinder)service;
mService = ib.getService();
}
}
class MyService extends Service{
public IBinder onBind(Intent intent){
if ("LocalBindingRequest".equals(intent.getAction()){
return getLocalBinder();
else {
return getAidlBinder();
}
}
}
而且我们可以为每个绑定请求设置单独的绑定器