有时候我们写的app要用uid=0的方式启动一个process,framework层和app层是做不到的,只有通过写脚本,利用am来实现。下面是具体步骤:

1.创建一个包含Main()方法Java project

1.1.创建一个Java project

1.2.添加Main()方法

1.3.导出为jar包

1.4.将java 版本的jar变成android 版本的jar

首先,找到dx工具所在文件夹,如android-sdk/build-tools/20.0.0,并将该文件夹加入到环境变量PATH中;

其次,执行编译命令dx --dex --output=classes.dex  BKTools.jar

最后,将dex文件打包成android版本的jar,aapt add BKTools.jar classes.dex

1.5.将该jar包push到手机的/system/framework目录下

1.6.修改jar包的权限为777

2.编写一个linux shell脚本

2.1.新建一个文本,命名为run_bktools.sh

输入以下代码:

# Script to start "am" on the device, which has a very rudimentary
# shell.
#
base=/system
export CLASSPATH=$base/framework/BKTools.jar
exec app_process $base/bin com.larack.bktools.BKMain "$@"
CLASSPATH为jar包的路径,com.larack.bktools.BKMain为jar包的main函数所在的类,"$@"表示把当前参数传入到main中。

2.2.将该sh文件run_bktools.sh push到手机/system/bin目录下,并且修改权限为777

2.3.测试利用sh脚本启动jar包

OK,启动成功啦。

3.在android app中启动shell脚本

3.1.创建一个android project

3.2.用root起shell脚本

package com.larackbkapp;

