在Android XMPP事件事件、Android、XMPP

由网友(床上轻喘)分享简介:我试图建立一个后台进程,截获一个XMPP消息,并执行一个动作,我用asmack为主要XM​​PP库。我presume我需要一个broadcastReciever是响应特定的意图。现在的问题是如何提高意图是什么?它必须能够为这个功能是present在谷歌Talk客户端。提前谢谢了。 I'm trying to dev...

我试图建立一个后台进程,截获一个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).

阅读全文

相关推荐

最新文章