如何使用Java

时间:2019-10-20 14:52:19

标签: java android android-studio

代码从套接字接收消息后,我正在上课。我想展示一下。我已经尝试使用private static导入它,但是当我想使用功能settext时,它将停止应用程序。如果不可能的话,我该如何返回变量并将其在主类中正常地返回到settext的{​​{1}}中。

textview

要使用私有静态信息导入class Serveur implements Runnable { private Exception exception; private DataInputStream dis; Handler handler = new Handler( ); public void run() { System.out.println( "start serveur" ); handler.post( new Runnable() { @Override public void run() { Toast.makeText(getApplicationContext(),"start message", Toast.LENGTH_LONG).show(); } } ); try { socketserveur = new ServerSocket(5000); String line = "no"; while (line == "no") { try { sock = socketserveur.accept(); dis = new DataInputStream(sock.getInputStream()); final String message_entrant21; message_entrant21 = dis.readLine(); handler.post( new Runnable() { @Override public void run() { Toast.makeText(getApplicationContext(), message_entrant21, Toast.LENGTH_LONG).show(); } } ); }catch (Exception i){ System.out.println( i ); handler.post( new Runnable() { @Override public void run() { Toast.makeText(getApplicationContext(),"error", Toast.LENGTH_LONG).show(); } } ); } } } catch (IOException e) { e.printStackTrace(); } } }

textview

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

我已经编辑了整个答案:该答案包含一个示例代码,该示例代码启动后台线程并通过定期更新来更新Activity's TextView

MainActivity代码

public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
private Handler myHandler;
private TextView textView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);
    textView = findViewById(R.id.textView);

    // In MyHandler class you will receive messages.
    myHandler = new MyHandler(new WeakReference<>(MainActivity.this));
    startThread();
}

private void startThread() {
    new Thread(new Serveur(myHandler) {
        @Override
        public void run() {
            super.run();
        }
    }).start();
}


private static class MyHandler extends Handler {

    private WeakReference<MainActivity> activityWeakReference;

    private MyHandler(WeakReference<MainActivity> activityWeakReference) {
        this.activityWeakReference = activityWeakReference;
    }

    @Override
    public void handleMessage(@NonNull Message msg) {
        if (null == activityWeakReference.get()) {
            Log.e(TAG, "handleMessage: Activity destroyed");
            return;
        }
        String string = (String) msg.obj;
        Log.e(TAG, "handleMessage: received string = " + string);
        activityWeakReference.get().textView.setText(string);

    }
  }

}

Serveur Class代码:

class Serveur implements Runnable {

private final Handler myHandler;


public Serveur(Handler uiHandler) {
    myHandler = uiHandler;
}


public void run() {
    System.out.println("start serveur");

    // simply loop and make Thread sleep in every iteration(you need to implement your task)
    for (int i = 1; i < 10; i++) {
        try {
            Thread.sleep(i * 500);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        // got some data ?? pass it via Message to handler.
        final Message message = Message.obtain();
        message.obj = "Read line" + i;  // wrap your data in message Object.
        myHandler.sendMessage(message); // this sent message will be received in MainActivity.MyHandler class. 

    }

  }
}

希望您的问题现在得到解决。