zoukankan      html  css  js  c++  java
  • Android以root起一个process[shell脚本的方法]

    有时候我们写的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 + "
    ");
                dos.flush();
                dos.writeBytes("exit
    ");
                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运行的。

    
    

     

  • 相关阅读:
    tomcat安装
    卸载重安firefox
    Metasploit笔记之信息收集命令
    postgresql-9.0.18-1-linux.run启动
    ubuntu 安装自启动管理
    MySQL数据库”mysql SQL Error:1146,SQLState:42S02 “解决方法
    PE笔记之节表
    标准类型String(学习中)
    链表实现(打印元素的实现)
    C++中new和delete来创建和释放动态数组
  • 原文地址:https://www.cnblogs.com/larack/p/4139766.html
Copyright © 2011-2022 走看看