如何在Android设备上运行Linux命令? [英] How can I run Linux commands on an Android device?
问题描述
在某些Android设备上,在ADB shell中,我只能运行 echo
, cd
, ls
。当我运行时:
tar -cvf //mnt/sdcard/BackUp1669/apk/test.tar / mnt / sdcard / test .apk
或命令 cp
,返回值:
sh:tar:未找到
为什么我不能运行这些命令?某些设备支持这些命令。我的最终目标是将文件从 / data / data 文件夹复制到SD卡。我得到了 su ,得到了以下代码:
int timeout = 1000;
字符串命令= tar -cvf / + Environment.getExternalStorageDirectory()+ / cp /
+ packageName + .tar + +路径;
DataOutputStream os = new DataOutputStream(process.getOutputStream());
BufferedReader是= new BufferedReader(new InputStreamReader(new DataInputStream(
process.getInputStream())),64);
字符串内联;
try {
StringBuilder sbCommand = new StringBuilder();
sbCommand.append(command).append();
sbCommand.append( \n);
os.writeBytes(command.toString());
if(is!= null){
for(int i = 0; i< timeout; i ++){
if(is.ready())
break;
try {
Thread.sleep(5);
} catch(InterruptedException e){
e.printStackTrace();
}
}
if(is.ready()){
inLine = is.readLine();
} else {
}
}
} catch(IOException e){
e.printStackTrace();
}
它总是在 is.ready()<中停止/ code>,当我将其更改为
process.waitfor()
时,它也停止了。为什么?
您可以在Android上运行Linux命令。但是通常只有很少的预装。
如果您想添加更多命令,可能需要 root 您的设备并在其上安装 busybox 。。 p>
这不是在应用程序中进行生产性使用,但可以帮助您使用设备。
On some Android devices, in the ADB shell, I can only run echo
, cd
, ls
. When I run:
tar -cvf //mnt/sdcard/BackUp1669/apk/test.tar /mnt/sdcard/test.apk
Or the command cp
, it returns:
sh: tar: not found
Why can I not run these commands? Some devices support these commands. My end goal is to copy a file from the /data/data folder to SD card. I got su and I got the following code:
int timeout = 1000;
String command = "tar -cvf /" + Environment.getExternalStorageDirectory() + "/cp/"
+ packageName + ".tar" + " " + path;
DataOutputStream os = new DataOutputStream(process.getOutputStream());
BufferedReader is = new BufferedReader(new InputStreamReader(new DataInputStream(
process.getInputStream())), 64);
String inLine;
try {
StringBuilder sbCommand = new StringBuilder();
sbCommand.append(command).append(" ");
sbCommand.append("\n");
os.writeBytes(command.toString());
if (is != null) {
for (int i = 0; i < timeout; i++) {
if (is.ready())
break;
try {
Thread.sleep(5);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
if (is.ready()) {
inLine = is.readLine();
} else {
}
}
} catch (IOException e) {
e.printStackTrace();
}
It always stops in is.ready()
, and when I changed it to process.waitfor()
it also stopped. Why?
You can run Linux commands on Android. But there are usually just very few pre-installed. If you want to add more commands you might want to root your device and install busybox on it.
This is not for productive use within an application but can help you to work with your device.
这篇关于如何在Android设备上运行Linux命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!