import java.io.DataOutputStream;
import java.io.IOException; import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast; public class MainActivity extends Activity { private static final String TAG = "AAA"; private static final String CMD = "run_bktools.sh"; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); int result = -1;
result = execRootCmdSilent(CMD);
if (-1 == result)
Toast.makeText(this, "start fail.", Toast.LENGTH_LONG).show();
else
Toast.makeText(this, "start success.", Toast.LENGTH_LONG).show();
} public int execRootCmdSilent(String cmd) {
int result = -1;
DataOutputStream dos = null;
try {
Process p = Runtime.getRuntime().exec("su");
dos = new DataOutputStream(p.getOutputStream());
Log.i(TAG, cmd);
dos.writeBytes(cmd + "\n");
dos.flush();
dos.writeBytes("exit\n");
dos.flush();
p.waitFor();
result = p.exitValue();
Log.i(TAG, "Success execRootCmdSilent(" + cmd + ")=" + result);
} catch (Exception e) {
e.printStackTrace();
Log.e(TAG,
"execRootCmdSilent(" + cmd + "),Exception:"
+ e.getMessage());
} finally {
if (dos != null) {
try {
dos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return result;
} @Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
} @Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}

3.3.检验shell是否成功叫起

将手机连上电脑,编译执行bkapp,用adb logcat检查是否打有“Success execRootCmdSilent.."字样,观察手机上是否显示“start success.”

检验OK,我就不再截图了。

此时后台也在执行我们在Jar包中写的代码了,如果,我们在Jar包中写一个自己的 ActivityManagerSerive,PowerManagerSerive或者,其他,都将是以root运行的。

												

Android以root起一个process[shell脚本的方法]的更多相关文章

  1. [Android Pro] Android以root起一个process[shell脚本的方法]

    reference to :  http://***/Article/11768 有时候我们写的app要用uid=0的方式启动一个process,framework层和app层是做不到的,只有通过写脚 ...

  2. 查看当前支持的shell,echo -e相关转义符,一个简单shell脚本,dos2unix命令把windows格式转为Linux格式

    /etc/shells [root@localhost ~]# more /etc/shells /bin/sh /bin/bash /sbin/nologin /usr/bin/sh /usr/bi ...

  3. Python 调用 Shell脚本的方法

    Python 调用 Shell脚本的方法 1.os模块的popen方法 通过 os.popen() 返回的是 file read 的对象,对其进行读取 read() 的操作可以看到执行的输出. > ...

  4. 2.8 补充:shell脚本执行方法

    bash shell 脚本的方法有多种,现在作个小结.假设我们编写好的shell脚本的文件名为hello.sh,文件位置在/data/shell目录中并已有执行权限.   方法一:切换到shell脚本 ...

  5. Linux crontab 每5秒钟执行一次 shell 脚本 的方法

    Linux crontab 每5秒钟执行一次 shell 脚本 的方法 由于 Linux 的 crontab 的定时命令格式如下: minute hour day-of-month month-of- ...

  6. [shell]上一个命令执行完成,才执行下一个操作 | shell脚本中判断上一个命令是否执行成功

    shell脚本中判断上一个命令是否执行成功  shell中使用符号“$?”来显示上一条命令执行的返回值,如果为0则代表执行成功,其他表示失败.结合if-else语句实现判断上一个命令是否执行成功. 场 ...

  7. shell脚本中切换用户执行相应的命令或者shell脚本的方法

    通常在执行自动化过程中可能需要将root用户切换到其他用户进行执行,如:oralce 但是,执行的命令又要回到root用户下,继续执行root用户下的其他命令. 此时需要了解 su 命令中的参数 -c ...

  8. Nodejs中调用系统命令、Shell脚本的方法和实例

    每种语言都有自己的优势,互相结合起来各取所长程序执行起来效率更高或者说哪种实现方式较简单就用哪个,nodejs是利用子进程来调用系统命令或者文件,文档见http://nodejs.org/api/ch ...

  9. shell脚本调试方法

    我们开启了 Shell 脚本调试系列文章,先是解释了不同的调试选项,下面介绍如何启用 Shell 调试模式. 写完脚本后,建议在运行脚本之前先检查脚本中的语法,而不是查看它们的输出以确认它们是否正常工 ...

随机推荐

  1. python编码总结

    关于ASCII码和Unicode码的来源 计算机只能处理数字,如果要处理文本,需要先将文本转换成数字.早期计算机采用8bit作为一个字节(byte).所以一个字节最大为255(二进制11111111= ...

  2. Deep learning Reading List

    本文来自:http://jmozah.github.io/links/ Following is a growing list of some of the materials i found on ...

  3. Swap 2 Variables in Python

    In Python, it's concise, easy and faster to swap 2 variables compared in other Programming languages ...

  4. 设置SVN提交日志必填

    1.打开visualSVN Server2.打开Repositories3.右键需要控制的项目->所有任务->manage hooks...4.选择Hooks tab,点击Pre-comm ...

  5. ServiceModel 元数据实用工具 (Svcutil.exe)

    ServiceModel 元数据实用工具用于依据元数据文档生成服务模型代码,以及依据服务模型代码生成元数据文档 一.SvcUtil.exe ServiceModel 元数据实用工具可在 Windows ...

  6. javascript DOM dindow.docunment对象

    一.找到元素: docunment.getElementById("id"):根据id找,最多找一个:    var a =docunment.getElementById(&qu ...

  7. 格式化字符串--format用法

    print("hello {leon}".format(leon="world!")) #format 是一个格式化字符穿的方法. print("he ...

  8. $命令行参数解析模块argparse的用法

    argparse是python内置的命令行参数解析模块,可以用来为程序配置功能丰富的命令行参数,方便使用,本文总结一下其基本用法. 测试脚本 把以下脚本存在argtest.py文件中: # codin ...

  9. CSS 3 中的多列属性

    .column-count <!DOCTYPE html> <html> <head> <meta charset="utf-8"> ...

  10. 20145201《Java程序设计》第九周学习总结

    20145201 <Java程序设计>第九周学习总结 教材学习内容总结 JDBC是用于执行SQL的解决方案,开发人员使用JDBC的标准接口,数据库厂商则对接口进行操作,开发人员无须接触底层 ...