开发者

How to pass Remote Interface (aidl) throughout Activities?

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 开发者_如何学Cdo ?


I am developing an application using services and Remote interface.

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.

passing x with my intent when I call the new Activity

I doubt this is possible or safe.

redo this.bindService(new Intent(y.this,z.class), mConnection, Context.BIND_AUTO_CREATE); in the onCreate() of my new Activity

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