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;
}
}
}
Android 执行 adb shell 命令的更多相关文章
- Android 通过adb shell命令查看内存,CPU,启动时间,电量等信息
Android 通过adb shell命令查看内存,CPU,启动时间,电量等信息 by:授客 QQ:1033553122 1. 查看内存信息 1)查看所有内存信息 命令: dumpsys mem ...
- Android 常用adb shell 命令
原文地址http://blog.csdn.net/rain_butterfly/article/details/40894807 调试Android程序有时需要adb shell 命令,adb全称An ...
- Android 常用adb shell 命令(转)
调试Android程序有时需要adb shell 命令,adb全称Android Debug Bridge ,就是起到调试桥的作用. 通过adb我们可以在Eclipse中通过DDMS来调试Androi ...
- 【Android】-- adb shell 命令探索
ADB是什么,做android开发的没有不知道的. window下执行cmd,输入adb help就会打印adb都可以做的事情,包含 adb push ..adb pull .. adb device ...
- Android 通过adb shell命令查看内存,CPU,启动时间,电量等信息
1. 查看内存信息 1)查看所有内存信息 命令: dumpsys meminfo 例: C:\Users\laiyu>adb shell shell@android:/ $ dumpsys m ...
- Android:adb shell 命令详解
安卓系统是基于Linux系统开发,也就支持常见的Linux的命令,这些命令都保存在手机“/system/bin”的目录下,通过adb shell 便可以调用这些命令. 进入“/system/bin”该 ...
- Android自动化----adb shell,appium,uiautomator2
1.区别 1,adb shell脚本的方式 不但可以在有电脑的情况下使用,通过数据线连接电脑然后adb shell命令,而且还可以打包成app,在手机的终端使用adb shell命令. 2,appiu ...
- 【原创】Android开发之ADB及ADB SHELl命令的应用
adb的全称为Android Debug Bridge,就是起到调试桥的作用.通过adb我们可以在Eclipse中方面通过DDMS来调试Android程序,说白了就是debug工具.adb的工作方式比 ...
- Android成长记(1)-----android环境搭建与adb shell 命令
整理一下学习android一步一步存下来的自己总结或是从网上摘抄的比较不错的文档,电脑要上交了,最舍不得的就是自己积累的这么点东西了,所以决定发布到黎梓小站,以供大家一起学习以及自己日后忘记了也有地方 ...
随机推荐
- HDU 1216 Assistance Required 埃拉托色尼色筛法
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1216 思路:色筛法 代码(1): #include<iostream>//-------- ...
- 初窥c++11:lambda函数及其用法
转载于:点击打开链接 为什么需要lambda函数 匿名函数是许多编程语言都支持的概念,有函数体,没有函数名.1958年,lisp首先采用匿名函数,匿名函数最常用的是作为回调函数的值.正因为有这样的需求 ...
- Python3实现简单可学习的手写体识别
0.目录 1.前言 2.通过pymssql与数据库的交互 3.通过pyqt与界面的交互 4.UI与数据库的交互 5.最后的main主函数 1.前言 版本:Python3.6.1 + PyQt5 + S ...
- 深入理解Mysql数据库主从延迟
1什么会增加主从延迟? 1 网络不好 2 从库硬件差 3 索引没做好,从库执行慢 4 从库锁等待,多见于myisam 5 主库写频繁,从库单线程执行慢 6 使用row复制,或mix使用行复制 2如何优 ...
- JS 中对变量类型的五种判断方法
5种基本数据类型:undefined.null.boolean.unmber.string 复杂数据类型:object. object:array.function.date等 方法一:使用typeo ...
- AspNet Core :创建自定义 EF Core 链接数据库
这两天比较忙,写的会慢一点. 我们以控制台演示 EF Core的链接数据库 首先创建控制台程序 创建数据上下文类 EntityTable /// <summary> /// 继承 DbCo ...
- hdu 5305 friends
每一次比赛的时候脑子都卡顿, 这次更离谱,我居然二进制枚举边,这么大的复杂度.而且剪不了枝 后来学长说着是道爆搜.搜每一条边.恍然大悟. 仅仅须要剪掉点的度数是奇数的时候,或者他的线上朋友或线下朋友大 ...
- quick-cocos2d-x教程1:在window上创建第一个项目文件夹,并制作helloworld
说明:此教程是针对cocos2dx 2.0系列的,3.0的版本号,如今还没有公布出来. 1)首先从github.com把这个项目下载到本地.然后装到d盘的根文件夹,并设置文件夹路径为d:\quick- ...
- “command line option syntax error,Type command/?for help
VS2010安装WDT时出现"command line option syntax error.Type command/?for help错误 解决:可能是由于你的安装源文件所在的路径中有 ...
- Codeforces 29D Ant on the Tree 树的遍历 dfs序
题目链接:点击打开链接 题意: 给定n个节点的树 1为根 则此时叶子节点已经确定 最后一行给出叶子节点的顺序 目标: 遍历树并输出路径.要求遍历叶子节点时依照给定叶子节点的先后顺序訪问. 思路: 给每 ...