Android 执行 adb shell 命令

Android 执行Adb shell 命令大多需要root权限,Android自带的Runtime. getRuntime().exec()容易出错,在网上找到了一个执行adb shell命令的类

代码如下:

/**检查手机是否存在root权限,发送一些消息*/
package com.dx.superbar.util;

import java.io.DataOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import android.content.Context;

public class RootContext
{
private static RootContext instance = null;
private static Object mLock = new Object();
String mShell;
OutputStream o;
Process p;

private RootContext(String cmd) throws Exception
{
this.mShell = cmd;
init();
}

public static RootContext getInstance()
{
if (instance != null)
{
return instance;
}
synchronized (mLock)
{
try
{
instance = new RootContext("su");
}
catch (Exception e)
{
while (true)
try
{
instance = new RootContext("/system/xbin/su");
}
catch (Exception e2)
{
try
{
instance = new RootContext("/system/bin/su");
}
catch (Exception e3)
{
e3.printStackTrace();
}
}
}
return instance;
}
}

private void init() throws Exception
{
if ((this.p != null) && (this.o != null))
{
this.o.flush();
this.o.close();
this.p.destroy();
}
this.p = Runtime.getRuntime().exec(this.mShell);
this.o = this.p.getOutputStream();
system("LD_LIBRARY_PATH=/vendor/lib:/system/lib ");
}

private void system(String cmd)
{
try
{
this.o.write((cmd + "\n").getBytes("ASCII"));
return;
}
catch (Exception e)
{
while (true)
try
{
init();
}
catch (Exception e1)
{
e1.printStackTrace();
}
}
}

public void runCommand(String cmd)
{
system(cmd);
}

/**
* 判断是否已经root了
* */
public static boolean hasRootAccess(Context ctx)
{
final StringBuilder res = new StringBuilder();
try
{
if (runCommandAsRoot(ctx, "exit 0", res) == 0)
return true;
}
catch (Exception e)
{
}
return false;
}

/**
* 以root的权限运行命令
* */
public static int runCommandAsRoot(Context ctx, String script,
StringBuilder res)
{
final File file = new File(ctx.getCacheDir(), "secopt.sh");
final ScriptRunner runner = new ScriptRunner(file, script, res);
runner.start();
try
{
runner.join(40000);
if (runner.isAlive())
{
runner.interrupt();
runner.join(150);
runner.destroy();
runner.join(50);
}
}
catch (InterruptedException ex)
{
}
return runner.exitcode;
}

private static final class ScriptRunner extends Thread
{
private final File file;
private final String script;
private final StringBuilder res;
public int exitcode = -1;
private Process exec;

public ScriptRunner(File file, String script, StringBuilder res)
{
this.file = file;
this.script = script;
this.res = res;
}

@Override
public void run()
{
try
{
file.createNewFile();
final String abspath = file.getAbsolutePath();
Runtime.getRuntime().exec("chmod 777 " + abspath).waitFor();
final OutputStreamWriter out = new OutputStreamWriter(
new FileOutputStream(file));
if (new File("/system/bin/sh").exists())
{
out.write("#!/system/bin/sh\n");
}
out.write(script);
if (!script.endsWith("\n"))
out.write("\n");
out.write("exit\n");
out.flush();
out.close();

exec = Runtime.getRuntime().exec("su");
DataOutputStream os = new DataOutputStream(
exec.getOutputStream());
os.writeBytes(abspath);
os.flush();
os.close();

InputStreamReader r = new InputStreamReader(
exec.getInputStream());
final char buf[] = new char[1024];
int read = 0;
while ((read = r.read(buf)) != -1)
{
if (res != null)
res.append(buf, 0, read);
}

r = new InputStreamReader(exec.getErrorStream());
read = 0;
while ((read = r.read(buf)) != -1)
{
if (res != null)
res.append(buf, 0, read);
}

if (exec != null)
this.exitcode = exec.waitFor();
}
catch (InterruptedException ex)
{
if (res != null)
res.append("\nOperation timed-out");
}
catch (Exception ex)
{
if (res != null)
res.append("\n" + ex);
}
finally
{
destroy();
}
}

public synchronized void destroy()
{
if (exec != null)
exec.destroy();
exec = null;
}
}
}

