在Android XMPP事件 [英] XMPP events on Android

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

问题描述

我试图建立一个后台进程,截获一个XMPP消息,并执行一个动作,我用asmack为主要XM​​PP库。我presume我需要一个broadcastReciever是响应特定的意图。现在的问题是如何提高意图是什么?它必须能够为这个功能是present在谷歌Talk客户端。 提前谢谢了。

I'm trying to develop a background process that intercepts a XMPP message and performs an action, I'm using asmack as the main XMPP library. I presume I need a broadcastReciever that responds to a specific intent. The question is how to raise the intent? It must be possible as this functionality is present in the google talk client. many thanks in advance.

推荐答案

如果你真正想实现这种行为,你可能会认为有关运行asmack XMPP客户端的持久后台服务。您的XMPP客户端的监听方法(即processPacket)可能引发的意图。然后,您可以通过使用一个BroadcastReceiver抓住这个意图从另一个应用程序或在此应用程序。

If you really want to achieve this behavior, you might think about a persistent background service running the asmack XMPP client. The listener method (i.e. processPacket) of your XMPP client could raise an intent. You could then catch this intent from another application or within this application by using a BroadcastReceiver.

final Context context = getContext(); // or getApplicationContext(). context must be final.
PacketFilter packetFilter = new MessageTypeFilter(Message.Type.chat);
connection.addPacketListener(new PacketListener() {
    @Override
    public void processPacket(Packet packet) {
        Message message = (Message) packet;
        if (message.getBody() != null) {
            String from = StringUtils.parseBareAddress(message.getFrom());
            Intent intent = new Intent();
            intent.setAction("your.package.XMPP_PACKET_RECEIVED");
            intent.putExtra("from", from);
            intent.putExtra("body", message.getBody());
            context.sendBroadcast(i);
        }
    }
}, packetFilter);

您也可以尝试通过创建一个BroadcastReceiver(或IntentService)接收的意图,并通过XMPP将其发送给执行其他通信方向。一个BackgroundReceiver必须为每一个消息,该消息将是缓慢的,但节能减排的新连接(也没有必要保持XMPP会话还活着)。

You could also try to implement the other communication direction by creating a BroadcastReceiver (or IntentService) that receives an intent and sends it via XMPP. A BackgroundReceiver would have to create a new connection for each message which would be slow but energy saving (there is no need to keep the XMPP session alive).

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

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