如何在整个活动中传递远程接口(aidl)?

发布于 2024-09-01 01:03:47 字数 786 浏览 3 评论 0原文

我正在开发一个使用服务和远程接口的应用程序。

我有一个关于在整个活动中传递远程接口的引用的问题。

在我的第一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

り繁华旳梦境 2024-09-08 01:03:47

我正在开发一个应用程序
服务和远程接口。

你确定有必要吗?如果活动和服务位于同一应用程序中,请不要使用 AIDL 来访问它们,因为这会增加开销,但没有任何价值。即使您还支持 AIDL 供第三方应用程序连接,也请改用本地绑定模式。

当我打电话时传递 x 与我的意图
新活动

我怀疑这是可能的或安全的。

重做this.bindService(new
意图(y.this,z.class),mConnection,
上下文.BIND_AUTO_CREATE);在
我的新 Activity 的 onCreate()

我怀疑这是您唯一的选择。对于本地服务,像这样的绑定操作相当便宜。这就是为什么我鼓励您在应用程序中使用本地服务绑定模式,而不是 AIDL。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文