如何在整个活动中传递远程接口(aidl)?
我正在开发一个使用服务和远程接口的应用程序。
我有一个关于在整个活动中传递远程接口的引用的问题。
在我的第一个 Activity 中,我将服务与 Activity 绑定,为了获取对我的界面的引用,我使用
private ServiceConnection mConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName arg0, IBinder service) {
x = X.Stub.asInterface(service);
}
@Override
public void onServiceDisconnected(ComponentName arg0) {
// TODO Auto-generated method stub
}
};
x 作为对我的界面的引用。 现在我想从另一个活动访问此界面,我看到两种方法可以做到这一点,但我不知道哪一种是“正确”的方法:
- 当我调用新的活动
- 重做时,传递 x 与我的意图< code>this.bindService(new Intent(y.this,z.class), mConnection, Context.BIND_AUTO_CREATE); 在我的新 Activity 的 onCreate() 中
您建议我做什么?
I am developing an application using services and Remote interface.
I have a question about passing the reference of my Remote interface throughout Activities.
In my first Activity, I bind my service with my activity, in order to get a reference to my interface I use
private ServiceConnection mConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName arg0, IBinder service) {
x = X.Stub.asInterface(service);
}
@Override
public void onServiceDisconnected(ComponentName arg0) {
// TODO Auto-generated method stub
}
};
x being the reference to my interface.
Now I would like to access this interface from another activity, I see two ways to do it but I don't know which one is the "proper" way to do it :
- passing x with my intent when I call the new Activity
- redo
this.bindService(new Intent(y.this,z.class), mConnection, Context.BIND_AUTO_CREATE);
in the onCreate() of my new Activity
What would you advice me to do ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你确定有必要吗?如果活动和服务位于同一应用程序中,请不要使用 AIDL 来访问它们,因为这会增加开销,但没有任何价值。即使您还支持 AIDL 供第三方应用程序连接,也请改用本地绑定模式。
我怀疑这是可能的或安全的。
我怀疑这是您唯一的选择。对于本地服务,像这样的绑定操作相当便宜。这就是为什么我鼓励您在应用程序中使用本地服务绑定模式,而不是 AIDL。
Are you sure that is necessary? If the activities and the service are in the same application, please do not use AIDL to access them, as that adds overhead for no value. Use the local binding pattern instead, even if you also support AIDL for third-party applications to connect to.
I doubt this is possible or safe.
I suspect this is your only option. For a local service, binding operations like this are fairly inexpensive. That is why I encourage you to use the local binding pattern for services within your application, not AIDL.