Tengo una variable que se captura en el MainActivity y los guardó en un sharedpreference y la proxima vez que lanzo la app puedo usar el valor de esta variable, pero necesito que esta misma variable también sea leída en una clase llamada TcpClient pero esta clase no tiene constructor, alguien me puede decir como hacer esto, la variable que quiero compartir es "mconfigipaddress".
Aqui esta mi MainActivity
public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
private TcpClient mTcpClient;
public static String SERVER_IP = "192.168.4.1";
public static String SERVER_PORT = "8888";
.
.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate( savedInstanceState );
setContentView( R.layout.activity_main );
.
.
sharedpreferences = getSharedPreferences( mypreference,Context.MODE_PRIVATE );
if (sharedpreferences.contains( SERVER_IP )) {
mconfigipaddress = sharedpreferences.getString( SERVER_IP, "");
}
if (sharedpreferences.contains( SERVER_PORT )) {
mconfigport = sharedpreferences.getString( SERVER_PORT, "");
}
Aqui esta el codigo del TcpClient:
public class TcpClient {
private String mconfigipaddress = ""; //your computer IP address
private String mconfigport = ;
private String mServerMessage; // message to send to the server
private OnMessageReceived mMessageListener = null; // sends message received notifications
private boolean mRun = false; // while this is true, the server will continue running
private PrintWriter mBufferOut; // used to send messages
private BufferedReader mBufferIn; // used to read messages from the server
private String Modelox="";
public Boolean statusWIFIX = false;
/**
* Constructor of the class. OnMessagedReceived listens for the messages received from server
*/
public TcpClient(OnMessageReceived listener) {
mMessageListener = listener;
}
/**
* Sends the message entered by client to the server
* @param message text entered by client
*/
public void sendMessage(String message) {
if (mBufferOut != null && !mBufferOut.checkError()) {
mBufferOut.println(message);
mBufferOut.flush();
}
}
interface TcpListener
{
void onReportStatusWIFIX(boolean statusWIFIX);
}
/**
* Close the connection and release the members
*/
public void stopClient() {
statusWIFIX = false;
Log.e( "DEBUG-->", "Coneccion Cerrada: "+ statusWIFIX );
sendMessage(Constants.CLOSED_CONNECTION+": " + Modelox); // send message that we are closing the connection
mRun = false;
if (mBufferOut != null) {
mBufferOut.flush();
mBufferOut.close();
}
mMessageListener = null;
mBufferIn = null;
mBufferOut = null;
mServerMessage = null;
}
public void run() {
mRun = true;
try {
InetAddress serverAddr = InetAddress.getByName(SERVER_IP); //here you must put your computer's IP address.
Log.e("TCP Client", "C: Connecting...");
Socket socket = new Socket(serverAddr, SERVER_PORT); //create a socket to make the connection with the server
try {
//sends the message to the server
mBufferOut = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())), true);
//receives the message which the server sends back
getDeviceName();
mBufferIn = new BufferedReader(new InputStreamReader(socket.getInputStream()));
sendMessage(Constants.LOGIN_NAME+": "+ Modelox); // send login name
statusWIFIX = true;
while (mRun) { //in this while the client listens for the messages sent by the server
mServerMessage = mBufferIn.readLine();
if (mServerMessage != null && mMessageListener != null) {
mMessageListener.messageReceived(mServerMessage); //call the method messageReceived from MyActivity class
}
}
Log.e("RESPONSE FROM SERVER", "S: Received Message: '" + mServerMessage + "'");
} catch (Exception e) {
Log.e("TCP", "S: Error", e);
} finally {
socket.close(); //the socket must be closed. It is not possible to reconnect to this socket
// after it is closed, which means a new socket instance has to be created.
}
} catch (Exception e) {
Log.e("TCP", "C: Error", e);
}
}
public interface OnMessageReceived { //Declare the interface. The method messageReceived(String message) will must be implemented in the MyActivity
public void messageReceived(String message); //class at on asynckTask doInBackground
}
TcpClient(OnMessageReceived listener)
de igual forma puedes crear otro consturctor que recibar la variable que necesitasTcpClient(OnMessageReceived listener)
de igual forma puedes crear otro consturctor que recibar la variable que necesitas