改进Termux应用调用函数,添加TermuxWorkSpaces按钮响应。
This commit is contained in:
@@ -1,8 +1,8 @@
|
||||
#Created by .winboll/winboll_app_build.gradle
|
||||
#Thu Apr 30 02:12:23 GMT 2026
|
||||
#Thu Apr 30 02:41:13 GMT 2026
|
||||
stageCount=26
|
||||
libraryProject=
|
||||
baseVersion=15.11
|
||||
publishVersion=15.11.25
|
||||
buildCount=46
|
||||
buildCount=54
|
||||
baseBetaVersion=15.11.26
|
||||
|
||||
@@ -15,6 +15,7 @@ public class MyTermuxActivity extends AppCompatActivity {
|
||||
|
||||
private Toolbar mToolbar;
|
||||
private Button mTermuxButton;
|
||||
private Button mTermuxWorkSpacesButton;
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
@@ -24,7 +25,7 @@ public class MyTermuxActivity extends AppCompatActivity {
|
||||
// 初始化工具栏
|
||||
initToolbar();
|
||||
// 初始化按钮
|
||||
initTermuxButton();
|
||||
initView();
|
||||
}
|
||||
|
||||
private void initToolbar() {
|
||||
@@ -33,38 +34,40 @@ public class MyTermuxActivity extends AppCompatActivity {
|
||||
setSupportActionBar(mToolbar);
|
||||
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
|
||||
mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
LogUtils.d(TAG, "点击返回按钮");
|
||||
finish();
|
||||
}
|
||||
});
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
LogUtils.d(TAG, "点击返回按钮");
|
||||
finish();
|
||||
}
|
||||
});
|
||||
LogUtils.d(TAG, "工具栏初始化完成");
|
||||
}
|
||||
}
|
||||
|
||||
private void initTermuxButton() {
|
||||
private void initView() {
|
||||
mTermuxButton = findViewById(R.id.btn_termux);
|
||||
if (mTermuxButton != null) {
|
||||
mTermuxButton.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
LogUtils.d(TAG, "点击 Termux 按钮");
|
||||
executeSampleCommand();
|
||||
}
|
||||
});
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
LogUtils.d(TAG, "点击 Termux 按钮");
|
||||
TermuxCommandExecutor.openTermuxBash(MyTermuxActivity.this, "cd ~");
|
||||
//TermuxCommandExecutor.openTermuxBash(MyTermuxActivity.this, "cd ~/TermuxWorkSpaces", "./TermuxWorkSpaces");
|
||||
}
|
||||
});
|
||||
LogUtils.d(TAG, "Termux 按钮初始化完成");
|
||||
}
|
||||
}
|
||||
|
||||
private void executeSampleCommand() {
|
||||
if (isTermuxAvailable()) {
|
||||
boolean success = TermuxCommandExecutor.openTermuxBash(this, "ls -la ./");
|
||||
if (success) {
|
||||
LogUtils.i(TAG, "命令执行成功");
|
||||
} else {
|
||||
LogUtils.e(TAG, "命令执行失败");
|
||||
}
|
||||
mTermuxWorkSpacesButton = findViewById(R.id.btn_termuxworkspaces);
|
||||
if (mTermuxWorkSpacesButton != null) {
|
||||
mTermuxWorkSpacesButton.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
LogUtils.d(TAG, "点击 TermuxWorkSpaces 按钮");
|
||||
TermuxCommandExecutor.openTermuxBash(MyTermuxActivity.this, "cd ~/TermuxWorkSpaces", "./TermuxWorkSpaces");
|
||||
}
|
||||
});
|
||||
LogUtils.d(TAG, "TermuxWorkSpaces 按钮初始化完成");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -115,7 +115,7 @@ public class TermuxCommandExecutor {
|
||||
context,
|
||||
"/data/data/com.termux/files/usr/bin/bash", // Termux 默认 bash 路径(正确)
|
||||
args,
|
||||
"/data/data/com.termux/files/home", // 默认工作目录
|
||||
TERMUX_HOME_PATH, // 默认工作目录
|
||||
false, // 终端会话执行(可见)
|
||||
null // 不输出到文件
|
||||
);
|
||||
@@ -176,6 +176,10 @@ public class TermuxCommandExecutor {
|
||||
}
|
||||
|
||||
public static boolean openTermuxBash(Context context, String command) {
|
||||
return openTermuxBash(context, command, "~");
|
||||
}
|
||||
|
||||
public static boolean openTermuxBash(Context context, String command, String workDir) {
|
||||
LogUtils.d(TAG, "openTermuxBash() 按钮点击,执行Gradle命令(实时输出)");
|
||||
|
||||
// 1. 校验Termux是否安装
|
||||
@@ -185,7 +189,10 @@ public class TermuxCommandExecutor {
|
||||
}
|
||||
|
||||
// 2. 定义核心路径(确保路径与Termux中一致)
|
||||
String projectPath = TERMUX_HOME_PATH + "/"; // 项目目录
|
||||
String projectPath = TERMUX_HOME_PATH;
|
||||
if (workDir.startsWith("~") || workDir.startsWith(".")) {
|
||||
projectPath = TERMUX_HOME_PATH + "/" + workDir.substring(1);
|
||||
}
|
||||
|
||||
// 3. 构造命令(核心:用stdbuf禁用缓冲,实现实时输出)
|
||||
String targetCmd = "";
|
||||
|
||||
Reference in New Issue
Block a user