HashMap<String, Object> params = new HashMap<>();
params.put(CommandSender.JobRunning, isCurrentJobRunning);
params.put(CommandSender.isRightEye, isRightEye);
params.put("updateDistanceString", eyeXYZValText);
params.put("updateString", updateString);
Command
Sender.getInstance(MyTestActivity.this).sendCommand(CommandSender.RESULT_ROUTE, params);
抽取方法
要将这段代码抽取成方法,你可以在 `MyTestActivity` 类中创建一个专用的方法来构建参数并发送命令。以下是如何实现的示例:
### MyTestActivity 类中的抽取方法
```java
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import com.google.gson.Gson;
import java.util.HashMap;
import java.util.Map;
public class MyTestActivity extends AppCompatActivity {
private static final String TAG = "MyTestActivity";
private boolean isCurrentJobRunning;
private boolean isRightEye;
private String eyeXYZValText;
private String updateString;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_test);
// 示例参数
isCurrentJobRunning = true; // 假设这些值是从某处获取的
isRightEye = true;
eyeXYZValText = "Example eye XYZ value";
updateString = "Example update string";
// 发送命令
sendCommand();
}
private void sendCommand() {
HashMap<String, Object> params = new HashMap<>();
params.put(CommandSender.JobRunning, isCurrentJobRunning);
params.put(CommandSender.isRightEye, isRightEye);
params.put("updateDistanceString", eyeXYZValText);
params.put("updateString", updateString);
CommandSender.getInstance(this).sendCommand(CommandSender.RESULT_ROUTE, params);
}
}
```
### CommandSender 工具类
确保 `CommandSender` 工具类已经正确设置,并且可以发送命令。以下是 `CommandSender` 类的示例:
```java
import android.content.Context;
import com.github.pwittchen.pigeon.Pigeon;
import com.github.pwittchen.pigeon.library.PigeonBuilder;
import com.google.gson.Gson;
import java.util.HashMap;
import java.util.Map;
public class CommandSender {
private static CommandSender instance;
private static final String AUTHORITY = "com.hztc.iflashsee";
private static final String RESULT_ROUTE = "/response/result";
private final Pigeon flyPigeon;
private final Gson gson = new Gson();
private CommandSender(Context context) {
flyPigeon = Pigeon.newBuilder(context).setAuthority(AUTHORITY).build();
}
public static synchronized CommandSender getInstance(Context context) {
if (instance == null) {
instance = new CommandSender(context);
}
return instance;
}
public void sendCommand(String route, Map<String, Object> params) {
HashMap<String, Object> paramsMap = new HashMap<>(params);
String json = gson.toJson(paramsMap);
Log.e("CommandSender", "sendCommand: " + json);
Bundle bundleSend = new Bundle();
for (Map.Entry<String, Object> entry : paramsMap.entrySet()) {
bundleSend.putString(entry.getKey(), String.valueOf(entry.getValue()));
}
Bundle bundle = flyPigeon.route(route).withBundle("Command", bundleSend).fly();
String result = bundle.getString("responseResult");
Log.e("CommandSender", "Command result: " + result);
}
}
```
### 注意:
1. **MyTestActivity 类**:在这个类中,我们创建了一个 `sendCommand` 方法,它构建了一个参数 `HashMap` 并调用 `CommandSender` 的 `sendCommand` 方法。
2. **CommandSender 类**:这是一个单例工具类,负责发送命令。它使用 Gson 库将参数转换为 JSON 字符串,并发送命令。
确保你已经添加了必要的依赖项到你的 `build.gradle` 文件中,例如 Gson 和 Pigeon 库。
**Gson 库依赖**(如果使用 Gradle):
```gradle
dependencies {
implementation 'com.google.code.gson:gson:2.8.6'
}
```
**Pigeon 库依赖**(如果使用 Gradle):
```gradle
dependencies {
implementation 'com.github.pwittchen:pigeon:1.0.0'
}
```
通过这种方式,你可以将发送命令的逻辑封装在一个单独的方法中,使代码更加模块化和可重用。