0

我的主要活动中有一个名为“pinNumberConverted”的全局变量。我需要在另一类可运行类型中获取此变量的值(内部有一个服务器套接字)。我该怎么做?这是主要活动:

public class AndroidServer2 extends Activity {

  Random pin_generator = new Random();
  public int pin_number = pin_generator.nextInt(9000-1000 +1) +1000; //generate a random code for the first connection with the kiosk
  public String pinNumberConverted = String.valueOf(pin_number);
  Thread fst = new Thread(new MyServer()); //declaration of a new thread

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_android_server2);
    initialize();
}

@Override
protected void onResume(){
    super.onResume();
    Utilities utilities = new Utilities();
    utilities.connectionInfo(this, wiFi, wifiInfo, ipAddress, wirelessName, pin, ipAddressConversion, serverIp, pin_number);
    if (fst.isAlive() == false){
        fst.start();
    }
  }
} 

这是可运行的类:

public class MyServer implements Runnable{

    //Need to pass the information taken from connection info method in Utilities.java
    public Handler handler = new Handler();
    public ServerSocket serverSocket;
    MyServerMethods myServerMethods = new MyServerMethods();
    AndroidServer2 androidServer2 = new AndroidServer2(); //this doesn't work and give error in runtime

    @Override
    public void run() {
        try{
          ServerSocket parent = new ServerSocket(); //create a new socket
          parent.setReuseAddress(true);
          parent.bind(new InetSocketAddress(serverPort)); //bind the server port and reuse it if necessary
            if ( serverIp != null){
                Log.i("Status","READY");
                while (true){
                    Socket client = parent.accept(); //accept the incoming connection
            //Here i need to take the value of the global variable from the main activity               
               } catch (Exception e) {
            e.printStackTrace();
        }
      }
}

谢谢

4

0 回答 0