new Thread(){ @Override public void run() { super.run(); getCpu(); str_memoryPercent = _memoryPercent.getUsedPercentValue(); Message msg = new Message(); msg.what = 2; handler.sendMessageDelayed(msg,0); } }.start(); Message msg = new Message(); isUpdate = false; msg.what = 1; handler.sendMessageDelayed(msg, 3000); 停止刷新这个
时间: 2024-01-11 07:05:51 浏览: 128
如果你想停止刷新这个,可以在 `handler` 中通过 `removeMessages` 方法移除相应的消息,例如:
```java
// 在适当的位置调用该方法来停止刷新
handler.removeMessages(1);
handler.removeMessages(2);
```
这样就可以停止发送编号为 1 和 2 的消息,从而停止刷新了。
相关问题
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 Thread(new Runnable() { @Override public void run() { try { socket = new Socket(ip, port); in = new BufferedReader(new InputStreamReader(socket.getInputStream())); out = new PrintWriter(socket.getOutputStream(), true); // 获取SeekBar对象 SeekBar progressBar = findViewById(R.id.progressBar); // 添加OnSeekBarChangeListener监听器 progressBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { @Override public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { // 计算百分比对应的数字 int number = progress * 10; 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(); } } }).start(); } }); Button sendMessageButton = findViewById(R.id.sendMessageButton); sendMessageButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { showSendMessageDialog(); } }); }主线程有问题,能帮我修改一下吗
可能是因为您没有在UI线程上更新UI。建议使用runOnUiThread方法将UI更新放在UI线程上。
以下是修改后的代码:
```java
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 Thread(new Runnable() {
@Override
public void run() {
try {
socket = new Socket(ip, port);
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
out = new PrintWriter(socket.getOutputStream(), true);
// 获取SeekBar对象
SeekBar progressBar = findViewById(R.id.progressBar);
// 添加OnSeekBarChangeListener监听器
progressBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
// 计算百分比对应的数字
int number = progress * 10;
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();
}
}
}).start();
}
});
Button sendMessageButton = findViewById(R.id.sendMessageButton);
sendMessageButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showSendMessageDialog();
}
});
}
// 在UI线程上更新UI
private void updateUI(Runnable runnable) {
runOnUiThread(runnable);
}
private void showSendMessageDialog() {
// 创建AlertDialog.Builder对象
AlertDialog.Builder builder = new AlertDialog.Builder(this);
// 设置标题和消息
builder.setTitle("Send Message");
builder.setMessage("Please enter the message:");
// 添加一个EditText
final EditText input = new EditText(this);
builder.setView(input);
// 添加确定按钮
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// 获取用户输入的文本
String message = input.getText().toString();
// 发送消息到服务器
out.println(message);
// 在UI线程上更新UI
updateUI(new Runnable() {
@Override
public void run() {
// 显示Toast提示消息发送成功
Toast.makeText(MainActivity.this, "Message sent successfully", Toast.LENGTH_SHORT).show();
}
});
}
});
// 添加取消按钮
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
// 显示对话框
AlertDialog dialog = builder.create();
dialog.show();
}
```
在这个修改后的代码中,我们添加了一个 `updateUI` 方法,用于在UI线程上更新UI。在 `showSendMessageDialog` 方法中,我们使用 `updateUI` 方法来显示Toast提示消息发送成功。
private EditText ipEditText; private EditText portEditText; private TextView messageTextView; private BufferedReader in; private PrintWriter out; private Socket socket; private Handler handler = new Handler(); @SuppressLint("MissingInflatedId") @Override 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); // 获取SeekBar对象 SeekBar progressBar = findViewById(R.id.progressBar); // 添加OnSeekBarChangeListener监听器 progressBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { @Override public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { // 计算百分比对应的数字 int number = progress * 10; int tnum = 0; tnum = number; out.println(tnum); } @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 onDestroy() { super.onDestroy(); try { if (in != null) { in.close(); } if (out != null) { out.close(); } if (socket != null) { socket.close(); } } catch (IOException e) { e.printStackTrace(); } } private void showSendMessageDialog() { AlertDialog.Builder builder = new AlertDialog.Builder(this); LayoutInflater inflater = getLayoutInflater(); View view = inflater.inflate(R.layout.dialog_send_message, null); final EditText messageEditText = view.findViewById(R.id.messageEditText); builder.setView(view) .setTitle("发送消息") .setPositiveButton("发送", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { String message = messageEditText.getText().toString(); if (out != null) { new Thread(new Runnable() { @Override public void run() { out.println(message); } }).start(); } } }) .setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.cancel(); } }) .create() .show(); }代码有问题
代码中存在一些问题:
1. 在 ConnectTask 类的 doInBackground 方法中,如果出现异常,不会抛出异常,也不会给用户任何反馈,需要添加一些异常处理代码。
2. 在 onDestroy 方法中,关闭 in、out 和 socket 时,应该先判断它们是否为 null,避免空指针异常。
3. 在 showSendMessageDialog 方法中,如果 out 为 null,应该给用户提示信息,告诉用户连接未建立。
4. 在 sendMessageButton 的点击事件中,不应该在子线程中调用 out 的 println 方法,因为 out 是在 doInBackground 方法中创建的,不在主线程中,需要使用 Handler 将消息发送到主线程再执行。
5. SeekBar 的 progress 值只能是 0 到 10,但是在 onProgressChanged 方法中,有计算 progress * 10,这是不正确的。
6. SeekBar.OnSeekBarChangeListener 中的方法 onStartTrackingTouch 和 onStopTrackingTouch 可以不用实现。
最后,建议将 Handler 定义在 Activity 中,避免内存泄漏。
阅读全文