Okan
Okan

Reputation: 1389

Android thread management

I am connecting xmpp server with using threads but i am curious about one thing.I am connecting with this code in onResume method:

Thread t=new Thread(new Runnable() {
        @Override
        public void run() {
            ConnectionConfiguration connConfig=new ConnectionConfiguration("SERVER IP", 5222,"localhost");
            XMPPConnection connection=new XMPPTCPConnection(connConfig);
            connection.connect();
        }
    });
    t.start();

Then if user pauses the app i am disconnecting with this code in onPause

Thread t=new Thread(new Runnable() {

    @Override
    public void run() {
            connection.disconnect();
    }
});
t.start();

So,i am creating new threads on each home button press.Is this a good approach ? I don't know maybe it will be cause memory leak ? What do you suggest for this operation.

Upvotes: 0

Views: 80

Answers (2)

Renato Probst
Renato Probst

Reputation: 6054

I have done similar things, not with XMPP, but socket and it worked. You should create a Static class for your XMPPConnection, to make sure you are always working with only one (and the same) connection. When using socket, i found out that sometimes my connection was closed on object, but still active on network layer, and i have double messages (two connections at same time).

public class XMPPChatSingleton {

 public static XMPPChatSingleton instance;
 private XMPPConnection xmpp;
 private Activity activity;
 ProgressDialog pdialog;
 int timeOut;

public static XMPPChatSingleton getInstance(Activity activity, boolean reconnect) {

    if (instance == null || reconnect) {
        instance = getSync(activity, event);
    }

    return instance;
}

public static synchronized XMPPChatSingleton getSync(Activity activity, Event event) {
    instance = new XMPPChatSingleton (activity, event);

    return instance;
}

public XMPPConnection getSocket() {
    return this.socket;
}

private XMPPChatSingleton (Activity activity, Event event) {
    this.activity = activity;
    this.context = activity.getApplicationContext();
    this.event = event;
    this.xmpp = getChatXMPP();
}

 private getChatXMPP() {

   dialogLoad();
   xmpp = new XMPPConnection();
   // here you can handle your connection too;
   // use timeout to handle reconnection

 }

  public void disconnect() {

     xmpp.disconnect;
  }


}

The advantage of this approach is that you can reuse the same connection in all of yours activities and fragments. You can even create methods to handle message send. Pass and adapter to reflesh when a message arrive, and all kind of stuff.

I suggest you to get all data sent from server here and pass to the current active activity or fragment using context.sendBroadcast(dataYouWantToPass).

Upvotes: 1

testuserx
testuserx

Reputation: 226

I guess using AsyncTask is much efficient way to do this kind of stuff instead of manually creating threads. AsyncTask never blocks the UI thread and is much more secure.

Upvotes: 0

Related Questions