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
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()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With