android 应用程序中执行Linux 命令
ADB 无线调试命令
son = "setprop service.adb.tcp.port 5555\n" +
"stop adbd\n" +
"start adbd\n";
soff = "setprop service.adb.tcp.port -1\n" +
"stop adbd\n" +
"start adbd\n";
reboot 立即重启 //获取Ip
String getIp() {
String ip = "127.0.0.1";
WifiManager wm = (WifiManager) getApplicationContext().getSystemService(WIFI_SERVICE);
if (!wm.isWifiEnabled()) {
wm.setWifiEnabled(true);
}
WifiInfo wi = wm.getConnectionInfo();
int addr = wi.getIpAddress();
return intToIp(addr);
} String intToIp(int t) {
return (t & 0xFF) + "." + ((t >> 8) & 0xFF) + "." + ((t >> 16) & 0xFF) + "." + ((t >> 24) & 0xFF);
} //执行普通命令
String exec(String cmd) {
if (cmd.startsWith("su")|cmd.startsWith("ping")) {
return "不允许执行的命令";
}
String result = "";
Process ps = null; ProcessBuilder pb = new ProcessBuilder(cmd);
InputStream es = null;
InputStream is = null;
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int read = -1;
ps = pb.start();
es = ps.getErrorStream();
while ((read = es.read()) != -1) {
baos.write(read); }
baos.write('\n');
is = ps.getInputStream();
while ((read = is.read()) != -1) {
baos.write(read); }
byte[] data = baos.toByteArray();
result = new String(data);
} catch (IOException e) {
e.printStackTrace();
} finally {
try { if (es != null) {
es.close();
} if (is != null) {
is.close();
}
} catch (IOException e) {
e.printStackTrace();
}
if (ps != null) {
ps.destroy();
}
}
return result;
}
//执行root命令
String rootExec(String cmd) {
if (cmd.startsWith("su")|cmd.startsWith("ping")) {
return "不允许执行的命令";
}
String result = "";
DataOutputStream dos = null;
DataInputStream dis = null;
try {
Process ps = Runtime.getRuntime().exec("su");
dos = new DataOutputStream(ps.getOutputStream());
dis = new DataInputStream(ps.getInputStream());
dos.writeBytes(cmd + "\n");
dos.flush();
dos.writeBytes("exit\n");
dos.flush();
String line = null;
while ((line = dis.readLine())!=null) {
result += "\n"+line;
// Message ms = new Message();
// ms.obj = line;
// handler.sendMessageDelayed(ms,1000);
} ps.waitFor();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (dos != null) { try {
dos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (dis != null) { try {
dis.close();
} catch (IOException e) {
e.printStackTrace();
}
} }
return result;
}
android 应用程序中执行Linux 命令的更多相关文章
- 在Python程序中执行linux命令
import commands print commands.getstatusoutput('ls') 输出: (0, '1.py\nwork.nfs') 参考文档: https://blog.cs ...
- 使用Android平板编程,执行linux命令
android有一些应用支持开发, AIDE 介绍http://www.wandoujia.com/apps/com.aide.ui https://play.google.com/store/app ...
- 在程序中执行shell命令
在linux系统下的操作中我们会经常用到shell命令来进行,一开始学习进程的时候对于shell命令也进行了思考,认为shell命令就是一个进程的外壳,经过了后来的学习对于这一点也有了更多的认识. 用 ...
- 如何在java程序中调用linux命令或者shell脚本
转自:http://blog.sina.com.cn/s/blog_6433391301019bpn.html 在java程序中如何调用linux的命令?如何调用shell脚本呢? 这里不得不提到ja ...
- 写一段java程序来执行linux命令
摘要 在日常开发中,程序员需要经常查询服务器日志来排查问题和调试程序.如果是本地调试还好,但项目一旦发布到服务器上,每次查日志就很麻烦,而且日志量巨大,有时我们无法找到我们需要的信息.经常需要借助第三 ...
- 在c++程序中执行DOS命令
转自博客:http://blog.csdn.net/ypist/article/details/8485049 #1,system()方式 在C盘根目录下新建文件夹,名称为12: system(&qu ...
- Java代码中执行Linux命令,亲测可用
前提需要知道怎么在linux怎么新建java文件和怎么编译,否则请先学其他知识!! import java.io.*;public class Test{ public static void mai ...
- perl中执行linux命令,及其区别
1. system("date '+%Y-%m-%d %H:%M:%S'") 该命令返回的是-1.(应该是date命令的main函数的返回值) 2. `date '+%Y-%m-% ...
- java程序中调用Linux命令Windows命令
目前总结的方法: 调用Linux简单的命令行,设置文件夹权限755 String scriptDir = "/home/wenf"; String cmd = "chmo ...
随机推荐
- 详细讲解Linux下安装python3(Python3.5.4)
https://jingyan.baidu.com/article/6b97984dd6dbb01ca2b0bf0a.html
- LeetCode--455--分发饼干
问题描述: 假设你是一位很棒的家长,想要给你的孩子们一些小饼干.但是,每个孩子最多只能给一块饼干.对每个孩子 i ,都有一个胃口值 gi ,这是能让孩子们满足胃口的饼干的最小尺寸:并且每块饼干 j , ...
- ncnn框架
1.下载和编译ncnn git clone https://github.com/Tencent/ncnn cd ncnn mkdir build && cd build cmake ...
- Confluence 6 空间的权限是附加的
空间的权限是附加的.如果一个用户以个人的方式或者以一个用户组成员的方式赋予了权限,Confluence 将会把这些权限合并在一起. 下面是这个概念的示例... Sasha 是 confluence- ...
- Xor-MST CodeForces - 888G (最小生成树,分治)
大意: n结点无向完全图, 给定每个点的点权, 边权为两端点异或值, 求最小生成树
- navicat 连接 mysql 解决出现client does not support authentication protocol requested by server的问题
MySQL8换了加密插件,数据库管理客户端都来不及更新,连接方式缺乏sha2的加密方式首先第一步, UPDATE mysql.user SET plugin = 'mysql_native_passw ...
- python-django rest framework框架
1.API 接口 是什么,干什么用的? API简单的来说就是一个url - http://www.oldboyedu.com/get_user/ - http://www.oldboyedu.com ...
- leetcode-algorithms-30 Substring with Concatenation of All Words
leetcode-algorithms-30 Substring with Concatenation of All Words You are given a string, s, and a li ...
- JQ 实现监测input中值的变化并绑定到另个input
$('#input').bind('input propertychange', function () { $('#myDiv ...
- poj-2115-exgcd
C Looooops Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 32062 Accepted: 9337 Descr ...