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).
source share