时间: 2024-08-05 23:09:46

Android 执行 adb shell 命令的相关文章

【Android】-- adb shell 命令探索

ADB是什么,做android开发的没有不知道的. window下运行cmd,输入adb help就会打印adb都能够做的事情,包括 adb push ..adb pull .. adb devices  adb install... 等等..并且一搜一大把. 其中有一个命令叫做  adb shell,android是基于linux的,shell模式下linux的命令可是非常多的,android去掉了许多linux的命令,那么从这写命令里面又能够发现什么宝藏呢?寻宝之旅开启!! 首先linux的

Android Java代码执行adb Shell命令

通过java代码代替adb命令 增加工具类 ShellUtils.java import java.io.BufferedReader; import java.io.DataOutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.util.List; /** * ShellUtils */ public class ShellUtils { public static fin

Android 常用adb shell 命令(转)

调试Android程序有时需要adb shell 命令,adb全称Android Debug Bridge ,就是起到调试桥的作用. 通过adb我们可以在Eclipse中通过DDMS来调试Android程序,说白了就是debug工具.adb通过监听Socket TCP 5554等端口让IDE和Qemu通讯. 默认情况下当我们运行Eclipse时adb进程就会自动运行.adb是一个C/S模式的程序,由三个部分组成:a client,a server and a daemon. 其中client和s

Android 常用adb shell 命令

调试Android程序有时需要adb shell 命令,adb全称Android Debug Bridge ,就是起到调试桥的作用.通过adb我们可以在Eclipse中通过DDMS来调试Android程序,说白了就是debug工具.adb通过监听Socket TCP 5554等端口让IDE和Qemu通讯.默认情况下当我们运行Eclipse时adb进程就会自动运行.adb是一个C/S模式的程序,由三个部分组成:a client,a server and a daemon.其中client和serv

(转)Android 使用 adb shell 命令行启动应用

原文:http://blog.chinaunix.net/uid-26997997-id-3350449.html 在Android中,除了从界面上启动程序之外,还可以从命令行启动程序,使用的是命令行工具am. usage: am [subcommand] [options] start an Activity: am start [-D]        -D: enable debugging send a broadcast Intent: am broadcast start an Ins

adb shell 命令详解,android

http://www.miui.com/article-275-1.html http://noobjava.iteye.com/blog/1914348 adb shell 命令详解,android 博客分类: android 常用:1.进入模拟器的shell模式 :  adb shell 2.安装应用程序:  adb install -r 应用程序.apkadb installadb install <apk文件路径> :这个命令将指定的apk文件安装到设备上adb install  -r

【原】Android 设备,如何root,执行adb shell,查看设备中的数据库信息等

(一)第一步:root 使用百度一键root 等app,一键就可root,步骤略 (二) 1.执行 adb shell su 获得root权限 ls 查看当前目录 cd data/data ls 选com.tencent.cm为例 cd com.tencent.cm/database ls 看到里边有一些 .db数据库文件 这时候,执行 sqlite3 tes_db.db 如果你的设备中有sqlite3,这句应该会执行成功,接下来就可以 执行select 语句了, ---------------

【android】android 常用adb 和 adb shell 命令

db是SDK自带的工具,可实现桥接功能:adb shell 可以与手机系统建立交互,是基于andoid Linux系统下的操作 ADB常用命令: 1. 查看设备         adb  devices 这个命令是查看当前连接的设备, 连接到计算机的android设备或者模拟器将会列出显示 2. 安装软件 adb  install <apk文件路径> 这个命令将指定的apk文件安装到设备上       参数“-r”,它是更新安装的意思,       参数 -s ,安装到sdcard.     

Android手机Mac OSX下执行adb shell提示device not found解决方法

某些Android手机Mac OS X下执行adb shell提示device not found,参考网上解决方法,以三星S3为例详述方法如下. 终端命令行下输入: #system_profiler SPUSBDataType USB: USB Hi-Speed Bus: Host Controller Location: Built-in USB Host Controller Driver: AppleUSBEHCI PCI Device ID: 0x0d9d PCI Revision I