如何在android程序中执行adb shell命令

2024-01-11 15:11:08
推荐回答(5个)
回答1:

步骤1:安装USB驱动下载并安装HTC完整驱动程序(http://shuajidown3.lexun.cn/articile/2013/7/3/HTCDriver3.0.0.021.exe)手机进入设置-应用程序-开发-USB调试,将第一个选项打钩选中。然后通过USB线连接电脑,提示安装驱动。步骤2:软件准备1、把ADB工具http://shuajidown3.lexun.cn/articile/2013/7/3/adb工具.rar

解压放到你的电脑系统盘的根目录下

2、运行中,输入cmd进入命令提示符。以下命令均在命令提示符下进行。开始(点开始 在输入框里输入CMD)

3、 输入cd c:\adb回车,进入ADB所在目录

 现在就可以进行命令的操作了。    

【常用adb shell命令】 

   1. 显示系统中全部Android平台:    android list targets 

   2. 显示系统中全部AVD(模拟器):    android list avd   

   3. 创建AVD(模拟器):    android create avd --name 名称 --target 平台编号

   4. 启动模拟器:    emulator -avd 名称 -sdcard ~/名称.img (-skin 1280x800) 

   5. 删除AVD(模拟器):    android delete avd --name 名称 

   6. 创建SDCard:    mksdcard 1024M ~/名称.img 

   7. AVD(模拟器)所在位置:    Linux(~/.android/avd) Windows(C:\Documents and Settings\Administrator\.android\avd) 

   8. 启动DDMS:    ddms 

   9. 显示当前运行的全部模拟器:    adb devices 

   10. 对某一模拟器执行命令:    abd -s 模拟器编号 命令 

   11. 安装应用程序:    adb install -r 应用程序.apk 

   12. 获取模拟器中的文件:    adb pull  

   13. 向模拟器中写文件:    adb push

   14. 进入模拟器的shell模式:    adb shell 

   15. 启动SDK,文档,实例下载管理器:    android

   16. 卸载apk包:    adb shell    cd data/app    rm apk包    exit    adb uninstall apk包的主包名    adb install -r apk包 

   17. 查看adb命令帮助信息:    adb help 

   18. 在命令行中查看LOG信息:    adb logcat -s 标签名

   19. adb shell后面跟的命令主要来自:    源码\system\core\toolbox目录和源码\frameworks\base\cmds目录。

   20. 删除系统应用:    adb remount (重新挂载系统分区,使系统分区重新可写)。    adb shell    cd system/app    rm *.apk

   21. 获取管理员权限:    adb root 

   22. 启动Activity:    adb shell am start -n 包名/包名+类名(-n 类名,-a action,-d date,-m MIME-TYPE,-c category,-e 扩展数据,等)。

   23、发布端口:    你可以设置任意的端口号,做为主机向模拟器或设备的请求端口。    如:adb forward tcp:5555 tcp:8000 

   24、复制文件:    你可向一个设备或从一个设备中复制文件,    复制一个文件或目录到设备或模拟器上:    adb push     如:adb push test.txt /tmp/test.txt    从设备或模拟器上复制一个文件或目录:    adb pull     如:adb pull /addroid/lib/libwebcore.so 

   25、搜索模拟器/设备的实例:    取得当前运行的模拟器/设备的实例的列表及每个实例的状态:    adb devices 

   26、查看bug报告:    adb bugreport 

   27、记录无线通讯日志:    一般来说,无线通讯的日志非常多,在运行时没必要去记录,但我们还是可以通过命令,设置记录:    adb shell    logcat -b radio

   28、获取设备的ID和序列号:    adb get-product    adb get-serialno  

   29、访问数据库SQLite3    adb shell    sqlite3    #cd system/sd/data //进入系统内指定文件夹    #ls //列表显示当前文件夹内容    #rm -r xxx //删除名字为xxx的文件夹及其里面的所有文件    #rm xxx //删除文件xxx     #rmdir xxx //删除xxx的文件夹

回答2:

Android中执行adb shell命令的方式如下:

 /**
    * 执行一个shell命令,并返回字符串值
    *
    * @param cmd
    * 命令名称&参数组成的数组(例如:{"/system/bin/cat", "/proc/version"})
    * @param workdirectory
    * 命令执行路径(例如:"system/bin/")
    * @return 执行结果组成的字符串
    * @throws IOException
    */
    public static synchronized String run(String[] cmd, String workdirectory)
    throws IOException {
    StringBuffer result = new StringBuffer();
    try {
    // 创建操作系统进程(也可以由Runtime.exec()启动)
    // Runtime runtime = Runtime.getRuntime();
    // Process proc = runtime.exec(cmd);
    // InputStream inputstream = proc.getInputStream();
    ProcessBuilder builder = new ProcessBuilder(cmd);
    InputStream in = null;
    // 设置一个路径(绝对路径了就不一定需要)
    if (workdirectory != null) {
    // 设置工作目录(同上)
    builder.directory(new File(workdirectory));
    // 合并标准错误和标准输出
    builder.redirectErrorStream(true);
    // 启动一个新进程
    Process process = builder.start();
    // 读取进程标准输出流
    in = process.getInputStream();
    byte[] re = new byte[1024];
    while (in.read(re) != -1) {
    result = result.append(new String(re));
    }
    }
    // 关闭输入流
    if (in != null) {
    in.close();
    }
    } catch (Exception ex) {
    ex.printStackTrace();
    }
    return result.toString();
    }

  android系统底层采用的是linux,所以adb这样的linux指令是可以在java代码中调用的,可以使用ProcessBuilder 这个方法来执行对应的指令。还可以通过如下方式执行:

Process p = Runtime.getRuntime().exec("ls");
                String data = null;
                BufferedReader ie = new BufferedReader(new InputStreamReader(p.getErrorStream()));
                BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
                String error = null;
                while ((error = ie.readLine()) != null
                       && !error.equals("null")) {
                    data += error + "\n";
                }
                String line = null;
                while ((line = in.readLine()) != null
                       && !line.equals("null")) {
                    data += line + "\n";
                }
 
                Log.v("ls", data);

回答3:

ADB接口的作用主要是让电脑等其它设备控制安卓系统的,所以,称为“中间桥”;
不是为安卓自已用的,自已可直接执行称为SHELL,这与ADB无关。
所以安卓JAVA不一定有封装的ADB类。电脑上有ADB服务程序,端口5037,
它是中间程序,与安卓系统上守护进程(Daemon)通讯。
如果要在自已的手机上应该也能执行adb命令,应该直接跟守护进程
(Daemon)通讯了。百度上可以搜到的方法并不满意。

楼主用exec执行CMD命令,这已不是ADB接口了,这是系统的SHELL了!!!

自已用socket/tcp直接发命令效果不知怎样,地址用127.0.0.1, 安卓daemon进程的端口
5555 是奇数开始。
。。。 。至于ADB对话协议百度可以搜到,建议试一试。

楼上其实要的是SHELL,并不是ADB,我搜到一篇文章,但我并没有试过,
是否需要ROOT,不得而知,附上,你试一试 ,回个话。
满意就采纳!

回答4:

你可以写个apk,apk可以执行部分adb 命令。
代码的实现:例如界面的一个按钮,只需点击该按钮就能执行adb命令,如何在click()里写这些adb命并且在电脑上执行呢?
adb shell
insmod /system/lib/modules/wlan.ko
现在需要在该应用里也达到相同的效果,
但是执行
proc = Runtime.getRuntime().exec(“insmod /system/lib/modules/wlan.ko”);
num = proc.waitFor();
num的返回值是255,意思是没有执行成功。具体你可以参照一些书籍。

回答5:

package net.gimite.nativeexe;

import java.io.BufferedReader;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import net.gimite.nativeexe.R;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.*;

public class MainActivity extends Activity {

private TextView outputView;
private Button localRunButton;
private EditText localPathEdit;
private Handler handler = new Handler();
private EditText urlEdit;
private Button remoteRunButton;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

outputView = (TextView)findViewById(R.id.outputView);
localPathEdit = (EditText)findViewById(R.id.localPathEdit);
localRunButton = (Button)findViewById(R.id.localRunButton);
localRunButton.setOnClickListener(onLocalRunButtonClick);

}

private OnClickListener onLocalRunButtonClick = new OnClickListener() {
public void onClick(View v) {
String output = exec(localPathEdit.getText().toString());
output(output);
// try {
//
// // Process process = Runtime.getRuntime().exec(localPathEdit.getText().toString());
//
// } catch (IOException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
}
};

// Executes UNIX command.
private String exec(String command) {
try {
Process process = Runtime.getRuntime().exec(command);
BufferedReader reader = new BufferedReader(
new InputStreamReader(process.getInputStream()));
int read;
char[] buffer = new char[4096];
StringBuffer output = new StringBuffer();
while ((read = reader.read(buffer)) > 0) {
output.append(buffer, 0, read);
}
reader.close();
process.waitFor();
return output.toString();
} catch (IOException e) {
throw new RuntimeException(e);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}

private void download(String urlStr, String localPath) {
try {
URL url = new URL(urlStr);
HttpURLConnection urlconn = (HttpURLConnection)url.openConnection();
urlconn.setRequestMethod("GET");
urlconn.setInstanceFollowRedirects(true);
urlconn.connect();
InputStream in = urlconn.getInputStream();
FileOutputStream out = new FileOutputStream(localPath);
int read;
byte[] buffer = new byte[4096];
while ((read = in.read(buffer)) > 0) {
out.write(buffer, 0, read);
}
out.close();
in.close();
urlconn.disconnect();
} catch (MalformedURLException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
}
}

private void output(final String str) {
Runnable proc = new Runnable() {
public void run() {
outputView.setText(str);
}
};
handler.post(proc);
}

}