2012-05-16 38 views
0

如何傳遞對象流(exmple:的BufferedReader或DataOutputStream類Ñ等),以其他活動(從Client_layoutActivity.class到chat_wall.class)如果我使用意圖方法這樣的:通對象流使用意圖方法

 Intent i = new Intent(Client_layoutActivity.this, chat_wall.class); 
     startActivity(i); 

我做了一個由幾個頁面組成的聊天應用程序。 第一頁是登錄頁面。在第一頁中,客戶端使用套接字與服務器進行通信。 登錄過程成功後,服務器將發送「登錄成功」之後,客戶端將從第一頁(登錄頁面)更改爲第二頁面(聊天牆頁面)我的意思是從第一頁使用套接字,BufferedReader和DataOuputstream方法(我假設套接字登錄過程仍然連接,所以我仍然可以使用此套接字在第二頁進行通信 - 聊天過程)。所以我想傳遞對象Socket,BufferedReader和DataOutputstream到第二頁來使用這個。我寫我的代碼波紋管:

PAGE 1:登錄目的

public void login(){ 
    try { 
    String name  = usrname.getText().toString(); // usrname is android object edit 
                 text 
    String pass  = password.getText().toString();// password is android 
                   object edit text 

    String sending = "login."+name + "." + pass + "." +"\n"; 
    System.out.println("Sending Message: "+sending); 

    Socket clientsock = new Socket("192.168.136.6", 28000); 
    System.out.println("connect to the server..."); 

    BufferedReader in = new BufferedReader(new 
    InputStreamReader(clientsock.getInputStream())); 
    DataOutputStream out = new DataOutputStream(clientsock.getOutputStream()); 

    out.writeBytes(sending);  

    String line = in.readLine(); 

    if (line.contentEquals("login success")){ 
     Toast.makeText(this, "login success", Toast.LENGTH_SHORT).show(); 
     Toast.makeText(this, "receive data from 
    server:"+clientsock.getInetAddress(), Toast.LENGTH_SHORT).show(); 
     Intent i = new Intent(Client_layoutActivity.this, chat_wall.class); 
     startActivity(i); 

    } else { 
     Toast.makeText(this, "Error ", Toast.LENGTH_SHORT).show(); 
    } 


} 
catch (Exception e) 
{ 
    System.out.println(e); 
    System.out.println("Connection Failed"); 
} 

第2頁:用於聊天目的

package com.willis.layout; 

import java.io.*; 
import java.net.*; 

import android.widget.*; 
import android.os.Bundle; 
import android.view.*; 
import android.view.View.OnClickListener; 
import android.app.Activity; 
import android.R.integer; 

public class chat_wall extends Activity { 

Client_layoutActivity ob; 
public EditText chatroom;  
    public Button sendbtn; 

public void onCreate(Bundle savedInstanceState){ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.chatwall);  

    sendbtn = (Button)findViewById(R.id.sendmsg); 
    sendbtn.setOnClickListener(new OnClickListener() { 
     public void onClick(View view) {     

       sendingmsg();           
      } 
     }); 

chatroom = (EditText)findViewById(R.id.chatroom);  

} 

public void sendingmsg(){  

    try 
    { 


     BufferedReader in = new BufferedReader(new 
      InputStreamReader(clientsock.getInputStream())); 
     DataOutputStream out = new DataOutputStream(clientsock.getOutputStream()); 

    String name  = chatroom.getText().toString();    
    String send = "chat."+name+". \n"; 

     System.out.println("Sending message: "+send); 
     out.writeBytes(send); 

     String msgin = in.readLine(); 
     Toast.makeText(this, msgin, Toast.LENGTH_SHORT).show(); 

    } 
    catch (Exception e) 
    { 
     System.out.println(e); 
     System.out.println("Connection Failed"); 
    } 
    } 

    } 
+0

嘗試http://stackoverflow.com/questions/2139134/how-to-send-an-object-from- one-android-activity-to-another-using-intents – Som

+0

你不能使用Intents。 – L7ColWinters

+0

可以請你給我們提供什麼,你正在試圖做的對象流(爲什麼它從一個活動到下一個?) – L7ColWinters

回答

1

正如我理解你想要的是使用用於登錄和聊天的相同套接字。取而代之的活動之間發送Socket對象與Intent的考慮另一種選擇:

  1. 定義一個類來管理打開的套接字,並創建它的靜態實例或使一個Singleton。
  2. 按照documentation中所述使用本地Service。定義你的sendingmsglogin方法,並與Activity在其綁定的onResume

    private ChatService mService; 
    ... 
    @Override 
    protected void onResume() { 
        doBindService(); 
        sendbtn.setOnClickListener(new OnClickListener() { 
         public void onClick(View view) { 
          mService.sendingmsg(); 
         } 
        }); 
    } 
    
    @Override 
    protected void onPause() { 
        doUnbindService(); 
    }