有办法在Android上运行shell命令。您可以使用以下方法之一:
try {
String[] commands = {"ls", "-l"};
Process process = Runtime.getRuntime().exec(commands);
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
}
try {
String[] commands = {"ls", "-l"};
ProcessBuilder processBuilder = new ProcessBuilder(commands);
Process process = processBuilder.start();
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
}
Termux是一个Android终端模拟器和Linux环境应用程序,可以在Android设备上运行shell命令。您可以从Google Play商店下载Termux,然后使用以下命令安装Termux:
pkg install<package_name>
然后,您可以使用以下命令运行shell命令:
请注意,这些方法可能需要在AndroidManifest.xml文件中请求权限,例如:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
推荐的腾讯云相关产品和产品介绍链接地址:
领取专属 10元无门槛券
手把手带您无忧上云