Android应用程序如何调用shell脚本(一)
转自:
Android应用程序如何调用shell脚本(一)
一般来说, Android 下的应用程序可以“直接”得到的最大的权限为 system ,但是如果我们需要在程序中执行某些需要 root 权限的命令,就需要 root 权限了。按照 Simon 的文章中提到的,应用程序有以下两种办法临时获得 root 权限:
1) 在 init.rc 实现一个 Service ,来帮助 Android 应用程序执行 root 权限的命令。
2) 实现一个虚拟设备,这个设备帮助 Android 应用程序执行 root 权限的命令。
第二种办法我这里没有尝试过。只介绍第一种方法。
1. 编写shell脚本或者可执行程序
下面是我的脚本cp_file.sh:
#! /system/bin/sh
cat /mnt/sdcard/launcher.db > /data/data/com.android.launcher/databases/launcher.db
chown system.system /data/ data/com.android.launcher/databases/launcher.db
chmod 600 /data/ data/com.android.launcher/databases/launcher.db
注意: 脚本的第一行必须为 # ! /system/bin/sh ,否则无法执行。
2. 在init.rc中注册service
service file_cp /system/bin/cp_file.sh
user root
oneshot
disabled
其中, oneshot 表示程序退出后不再重新启动, disabled 表示不在系统启动时启动。
Service中参数的含义参见其它文章。
3. 将应用程序的权限提升至system
1.在应用程序的AndroidManifest.xml中的manifest节点中加入android:sharedUserId="android.uid.system"这个属性。
2.使用mm命令来编译该应用程序,生成的apk就具有system权限了。
4. 在应用程序中添加属性设置代码:
A. 在应用程序中使用getruntime().exec()函数来执行shell脚本文件。参考代码如下:
package cycle.settings.system;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import android.util.Log;
public class RuntimeExec {
private static final String TAG = "RuntimeExec";
private static final boolean DEBUG = true;//TODO: close this flag
private Process proc;
private StreamGobbler outputGobbler = null;
private StreamGobbler errorGobbler = null;
static class StreamGobbler extends Thread{
InputStream is;
String type; //输出流的类型ERROR或OUTPUT
public StreamGobbler(InputStream is, String type) {
// TODO Auto-generated constructor stub
this.is = is;
this.type = type;
}
public void run(){
try {
if(DEBUG)Log.d(TAG, "StreamGobbler start");
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line = null;
while((line = br.readLine()) != null){
System.out.println(type+">"+line);
System.out.flush();
}
if(DEBUG)Log.d(TAG, "StreamGobbler end");
} catch (IOException e) {
// TODO Auto-generated catch block
if(DEBUG)Log.d(TAG, "StreamGobbler exception");
e.printStackTrace();
}
}
}
/**
*
* @param cmd : the command
* @return success or failure
*/
public boolean runtimeExec(String cmd){
if(DEBUG)Log.d(TAG, "runtimeExec start");
boolean mboolean = false;
try {
if(DEBUG)Log.d(TAG, "runtimeExec start1");
Runtime mRuntime = Runtime.getRuntime();
proc = mRuntime.exec(cmd);
//any output message
StreamGobbler outputGobbler = new StreamGobbler(proc.getInputStream(),"OUTPUT");
//any error message
StreamGobbler errorGobbler = new StreamGobbler(proc.getErrorStream(), "ERROR");
//kick them off
outputGobbler.start();
//kick them off
errorGobbler.start();
int exitVal = proc.waitFor();
if(DEBUG)Log.d(TAG, "process exitValue: "+exitVal);
mboolean = (proc.waitFor()== 0);
} catch (Throwable e) {
// TODO Auto-generated catch block
if(DEBUG)Log.d(TAG, "process exception");
e.printStackTrace();
}
return mboolean;
}
public void runtimeEND(){
if(DEBUG)Log.d(TAG, "runtimeEND start");
try {
if(proc != null){
if(DEBUG)Log.d(TAG, "runtimeEND start1");
proc.getOutputStream().close();
if(DEBUG)Log.d(TAG, "close getOutputStream finish");
proc.getErrorStream().close();
if(DEBUG)Log.d(TAG, "close getErrorStream finish");
proc.destroy();
if(DEBUG)Log.d(TAG, "proc has destory");
}
else{
if(DEBUG)Log.e(TAG, "proc is null!!!!!");
}
if(DEBUG)Log.e(TAG, "before System.exit(0);");
System.exit(0);
if(DEBUG)Log.e(TAG, "after System.exit(0);");
proc = null;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
未完,待续部分参考下篇博文。
参考博文:http://blog.csdn.net/silvervi/article/details/6315888
Android应用程序如何调用shell脚本(一)的更多相关文章
- C程序调用shell脚本共有三种方法
C程序调用shell脚本共有三种法子 :system().popen().exec系列函数call_exec1.c ,内容为:system() 不用你自己去产生进程,它已经封装了,直接加入自己的命令e ...
- linux c程序中获取shell脚本输出的实现方法
linux c程序中获取shell脚本输出的实现方法 1. 前言Unix界有一句名言:“一行shell脚本胜过万行C程序”,虽然这句话有些夸张,但不可否认的是,借助脚本确实能够极大的简化一些编程工作. ...
- linux C程序中获取shell脚本输出(如获取system命令输出)
转载自 http://blog.csdn.net/hjxhjh/article/details/7909518 1. 前言 Unix 界有一句名言:“一行shell脚本胜过万行C程序”,虽然这句话有些 ...
- Python 调用 Shell脚本的方法
Python 调用 Shell脚本的方法 1.os模块的popen方法 通过 os.popen() 返回的是 file read 的对象,对其进行读取 read() 的操作可以看到执行的输出. > ...
- Spring Boot 实现看门狗功能 (调用 Shell 脚本)
需要实现看门狗功能,定时检测另外一个程序是否在运行,使用 crontab 仅可以实现检测程序是否正在运行,无法做到扩展,如:手动重启.程序升级(如果只需要实现自动升级功能可以使用 inotify)等功 ...
- 【原】Gradle调用shell脚本和python脚本并传参
最近由于项目自动化构建的需要,研究了下gradle调用脚本并传参的用法,在此作个总结. Pre build.gradle中定义了$jenkinsJobName $jenkinsBuild两个Jenki ...
- 调用shell脚本,IP处理
//调用shell脚本,IP处理 package com.letv.sdns.web.utils; import org.slf4j.Logger; import org.slf4j.LoggerFa ...
- Unity开发Android应用程序:调用安卓应用程序功能
开发环境: Eclipse3.4 + adt12 + jdk6 + AndroidSDK2.2 Unity3.4 + windows7 测试设备: HTC Desire HD 本文要涉及到的几个重点问 ...
- Java 调用 shell 脚本详解
这一年的项目中,有大量的场景需要Java 进程调用 Linux的bash shell 脚本实现相关功能. 从之前的项目中拷贝的相关模块和网上的例子来看,有个别的“陷阱”造成调用shell 脚本在某些特 ...
随机推荐
- POJ 3220 位运算+搜索
转载自:http://blog.csdn.net/lyhypacm/article/details/5813634 DES:相邻的两盏灯状态可以互换,给出初始状态.询问是否能在三步之内到达.如果能的话 ...
- Unity 3D连接MySQl数据库
对数据库各种操作已经熟练,但是一遇到数据库问题还是头大,最近使用unity3d开发一款小型网络社区,遇到了各种问题分享一下以供大家参考: 以前使用的是SQL,第一次用MySQL,在网上随便下了一个,安 ...
- 根据ip,实现地址信息查询接口
偶然发现的360搜索的ip查询接口,记录下: 接口地址:https://m.so.com/position 使用方式1:传ip 如访问https://m.so.com/position?ip=47.1 ...
- 未能加载文件或程序集“Microsoft.Office.Interop.Excel
解决方法:未能加载文件或程序集“Microsoft.Office.Interop.Excel...” 2010-07-25 08:06:15 来源:源码之家 站长整理 [大 中 小] ...
- 漂亮的各种弹出框 sweet alert
Sweet Alert 是一个替代传统的 Alert 的提示效果.SweetAlert 自动居中对齐在页面中央,不管您使用的是台式电脑,手机或平板电脑看起来效果都很棒. 还带下拉 几种 动画效果 弹窗 ...
- android源码追踪学习 RecipientsEditor
RecipientsEditor 新建短信时输入收接者的editor, public class RecipientsEditor extends MultiAutoCompleteTextView ...
- FTP 主动模式 与被动模式
今天在被电信运营商给的没有内网ip被nat后的内网ip访问我的ftp服务器时出现了,连接被关闭的错误,经过多番查询发现问题原因是因为NAT内网ip没有被有效地转换为外网ip,也即是说NAT对ftp协议 ...
- postman--安装及Interceptor插件
1. 官网安装(看网速-我下载的时候一直下载失败) 打开官网,https://www.getpostman.com 选择ios或者win 2. 非官网安装 https://pan.baidu. ...
- [NLP] TextCNN模型原理和实现
1. 模型原理 1.1 论文 Yoon Kim在论文(2014 EMNLP) Convolutional Neural Networks for Sentence Classification提出Te ...
- 7.3 5种IO模型与IO复用
5种IO模型分别如下: 1.阻塞IO模型 当上层应用app1调用recv系统调用时,如果对等方没有发送数据(缓冲区没有数据),上层app1将阻塞(默认行为,被linux内核阻塞). 当对等方发送了数据 ...