如何从一个活动到另一活动获得相同的 xmpp 连接?
我是新程序员。我想实现使用 xmpp 服务器获取聊天的示例应用程序。在此实现中,我使用 ConnectionConfiguration 对象创建了连接,如下所示:
ConnectionConfiguration connConfig =new ConnectionConfiguration(host, Integer.parseInt(sport), service);
我通过调用 connect 方法将 connConfig 对象传递给 XMPPConnection 类,我正在获取连接并通过调用登录方法传递用户名和密码,然后我登录到密码。要登录,我正在使用一个按钮。当我单击按钮时,我正在使用意图来更改活动。一个我正在更改我想要获得的活动相同另一个活动中的连接。
我为LoginActivity编写了代码,如下所示:
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获得相同的连接?
请任何人帮助我
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用单例模式创建一个新类(在新的 .java 文件中)(http://en.wikipedia .org/wiki/Singleton_pattern),您可以在其中保持从应用程序的任何点访问当前活动连接。
可能的解决方案:
然后,在您的 LoginActivity 上设置连接:
并在 ChatPage 中您得到它:
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:
Then, on your LoginActivity you set the connection:
And in the ChatPage you get it: