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 ...
随机推荐
- java ----> java.lang.NoClassDefFoundError
环境: centos 6.10,vim,jdk1.8_u72,zookeeper-3.4.6,maven3+ 正文: 使用zk的api操作创建节点发生java.lang.NoClassDefFound ...
- 最简单的网络图片的爬取 --Pyhon网络爬虫与信息获取
1.本次要爬取的图片的url http://www.nxl123.cn/static/imgs/php.jpg 2.代码部分 import requestsimport osurl = "h ...
- VPC配置介绍
VPC(Virtual Port-Channel)是Cisco Nexus系列交换机中的一个特性.它支持一个跨机箱的二层Port-Channel.对于第三方设备来说(交换机或服务器)物理上是连接到了两 ...
- 用C#实现多种方式播放Wav声音
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- 【洛谷p1060】开心的金明
(DP背包第一题,值得记录思路呀) 开心的金明[传送门] 洛谷算法标签: 01背包问题的思路分析见[总结]01背包问题 这道题显然是典型的01背包问题,首先我们显然可以由输入的第i个物体的价格v[i] ...
- 『MXNet』第九弹_分类器以及迁移学习DEMO
解压文件命令: with zipfile.ZipFile('../data/kaggle_cifar10/' + fin, 'r') as zin: zin.extractall('../data/k ...
- Python之深浅copy与字符编码
一.深浅copy 1. 首先看赋值运算 l1 = [1,2,3,['barry','alex']] l2 = l1 l1[0] = 111 print(l1) # [111, 2, 3, ['barr ...
- shiro中记住我功能
Shiro提供了记住我(RememberMe)的功能,比如访问如淘宝等一些网站时,关闭了浏览器下次再打开时还是能记住你是谁,下次访问时无需再登录即可访问,基本流程如下: 1.首先在登录页面选中Reme ...
- 【Oracle】【1】查询N分钟之前的数据
--查询距离现在N分钟前的数据 1440:表示一天有1440分钟 SYSDATE - 10 :表示10天前 参考博客: 1,oracle 查询十分钟之前的数据 - 胡金水的博客 - CSDN博客 ht ...
- js 异步加载
document 加载 document.write("<scr" + "ipt src=\"js/jquery.js\"></sc ...