Android远程进程:messenger vs aidl?哪个更好?

发布于 2024-12-11 04:29:16 字数 171 浏览 3 评论 0原文

我目前有一个远程服务,可以与同一应用程序上的活动不断进行通信。

有时我需要大量传入和传出消息。哪种消息传递方法更好、更快且省电?使用消息或aidl接口?

截至目前,我正在使用 aidl 接口,但出于某种原因,我认为特别是在附加侦听器时会产生很多开销......

非常感谢任何反馈。

I currently have a remote service that communicates constantly with an activity on the same app.

I need to send messages in and out extensively at some times. Which method of messaging is better, faster, and battery friendly? Using messages or aidl interface?

As of now I'm using aidl interface but for some reason I think there is a lot of overhead specially when attaching listeners...

Any feedback greatly appreciated.

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

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

发布评论

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

评论(1

鲜血染红嫁衣 2024-12-18 04:29:16

我目前有一个远程服务,可以与同一应用上的活动不断通信。

远程服务通常不是一个好主意。

嗯,我的应用程序是一个多媒体音频应用程序,需要在后台运行,需要大量资源:内存。将其与我的活动放在同一进程中很可能会导致达到每个进程的资源使用限制的问题。

然后使用更少的内存。音频播放器不需要超过每个进程的 RAM 限制,即使有活动也是如此。

此外,这样做的好处是,如果您的 UI 由于某种原因崩溃,您的服务不会崩溃。

这并不是浪费设备 RAM 和 CPU 时间的特别好的理由。这对于服务器来说是一个很好的方法,但对于嵌入式系统来说就不那么好了。

哪种消息传递方法更好、更快且省电?使用消息或aidl接口?

它们应该具有相当的可比性。 IPC 考虑因素应该会淹没大多数其他影响。例如,引用我自己的一本书中的内容:

例如,在本书源代码的CPU-Java/AIDLOverhead目录中
代码中,你会发现两个项目实现了相同的无所事事
同等服务中的方法。一个使用AIDL并且绑定到远程
来自单独的客户端应用程序;另一个是客户端的本地服务
应用程序本身。然后客户端调用什么都不做的方法100万
两项服务各自的时间。平均而言,在三星 Galaxy Tab 上
10.1 中,远程服务 100 万次调用大约需要 170 秒,而
本地服务大约需要 170 毫秒。因此,开销为
单个远程方法调用很小(~170 微秒),但是
在循环中执行大量操作,或者当用户抛出 ListView 时,可能会变成
值得注意。

话虽这么说,如果您担心开销(但坚持使用远程服务),那么尝试这两种方法并使用日志记录来确定是否有明显的赢家可能会更好。

I currently have a remote service that communicates constantly with an activity on the same app.

A remote services is generally not a good idea.

Well, my app is a multimedia audio application that needs to run in the background which requires a lot of resources: memory. Having this in the same process as my activities would most likely bring issues to reaching the limit of resource usage per process.

Then use less memory. A audio player should not need to exceed the per-process RAM limits, even with activities.

Also, the benefits are, if your UI crashes for some reason your service doesn't.

This is not a particularly good reason for wasting device RAM and CPU time. That's a fine approach for a server, less so for an embedded system.

Which method of messaging is better, faster, and battery friendly? Using messages or aidl interface?

They should be fairly comparable. IPC considerations should swamp most other effects. For example, quoting myself from one of my books:

For example, in the CPU-Java/AIDLOverhead directory of the book's source
code, you will find a pair of projects implementing the same do-nothing
method in equivalent services. One uses AIDL and is bound to remotely
from a separate client application; the other is a local service in the client
application itself. The client then calls the do-nothing method 1 million
times for each of the two services. On average, on a Samsung Galaxy Tab
10.1, 1 million calls takes around 170 seconds for the remote service, while it
takes around 170 milliseconds for the local service. Hence, the overhead of
an individual remote method invocation is small (~170 microseconds), but
doing lots of them in a loop, or as the user flings a ListView, might become
noticeable.

That being said, if you are concerned about the overhead (yet insist on the remote service), you are probably better served trying both and using logging to determine if there is a clear winner.

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