I am working on a Chat Application using XMPP
with asmack
. At startup Activity
I want to make XMPP
connection then use it in the different activities. But I have no idea how to do this. I searched a lot about it but couldn't find any desired solution. Please guide me.
我正在使用带有asmack的XMPP进行聊天应用程序。在启动Activity时,我想制作XMPP连接,然后在不同的活动中使用它。但我不知道该怎么做。我搜索了很多关于它但没有找到任何想要的解决方案。请指导我。
3 个解决方案
#1
2
You could make the Connection
member static and choose a access level modifier keyword that allows other Activities access that member.
您可以使Connection成员静态,并选择允许其他活动访问该成员的访问级别修饰符关键字。
But, you usually don't want to have the Connection
be handled within an Activity, but within an class that extends Service
. There are a bunch of open source apps out there that use aSmack, have a look at their source.
但是,您通常不希望在Activity中处理Connection,而是在扩展Service的类中处理。有很多开源应用程序使用aSmack,看看它们的来源。
#2
2
you can use this class to set the connection and get it anywhere in other activity
您可以使用此类来设置连接并将其在其他活动中的任何位置获取
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;
}
}
and set the connection like this..
并设置这样的连接..
XMPPLogic.getInstance().setConnection(connection);
and get connection like this..
并获得这样的连接..
connection = XMPPLogic.getInstance().getConnection();
#3
1
you can use singleton design pattern or make a utility class and define xmpp connection as static and then you can use it in any activity.
您可以使用单例设计模式或创建实用程序类并将xmpp连接定义为静态,然后您可以在任何活动中使用它。
for singleton do like this :
对于单身人士这样做:
public class MyConnection{
private static MyConnection con;
private MyConnection(){
//ToDo here
}
public static MyConnection getInstance()
{
if (con == null)
{
MyConnection= new MyConnection();
}
return con;
}
}
then in any activity you can access that object by typing MyConnection.getInstance();
然后在任何活动中,您可以通过键入MyConnection.getInstance()来访问该对象;
#1
2
You could make the Connection
member static and choose a access level modifier keyword that allows other Activities access that member.
您可以使Connection成员静态,并选择允许其他活动访问该成员的访问级别修饰符关键字。
But, you usually don't want to have the Connection
be handled within an Activity, but within an class that extends Service
. There are a bunch of open source apps out there that use aSmack, have a look at their source.
但是,您通常不希望在Activity中处理Connection,而是在扩展Service的类中处理。有很多开源应用程序使用aSmack,看看它们的来源。
#2
2
you can use this class to set the connection and get it anywhere in other activity
您可以使用此类来设置连接并将其在其他活动中的任何位置获取
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;
}
}
and set the connection like this..
并设置这样的连接..
XMPPLogic.getInstance().setConnection(connection);
and get connection like this..
并获得这样的连接..
connection = XMPPLogic.getInstance().getConnection();
#3
1
you can use singleton design pattern or make a utility class and define xmpp connection as static and then you can use it in any activity.
您可以使用单例设计模式或创建实用程序类并将xmpp连接定义为静态,然后您可以在任何活动中使用它。
for singleton do like this :
对于单身人士这样做:
public class MyConnection{
private static MyConnection con;
private MyConnection(){
//ToDo here
}
public static MyConnection getInstance()
{
if (con == null)
{
MyConnection= new MyConnection();
}
return con;
}
}
then in any activity you can access that object by typing MyConnection.getInstance();
然后在任何活动中,您可以通过键入MyConnection.getInstance()来访问该对象;