Quantcast
Channel: Ignite Realtime: Message List
Viewing all articles
Browse latest Browse all 11413

Re: Doubt about listeners

$
0
0

Short answer:

 

No,Yes

 

Long answer


You can add it after your connection is established.

 

Don't put any xmpp logic in your application activity, you need to create one or more classes to handle it.

For example you can create a XmppManager class so whenever you need something from xmpp you can call

the class

 

XmppManager.getInstance().Connect();

 

We will use getInstance() method to insure that we are dealing with same instance.

we will use volatile and synchronized if we will access the instance from different threads (ui thread, service or handler)

 

Example:

 

public class XmppManager {

    public static String HOST = "Server name";

    public static int PORT = 5223;

    public static String RESOURCE = "android";

    public XMPPConnection mConnection;

 

    public XmppManager() {

        SmackAndroid.init([your application context]);

    }

 

    private static volatile XmppManager Instance = null;

 

    public static XmppManager getInstance() {

        XmppManager localInstance = Instance;

        if (localInstance == null) {

            synchronized (XmppManager.class) {

                localInstance = Instance;

                if (localInstance == null) {

                    Instance = localInstance = new XmppManager();

                }

            }

        }

        return localInstance;

    }

 

//once your connection is Established you can add your listeners

 

private void onConnectionEstablished(){

        PacketFilter filter = new PacketTypeFilter(org.jivesoftware.smack.packet.Message.class);

        mConnection.addPacketListener(packetListener, filter);

}

}


Viewing all articles
Browse latest Browse all 11413

Trending Articles