通过 AIDL 从服务传递到 UI 线程的自定义类

发布于 2024-09-03 08:38:17 字数 461 浏览 1 评论 0原文

我有一项服务可以定期查询网络服务器以获取新消息。该服务将新消息存储在 arrayList 中。这些消息是使用自定义类实现的,存储各种元数据(字符串和长整型)。

然后,活动连接到该服务以检索这些消息并将其显示给用户。

我有一个 .aidl 文件,它描述了服务公开的接口。

package com.example.package;

interface MyInterface {

    List<Message> getMessages();
}

Message 类扩展了 Parcelable 类,该类应该允许 IPC 传输。

问题是这样的:Eclipse 给我一个错误,指出 List 的类型未知。任何导入都会被标记为无效。

有想法吗?谢谢

I have a service that regularly queries a web server for new messages. The service stores the new messages in an arrayList. These messages are implemented using a custom class, storing all kinds of metadata (strings and longs).

An activity then connects to this service to retrieve those messages and display them to the user.

I have an .aidl file that describes the interface that the service exposes.

package com.example.package;

interface MyInterface {

    List<Message> getMessages();
}

The Message class extends the Parcelable class which should allow for the IPC transfer.

The problem is this: Eclipse gives me an error saying that the type of List<Message> is unknown. Any imports are marked as invalid.

Ideas? Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

疑心病 2024-09-10 08:38:17

我有一个 .aidl 文件,它描述了
服务的接口
暴露。

为什么?您仅需要远程服务,并且仅当您向第三方应用程序公开 API 时才需要远程服务。

如果您的服务是本地服务,请摆脱 AIDL 并使用本地绑定模式。

如果您确实需要 AIDL,那么您应该能够在 AIDL 文件中显式导入 Parcelable 类,以便可以引用它。上面显示的 AIDL 接口没有此导入语句——虽然普通 Java 可能不需要它,但 AIDL 需要它,因为 AIDL 不会自动导入类。

I have an .aidl file that describes
the interface that the service
exposes.

Why? You only need that for a remote service, and you only need a remote service if you are exposing an API to third-party applications.

If your service is a local service, get rid of the AIDL and use the local binding pattern instead.

If you truly do need AIDL, then you should be able to explicitly import your Parcelable class in your AIDL file, so it can be referenced. Your AIDL interface shown above does not have this import statement -- while it might not be needed for ordinary Java, it is needed for AIDL, since AIDL does not auto-import classes.

慕巷 2024-09-10 08:38:17

我认为 Message 是你的类,而不是 android.os.Message。如果它是一个自定义类,那么问题可能在于想要检索该类的活动现在不是 Message 类。确保此活动在类路径中包含此类。

I think Message is your class and not android.os.Message. If it is a custom class, then maybe it's the problem that the activity which wants to retrieve this class doesn't now the class Message. Make sure that this activity has this class in the classpath.

疯狂的代价 2024-09-10 08:38:17

您需要在同一个包中为您的 Message 类创建一个aidl 文件,如下所示:

package mypackage;

parcelable MyClass;

然后您可以在服务aidl 中使用导入。

You need to create a aidl file for your Message class in the same package, like this:

package mypackage;

parcelable MyClass;

Then you will can use the import in the service aidl.

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