在Android的服务包监听器 [英] Packet Listener in Android Service

查看:113
本文介绍了在Android的服务包监听器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用于使用XMPP一个聊天窗口小部件,其拾取当它们被发送给用户的聊天消息创建服务。

我创建了一个服务,并在ONSTART我使用的AsyncTask连接到聊天服务器,然后建立一个packetlistener。

这里的$ C $下封隔器监听器:

 公共无效setConnection(XMPPConnection连接){
    如果(连接!= NULL){
        //添加一个数据包监听器被发送给我们的消息
        PacketFilter过滤器=新MessageTypeFilter(Message.Type.chat);
        connection.addPacketListener(新PacketListener(){
            公共无效processPacket(分组数据包){
                消息消息=(消息)数据包;
                如果(message.getBody()!= NULL){
                    字符串fromName = StringUtils.parseBareAddress(消息
                            。从获得());
                    Log.v(TAG,得到的答案:+ message.getBody());
                    // messages.add(fromName +:);
                    // messages.add(message.getBody());

                }
            }
        }, 过滤);
    }
}
 

问题是,它似乎停止在空闲一段时间后听。如果我发送聊天消息马上他们得到的输出。

时的服务如何停止不知?那是正确的地方放packerlistener?

感谢

解决方案
  

我创建了一个服务,并在ONSTART我使用的AsyncTask连接到聊天服务器,然后建立一个packetlistener。

我不建议。创建你自己的线程,而不是一个的AsyncTask 的AsyncTask 是专为事情会以毫秒为单位或几秒钟,而不是几分钟或几小时内结束。

  

时的服务如何停止以某种方式?

很可能。使用亚行logcat ,DDMS或DDMS透视图在Eclipse检查LogCat中,看看它会告诉你关于你的code。

您应该使用 startForeground()在你的服务,特别是如果你打算保持服务运行时的聊天客户端的活动不一定在前台。

I am trying to create a service for a chat widget using XMPP, which picks up chat messages when they are sent to the user.

I have created a service, and in the onStart I use a AsyncTask to connect to the chat server, and then sets up a packetlistener.

Here's the code for the packer listener:

public void setConnection(XMPPConnection connection) {
    if (connection != null) {
        // Add a packet listener to get messages sent to us
        PacketFilter filter = new MessageTypeFilter(Message.Type.chat);
        connection.addPacketListener(new PacketListener() {
            public void processPacket(Packet packet) {
                Message message = (Message) packet;
                if (message.getBody() != null) {
                    String fromName = StringUtils.parseBareAddress(message
                            .getFrom());
                    Log.v(TAG, "Got:" + message.getBody());
                    // messages.add(fromName + ":");
                    // messages.add(message.getBody());

                }
            }
        }, filter);
    }
}

The problem is it seems to stop listening after being idle for a while. If I send chat messages straight away they get output.

Is the service getting stopped somehow? Is that the right place to put the packerlistener?

Thanks

解决方案

I have created a service, and in the onStart I use a AsyncTask to connect to the chat server, and then sets up a packetlistener.

I do not recommend that. Create your own thread, not an AsyncTask. AsyncTask is designed for things that will end in milliseconds or seconds, not minutes or hours.

Is the service getting stopped somehow?

Quite possibly. Use adb logcat, DDMS, or the DDMS perspective in Eclipse to examine LogCat and see what it tells you about your code.

You should be using startForeground() in your service, particularly if you plan on keeping the service running when your chat client activity is not necessarily in the foreground.

这篇关于在Android的服务包监听器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