XMPP events on Android

I am trying to develop a background process that intercepts an XMPP message and performs an action, I use asmack as the main XMPP library. I suppose I need a broadcaster that meets a specific intention. The question is how to raise an intention? This should be possible because this functionality is present in the Google Talk client. Thanks in advance.

+4
source share
2 answers

If you really want to achieve this behavior, you can think of a permanent background service working with the asmack XMPP client. The listener method (i.e. ProcessPacket) of your XMPP client may raise intent. You can then catch this intention from another application or in this application using 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); 

You can also try to implement a different direction of communication by creating a BroadcastReceiver (or IntentService) that receives the intention and sends it through XMPP. BackgroundReceiver will need to create a new connection for each message that will be slow but energy-saving (there is no need to keep the XMPP session alive).

+2
source

I guess I need a broadcast editor that responds to specific intentions.

Probably not. aSmack, apparently, is basically Smack, which has nothing to do with Android, and therefore has no idea about intentions.

This should be possible, as this functionality is present in the google talk client.

The Google Talk Client does not use Smack, AFAIK.

+1
source

Source: https://habr.com/ru/post/1335233/


All Articles