如何在MainActivity和不具有onCreate的类之间共享变量

时间:2019-03-13 18:02:58

标签: android

我需要与TcpClient类共享MainActivity中的变量“ mconfigipaddress”,但是该类没有onCreate,当我尝试使用Intent进行操作时会抛出错误,但我也需要这样做,但是不用离开MainActivity,有人可以告诉我该怎么做?

这是MainActivity的代码:

public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {

private TcpClient mTcpClient;

public static String mconfigipaddress = "192.168.4.1";
public static String mconfigport = "8888";
.
.
    @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate( savedInstanceState );
    setContentView( R.layout.activity_main );
.
.
    Intent intent = new Intent( this, TcpClient.class );
    intent.putExtra( "mconfigipaddressx", mconfigipaddress );
    intent.putExtra( "mconfigportx", mconfigport );
    startActivity( intent );
.
.

这是TcpClient的代码:

public class TcpClient {

private String  mconfigipaddress = ""; 
private String  mconfigport = ;
private String  mServerMessage;                        
private OnMessageReceived mMessageListener = null;    
private boolean mRun = false;                         
private PrintWriter mBufferOut;                       
private BufferedReader mBufferIn;                     
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;
sendMessage(Constants.CLOSED_CONNECTION+": " + Modelox);                
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);      
    Log.e("TCP Client", "C: Connecting...");
    Socket socket = new Socket(serverAddr, SERVER_PORT);            
    try {
        //sends the message to the server
        mBufferOut = new PrintWriter(new BufferedWriter(new 
 OutputStreamWriter(socket.getOutputStream())), true);
        getDeviceName();
        mBufferIn = new BufferedReader(new 
 InputStreamReader(socket.getInputStream()));
        sendMessage(Constants.LOGIN_NAME+": "+ Modelox);                   

        statusWIFIX = true;

        while (mRun) {                                              
            mServerMessage = mBufferIn.readLine();
            if (mServerMessage != null && mMessageListener != null) {
                mMessageListener.messageReceived(mServerMessage);   
            }
        }
        Log.e("RESPONSE FROM SERVER", "S: Received Message: '" + 
  mServerMessage + "'");
    } catch (Exception e) {
        Log.e("TCP", "S: Error", e);
    } finally {
        socket.close();                                             

    }

  } catch (Exception e) {
    Log.e("TCP", "C: Error", e);
  }
}

public interface OnMessageReceived {                                    
public void messageReceived(String message);                        
}

这是错误: enter image description here

1 个答案:

答案 0 :(得分:1)

TCPClient不是活动,因此不能以startActivity(Intent intent)开始。尝试为TCPClient类构建一个service