0

how can I pass object stream (exmple: BufferedReader or DataOutputStream n etc) to other activity (from Client_layoutActivity.class to chat_wall.class ) if i use intent method like this:

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

I made a chat application that consists of several pages. The first page is the login page. in the first page, client use socket to communicated with server. After login process is success than server will send "login success". After that clien side will change layout from first page (login page) to second page (chat wall page). I mean to use socket, BufferedReader and DataOuputstream method from first page (i assume socket for login process is still connected so i can still used this socket to communicate in second page - chatting process). So i want to pass object Socket, BufferedReader and DataOuputstream to second page to used this. i write my code bellow:

PAGE 1 : for login purpose

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");
}

PAGE 2 : for Chat purpose

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");
    }
    }

     }
user1388581
  • 11
  • 1
  • 5

1 Answers1

1

As I understood what you want is to use the same socket for login and chat purposes. Instead of sending a Socket object with Intent between activities consider another options:

  1. Define a class to manage opened sockets and create it's static instance or make it a Singleton.
  2. Use a local Service as described in documentation. Define your sendingmsg and login methods in it and bind with Activity on it's 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();
    }
    
Andrey Ermakov
  • 3,298
  • 1
  • 25
  • 46