在 Android 上保持 XMPP 连接(使用 asmack)处于活动状态

发布于 2024-12-08 13:32:48 字数 162 浏览 1 评论 0原文

我正在开发一个通过 XMPP 接收推送通知的应用程序(我知道 C2DM,但它有一些限制,因此我无法使用),问题是连接在一段时间后被垃圾收集,我不能向 Android 设备发送推送通知。

我认为我需要实现一个 Android 服务,但我不知道如何实现一个保持连接活动的服务。 有人可以帮助我吗?

I'm developing an application that receives push notifications via XMPP ( I know C2DM, but it has some limitations and I can't use because of it ), the problem is the connection that after some time is garbage collected and I can't send push notification to the Android device.

I think I need to implement an Android service but I have no idea how to implement a service that would keep the connection alive.
Somebody could help me?

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

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

发布评论

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

评论(1

一袭白衣梦中忆 2024-12-15 13:32:48

我不确定“垃圾收集”在这里是否是正确的术语。您的 Activity 更有可能被 Android 关闭,因为您在 Activity 中创建了连接。

但你是对的,为了保持稳定的连接,你需要将 XMPP 连接放入 Service。但请确保连接位于额外的线程中,因为服务本身并不是额外的进程或线程。例如,可以使用 Handler 来完成此操作。

HandlerThread thread = new HandlerThread(SERVICE_THREAD_NAME);
thread.start();
handlerThreadId = thread.getId();
serviceLooper = thread.getLooper();
serviceHandler = new ServiceHandler(serviceLooper);

然后您可以通过消息/意图与服务线程进行通信。另一种选择是 Binder 接口。

您还可以看看其他人是如何做到这一点的:BeemGTalkSMS 都是实现与 Android 服务的 XMPP 连接的开源项目。
GTalkSMS 使用 IntentService 方法,而 Beem使用 Binder 进行通信Xmpp 连接。

请注意,我们与 GTalkSMS 实现了相当稳定的连接,但它永远不如普通的 GTalk 服务/客户端那么好。原因之一是 smacks 默认的保持活动检查计时器为 5 分钟,对于移动设备来说,当您想尽可能节省电量时,这是经常使用的时间。所以我必须将其设置为更高的值,这会带来一些缺点。在此方面,您始终需要在快速、响应灵敏且稳定的连接与电池寿命之间进行权衡。

I am not sure if "garbage collected" is the right term here. It is more likely that your activity simply gets closed by Android, because you create the connection in an Activity.

But you are right, in order to keep a stable connection you need to put the XMPP connection into a Service. But make sure that the connection is in an extra thread, because a service for itself is not a extra process or thread. This could be done, for example with an Handler.

HandlerThread thread = new HandlerThread(SERVICE_THREAD_NAME);
thread.start();
handlerThreadId = thread.getId();
serviceLooper = thread.getLooper();
serviceHandler = new ServiceHandler(serviceLooper);

Then you can communicate with the Service Thread via messages/intents. Another alternative would be a Binder interface.

You could also have a look on how others do this: Beem and GTalkSMS are both open source projects that implement an XMPP connection with an Android service.
GTalkSMS uses the IntentService approach, whereas Beem uses an Binder to communicate with the XmppConnection.

Note that we achive a pretty stable connection with GTalkSMS, but it's never so good as the stock GTalk Service/Client. One reason is that smacks default keep-alive check timer is 5 minutes, which is way to often for a mobile device when you want to save as much battery as possible. So I had to set it to a higher value, which comes with some drawbacks. You will always have a trade-off between a fast, responsive and stable connection vs. battery life when it comes to this.

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