如何从一个活动到另一活动获得相同的 xmpp 连接?

发布于 2024-12-06 10:23:57 字数 1504 浏览 1 评论 0原文

我是新程序员。我想实现使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦在深巷 2024-12-13 10:23:57

使用单例模式创建一个新类(在新的 .java 文件中)(http://en.wikipedia .org/wiki/Singleton_pattern),您可以在其中保持从应用程序的任何点访问当前活动连接。

可能的解决方案:

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 上设置连接:

XMPPLogic.getInstance().setConnection(connection);

并在 ChatPage 中您得到它:

XMPPLogic.getInstance().getConnection().doStuff()

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()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文