本文介绍了请XMPP连接(使用asmack)在Android活着的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发了接收通过XMPP推送通知的应用程序(我知道C2DM,但它有一定的局限性,我不能用的,因为它),问题是,经过一段时间是垃圾回收的连接和我不能发送推送通知到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.

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

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?

推荐答案

我不知道,如果垃圾回收是正确的术语在这里。它更可能是您的活动简单地被搭载Android关闭,因为您在一个活动的连接。

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.

但是,你是对的,为了保持你需要把XMPP连接到服务。但要确保连接是一个额外的线程,因为本身就是一个服务是不是一个额外的进程或线程。可以这样做,例如用处理程序

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);

然后就可以通过邮件/意图的服务线程通信。另一种选择将是一个活页夹接口。

您也可以对别人是如何做到这一点看看:里奇 - 毕姆和的是实现与Android的服务的XMPP连接这两个开源项目。GTalkSMS使用<一个href="http://$c$c.google.com/p/gtalksms/source/browse/src/com/google$c$c/gtalksms/MainService.java#115">IntentService方法,而里奇 - 毕姆使用了Binder与XmppConnection通信。

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.

请注意,我们achive一个pretty的与GTalkSMS稳定的连接,但它从来没有这么好股票的GTalk服务/客户端。其中一个原因是嫌默认的保持检查时间为5分钟,这是通常用于移动设备,当你想保存尽可能多的电池,可能的方式。因此,我不得不将它设置为一个较高的值,其中附带了一些缺点。你将永远有一个快速,反应灵敏和稳定的连接与电池续航时间之间的权衡,当谈到这一点。

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.

这篇关于请XMPP连接(使用asmack)在Android活着的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 14:02