protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ipEditText = findViewById(R.id.ipEditText); portEditText = findViewById(R.id.portEditText); Button connectButton = findViewById(R.id.connectButton); connectButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String ip = ipEditText.getText().toString(); int port = Integer.parseInt(portEditText.getText().toString()); new ConnectTask().execute(ip, String.valueOf(port)); } }); Button sendMessageButton = findViewById(R.id.sendMessageButton); sendMessageButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { showSendMessageDialog(); } }); } private class ConnectTask extends AsyncTask<String, Void, Void> { @Override protected Void doInBackground(String... params) { String ip = params[0]; int port = Integer.parseInt(params[1]); try { socket = new Socket(ip, port); in = new BufferedReader(new InputStreamReader(socket.getInputStream())); out = new PrintWriter(socket.getOutputStream(), true); // 在 UI 线程中创建 SeekBar handler.post(new Runnable() { @Override public void run() { SeekBar progressBar = findViewById(R.id.progressBar); progressBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { @Override public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { // 计算百分比对应的数字 int number = progress * 10; // 在 UI 线程中更新 ProgressBar 的进度 handler.post(new Runnable() { @Override public void run() { seekBar.setProgress(number); } }); out.println(number); } @Override public void onStartTrackingTouch(SeekBar seekBar) {} @Override public void onStopTrackingTouch(SeekBar seekBar) {} }); } }); } catch (UnknownHostException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return null; } @Override protected void onPostExecute(Void aVoid) { // 在连接结束后做一些处理 // 比如关闭连接或者显示连接成功的提示 } }代码修改
时间: 2024-02-25 20:59:21 浏览: 66
Android中Activity的生命周期探讨
在 ConnectTask 的 doInBackground 方法中,将 SeekBar 的创建和更新进度的操作都放在了一个 handler.post 的 Runnable 中,从而保证了在子线程中进行操作,避免了在 UI 线程中进行耗时操作的问题。
```
private class ConnectTask extends AsyncTask<String, Void, Void> {
@Override
protected Void doInBackground(String... params) {
String ip = params[0];
int port = Integer.parseInt(params[1]);
try {
socket = new Socket(ip, port);
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
out = new PrintWriter(socket.getOutputStream(), true);
// 在子线程中创建 SeekBar
final SeekBar progressBar = new SeekBar(MainActivity.this);
progressBar.setMax(10);
progressBar.setProgress(0);
// 在 UI 线程中添加 SeekBar 到布局中
handler.post(new Runnable() {
@Override
public void run() {
LinearLayout layout = findViewById(R.id.layout);
layout.addView(progressBar);
}
});
progressBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
// 计算百分比对应的数字
int number = progress * 10;
// 在子线程中更新 ProgressBar 的进度
handler.post(new Runnable() {
@Override
public void run() {
progressBar.setProgress(progress);
}
});
out.println(number);
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {}
});
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
// 在连接结束后做一些处理
// 比如关闭连接或者显示连接成功的提示
}
}
```
阅读全文