优化这段代码package com.lili.esp8266; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.ImageView; import android.widget.Toast; // 主页面 public class MainActivity extends Activity { public ImageView ledImageView; public Button connectButton; // 连接esp8266 public Button openOrCloseLEDButton; // 打开或者关闭LED按钮 private ConnectThread connectThread; public boolean isConnected; public boolean isOpenLED; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); connectThread = new ConnectThread(this, "192.168.4.1", 80); // 开启连接线程 connectThread.start(); } private void initView() { // LED灯图标 ledImageView = (ImageView) findViewById(R.id.image); connectButton = findViewById(R.id.button_connect); openOrCloseLEDButton = findViewById(R.id.openOrCloseLED); } // 连接到esp8266 public void connect(View view) { // 如果没有连接 if (!isConnected) { connectThread.startConnect = true; } else { try { connectThread.closeConnect(); } catch (Exception e) { e.printStackTrace(); } } } // 打开或者关闭LED public void openOrCloseLED(View view) { // 如果已经连接到esp8266 if (isConnected) { // 如果已经打开LED if (isOpenLED) { // 关闭LED connectThread.closeLED(); } else { // 开启LED connectThread.openLED(); } } else { Toast.makeText(this, "未连接到服务器!", Toast.LENGTH_SHORT).show(); } } }
时间: 2023-06-09 09:02:04 浏览: 98
这段代码可以进行如下优化:
1. 将 findViewById 方法的调用统一放到 initView 方法中,以提高代码的复用性和可读性。
2. 将 connectThread 的创建和启动放到 connect 方法中,以便在需要连接时才创建线程。
3. 将 isConnected 和 isOpenLED 的初始值都设置为 false,避免出现不必要的错误。
4. 在 connect 方法中增加连接成功或失败的提示信息。
5. 在 openOrCloseLED 方法中增加 LED 状态的修改,并对操作成功或失败进行提示。
优化后的代码如下:
package com.lili.esp8266;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
// 主页面
public class MainActivity extends Activity {
public ImageView ledImageView;
public Button connectButton; // 连接esp8266
public Button openOrCloseLEDButton; // 打开或者关闭LED按钮
private ConnectThread connectThread;
private boolean isConnected = false;
private boolean isOpenLED = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
// LED灯图标
ledImageView = findViewById(R.id.image);
connectButton = findViewById(R.id.button_connect);
openOrCloseLEDButton = findViewById(R.id.openOrCloseLED);
}
// 连接到esp8266
public void connect(View view) {
// 如果没有连接
if (!isConnected) {
connectThread = new ConnectThread(this, "192.168.4.1", 80);
// 开启连接线程
connectThread.start();
Toast.makeText(this, "正在连接服务器,请稍候...", Toast.LENGTH_SHORT).show();
} else {
try {
connectThread.closeConnect();
} catch (Exception e) {
e.printStackTrace();
}
}
}
// 打开或者关闭LED
public void openOrCloseLED(View view) {
// 如果已经连接到esp8266
if (isConnected) {
// 如果已经打开LED
if (isOpenLED) {
// 关闭LED
if (connectThread.closeLED()) {
isOpenLED = false;
ledImageView.setImageResource(R.drawable.led_off);
Toast.makeText(this, "LED已关闭!", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "关闭LED失败,请重试!", Toast.LENGTH_SHORT).show();
}
} else {
// 开启LED
if (connectThread.openLED()) {
isOpenLED = true;
ledImageView.setImageResource(R.drawable.led_on);
Toast.makeText(this, "LED已打开!", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "打开LED失败,请重试!", Toast.LENGTH_SHORT).show();
}
}
} else {
Toast.makeText(this, "未连接到服务器!", Toast.LENGTH_SHORT).show();
}
}
}
阅读全文