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

简介:

reference to :  http://***/Article/11768

有时候我们写的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 Pro
本文转自demoblog博客园博客,原文链接http://www.cnblogs.com/0616--ataozhijia/p/4993196.html如需转载请自行联系原作者

demoblog
相关文章
|
3月前
|
Android开发 Python
Python封装ADB获取Android设备wifi地址的方法
Python封装ADB获取Android设备wifi地址的方法
61 0
|
8月前
|
Android开发
Android RecyclerView的notify方法和动画的刷新详解(二)
Android RecyclerView的notify方法和动画的刷新详解
141 0
|
21天前
|
Android开发
Android调用相机与相册的方法2
Android调用相机与相册的方法
17 0
|
3月前
|
安全 Java 数据安全/隐私保护
Android和iOS应用程序加固方法详解:混淆、加壳、数据加密、动态加载和数字签名实现
Android和iOS应用程序加固方法详解:混淆、加壳、数据加密、动态加载和数字签名实现
70 0
|
8月前
|
Android开发
Android LayoutInflater.from(context).inflate()方法的作用
Android LayoutInflater.from(context).inflate()方法的作用
48 0
|
8月前
|
XML Java Android开发
Android 中ScrollView垂直滚动视图之隐藏滚动条的三种方法
Android 中ScrollView垂直滚动视图之隐藏滚动条的三种方法
91 0
|
8月前
|
Android开发
Android 中使用spinner实现下拉列表的两种方法和getItemAtPosition()方法的使用
Android 中使用spinner实现下拉列表的两种方法和getItemAtPosition()方法的使用
62 0
|
4月前
|
XML JSON Android开发
[Android]使用JSONObiect和Gson相关方法实现json数据与kotlin对象的相互转换
[Android]使用JSONObiect和Gson相关方法实现json数据与kotlin对象的相互转换
54 0
|
4月前
|
XML Java Android开发
Android Studio App自定义控件中视图的构造和测量方法讲解及实战(附源码 实现下拉刷新功能 超详细必看)
Android Studio App自定义控件中视图的构造和测量方法讲解及实战(附源码 实现下拉刷新功能 超详细必看)
42 1
|
5月前
|
Android开发
[√]Android 通过adb内存监测方法
[√]Android 通过adb内存监测方法
126 1