10

i am new programmer.i would like to implement sample application for getting chat by using xmpp server.In this implementation i have created connection by using ConnectionConfiguration object as follows :

ConnectionConfiguration connConfig =new ConnectionConfiguration(host, Integer.parseInt(sport), service);

I am passing connConfig object to XMPPConnection class by calling connect method i am getting connection and by calling login method passing with user name pand password then i am login to password.to login i am using a button.When i clicked on button i am using Intent for change the activity.One i am changing activity i would like to get the same connection in another activity.

I have written code for LoginActivity as follows:

  public class LoginActivity extends Activity
 {

ConnectionConfiguration connConfig ;

 XMPPConnection connection;



  @Override
 protected void onCreate(Bundle savedInstanceState) 
  {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.setting);


    ((Button)findViewById(R.id.login)).setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View arg0) 
           {

             connConfig =new ConnectionConfiguration(host, Integer.parseInt(sport), service);

          connection = new XMPPConnection(connConfig);

            connection.connect();
            connection.login(uname, password);

        }
});

 }
}

I have written ChatPageActivity as follows:

     public class ChatPage extends Activity {

@Override
public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    setContentView(R.layout.chatpage);

    //How to get the same XMPPConnection from LoginActivity here    

     }
  }

how to get the same connection from LoginActivity to ChatPageActivity?

please any body help me

prasad.gai
  • 2,977
  • 10
  • 58
  • 93

1 Answers1

16

Create a new class (inside a new .java file), using the singleton pattern (http://en.wikipedia.org/wiki/Singleton_pattern), where you can keep the current active connection accessible from any point of your application.

Possible solution:

public class XMPPLogic {

  private XMPPConnection connection = null;

  private static XMPPLogic instance = null;

  public synchronized static XMPPLogic getInstance() {
    if(instance==null){
      instance = new XMPPLogic();
    }
    return instance;
  }

  public void setConnection(XMPPConnection connection){
    this.connection = connection;
  }

  public XMPPConnection getConnection() {
    return this.connection;
  }

}

Then, on your LoginActivity you set the connection:

XMPPLogic.getInstance().setConnection(connection);

And in the ChatPage you get it:

XMPPLogic.getInstance().getConnection().doStuff()
Tiago Simão
  • 797
  • 7
  • 5
  • How can i maintain more XMPPConnections in the same application, through the application i want to get a particular connection. how can i maintain like this.. – RajaReddy PolamReddy Jul 24 '12 at 10:35
  • How is that particular collection identified? By a string? I'll just assume so. You can do that by changing this "private XMPPConnection connection = null;" to this "private Map connection = new HashMap();" and change the getter and the setter to get the connection name (the string)... you'll just store the connections in the store. – Tiago Simão Jul 25 '12 at 11:07
  • i tried by using ArrayList with index but no luck. that gives index outoff size exception, i will try like this.. – RajaReddy PolamReddy Jul 25 '12 at 11:13
  • @TiagoSimão. Hi can you please help me in implementing Connection Listener for XMPP connection. It is not working in my case – Gaurav Arora Mar 11 '13 at 06:03
  • can we try using binder to get access to service and then access connection ? – Harshit Feb 17 '14 at 07:16
  • I'm facing problem when i'm logged out i mean close the smack connection that time push message are receiving from FCM even though user is not logged in. how can i resolve please help me?? – Vishal Patoliya ツ Jan 25 '17 at 12:45