2011-09-27 8 views
9

私は新しいprogrammer.i iは次のようにConnectionConfigurationオブジェクトを使用して接続を作成しているserver.In XMPPを使ってチャットを取得するために、この実装をサンプルアプリケーションを実装したいと思います午前:私はXMPPConnectionにconnConfigオブジェクトを渡していますあるアクティビティから別のアクティビティへ同じxmpp接続を取得する方法は?

ConnectionConfiguration connConfig =new ConnectionConfiguration(host, Integer.parseInt(sport), service); 

を接続メソッドを呼び出すことによってクラス私は接続を取得していると私はボタンを使用してログインするpassword.toログインするユーザー名パンドパスワードで渡すログインメソッドを呼び出すことによって私はボタンをクリックしたとき私はインテントを変更するために使用しているactivity.One私は別のアクティビティで同じ接続を取得したいと思うアクティビティを変更しています。

次のように私はLoginActivity用のコードを書かれている:

 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  

    } 
    } 

をChatPageActivityへ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を書かれていますか?

すべてのボディは

答えて

14

はあなたのアプリケーションの任意のポイントからアクセスできる現在のアクティブな接続を保つことができるシングルトンパターン(http://en.wikipedia.org/wiki/Singleton_pattern)を使用して、(新しい.javaファイル内の)新しいクラスを作成し、私を助けてください。

考えられる解決策:

XMPPLogic.getInstance().setConnection(connection); 

そしてChatPageにあなたはそれを得る:

私はより多くを維持するにはどうすればよい
XMPPLogic.getInstance().getConnection().doStuff() 
+0

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には、接続を設定します同じアプリケーションのXMPPConnections、アプリケーションを介して私は特定の接続を取得したい。どのように私はこのように維持することができます.. –

+0

その特定のコレクションはどのように識別されますか?文字列で?私はちょうどそう仮定する。これを行うには、この "private XMPPConnection connection = null;"を変更します。この "プライベートマップ接続=新しいHashMap ();" getterとsetterを接続名(文字列)を取得するように変更すると、接続をストアに保存するだけです。 –

+0

私はインデックス付きのArrayListを使ってみましたが運がありません。インデックスアウトオフサイズの例外を与える、私はこのようにしようとします.. –

関連する問題