如何从一个活动同XMPP连接到另一个? [英] how to get same xmpp connection from one activity to another?
问题描述
我是新programmer.i想实现通过使用XMPP server.In这个实现我已创建使用ConnectionConfiguration对象连接如下越来越聊天示例应用程序:
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);
我传递connConfig对象XMPPConnection类通过调用connect方法我收到的连接,并通过调用登录方法,通过使用用户名p和密码,然后我登录到password.to登录我使用的是button.When我点击按钮我使用意向变革的activity.One我改变活动我想获得的另一项活动相同的连接。
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.
我已经写了$ C $下的 LoginActivity 如下:
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);
}
});
}
}
我写的 ChatPageActivity 如下:
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
}
}
如何从LoginActivity相同的连接ChatPageActivity?
how to get the same connection from LoginActivity to ChatPageActivity?
请任何机构可以帮助我
推荐答案
创建一个新类(在一个新的.java文件),使用单例模式(<一href="http://en.wikipedia.org/wiki/Singleton_pattern">http://en.wikipedia.org/wiki/Singleton_pattern),在这里你可以保留当前的活动连接从任何角度你的应用程序的访问。
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.
可能的解决方法:
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;
}
}
然后,在你的LoginActivity您设置的连接:
Then, on your LoginActivity you set the connection:
XMPPLogic.getInstance().setConnection(connection);
而在ChatPage你得到它:
And in the ChatPage you get it:
XMPPLogic.getInstance().getConnection().doStuff()
这篇关于如何从一个活动同XMPP连接到另一个?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!