本人博客原文

首先把你的自己的su的放到Android应用程序project的assets文件夹,为了和系统的su区分,我自己的su文件叫做sur。

另外我这里没有考虑x86架构的cpu的手机。
废话不多说,直接上代码吧!
Util.java文件

package cdut.robin.root.utils;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import ledroid.nac.NacShellCommand;
import android.content.Context;
import android.util.Log;
public class Util {
    private static String getDeployMySuShellScript(String localSuPath) {
        StringBuffer strBuffer = new StringBuffer();
        strBuffer.append("mount -o remount,rw " + MountPoint.getDeviceName("/system") + " /system");
        strBuffer.append("\n");
        strBuffer.append("mount -o remount,rw /system /system");
        strBuffer.append("\n");
        strBuffer.append("cat ").append(localSuPath).append(">" + kSysSuPath);
        strBuffer.append("\n");
        strBuffer.append("chown 0:0 " + kSysSuPath);
        strBuffer.append("\n");
        strBuffer.append("chmod 6777 " + kSysSuPath);
        strBuffer.append("\n");
        strBuffer.append("mount -o remount,ro " + MountPoint.getDeviceName("/system") + " /system");
        strBuffer.append("\n");
        strBuffer.append("mount -o remount,ro /system /system");
        strBuffer.append("\n");
        return strBuffer.toString();
    }
    final static String kSysSuPath = "/system/xbin/sur";
    private static boolean isMySuExists() {
        return new File(kSysSuPath).exists();
    }
    private static boolean writeMySu(Context context) {
        Process processShell = null;
        DataOutputStream osShell = null;
        String mySuTempPath = context.getFilesDir().getPath() + "/sur";
        File file = new File(mySuTempPath);
        if (file.exists()) {
            file.delete();
        }
        InputStream open = null;
        FileOutputStream out = null;
        try {
            open = context.getResources().getAssets().open("sur");
            out = context.openFileOutput("sur", Context.MODE_WORLD_WRITEABLE);
            byte buffer[] = new byte[4 * 1024];
            int len = 0;
            while ((len = open.read(buffer)) != -1) {
                out.write(buffer, 0, len);
            }
            out.flush();
        } catch (IOException e) {
            LogHelper.e("TAG", "errMessage" + e.getMessage());
        } finally {
            if (out != null) {
                try {
                    out.close();
                    if (open != null) {
                        open.close();
                    }
                } catch (Exception e) {
                    LogHelper.e("TAG", "errMessage" + e.getMessage());
                }
            }
        }
        Runtime runTime = Runtime.getRuntime();
        try {
            processShell = runTime.exec("su");
            osShell = new DataOutputStream(processShell.getOutputStream());
            String str = getDeployMySuShellScript(mySuTempPath);
            osShell.writeBytes(str);
            osShell.writeBytes("exit\n");
            osShell.flush();
            processShell.waitFor();
        } catch (IOException e) {
            
            e.printStackTrace();
        } catch (InterruptedException e) {
          
            e.printStackTrace();
        } finally {
            if (processShell != null) {
                try {
                    processShell.destroy();
                } catch (Exception e) {
                    // e.printStackTrace();
                }
                processShell = null;
            }
            if (osShell != null) {
                try {
                    osShell.close();
                    osShell = null;
                } catch (IOException e1) {
                    // e1.printStackTrace();
                }
            }
        }
        return new File(kSysSuPath).exists();
    }
    public static boolean doSthBySu(Context context) {
        if (!isMySuExists()) {
            boolean res = writeMySu(context);
            if (res) {
                Log.i("robin", "deploy My Su success!");
            }
            else
            {
                Log.i("robin", "deploy My Su fail!");
            }
        } else{
            Log.i("robin", "My su exsit!");
        }
        Process processShell = null;
        DataOutputStream osShell = null;
        //do something here by su
        try {
            Runtime runTime = Runtime.getRuntime();
            processShell = runTime.exec("sur");
            osShell = new DataOutputStream(processShell.getOutputStream());
            String str = getBussinessShellScript();
            osShell.writeBytes(str);
            osShell.writeBytes("exit\n");
            osShell.flush();
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        } finally {
            if (processShell != null) {
                try {
                    processShell.destroy();
                } catch (Exception e) {
                     e.printStackTrace();
                }
                processShell = null;
            }
            if (osShell != null) {
                try {
                    osShell.close();
                    osShell = null;
                } catch (IOException e1) {
                    // e1.printStackTrace();
                }
            }
        }
        return true;
    }
    public static String getBussinessShellScript() {
        return "echo hello";
    }
}
MountPoint.java文件
package cdut.robin.root.utils;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
public final class MountPoint {
    private static HashMap<String, String> MOUNT_POINT_CACH = new HashMap(10);
    private static HashMap<String, List<String>> DEVICE_CACH = new HashMap(10);
    public static boolean isMountPoint(String mountPoint) {
        return getDeviceName(mountPoint) != null;
    }
    public static String getDeviceName(String mountPoint) {
        if (mountPoint == null) {
            return null;
        }
        String deviceName = null;
        if (MOUNT_POINT_CACH.containsKey(mountPoint)) {
            deviceName = (String) MOUNT_POINT_CACH.get(mountPoint);
        }
        return deviceName;
    }
    public static boolean hasMultiMountPoint(String deviceName) {
        List list = getMountPoints(deviceName);
        return (list != null) && (list.size() > 1);
    }
    public static List<String> getMountPoints(String deviceName) {
        return (List) DEVICE_CACH.get(deviceName);
    }
    static {
        BufferedReader mountPointReader = null;
        try {
            mountPointReader = new BufferedReader(new InputStreamReader(new FileInputStream(new File("/proc/mounts"))));
            String buffer = null;
            while ((buffer = mountPointReader.readLine()) != null) {
                MOUNT_POINT_CACH.put(buffer.split(" ")[1], buffer.split(" ")[0]);
                List list = (List) DEVICE_CACH.get(buffer.split(" ")[0]);
                if (list == null) {
                    list = new ArrayList(1);
                }
                list.add(buffer.split(" ")[1]);
                DEVICE_CACH.put(buffer.split(" ")[0], list);
            }
        } catch (IOException e) {
        } finally {
            try {
                if (mountPointReader != null)
                    mountPointReader.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
}

结束!

Android中部署自己的su的更多相关文章

  1. Android中怎样自己制作su

    本文原博客:http://hubingforever.blog.163.com/blog/static/171040579201372915716149/ 在Android源代码的system\ext ...

  2. Android动态部署五:怎样从插件apk中启动Service

    转载请注明出处:http://blog.csdn.net/ximsfei/article/details/51072332 github地址:https://github.com/ximsfei/Dy ...

  3. Android中使用开源框架android-image-indicator实现图片轮播部署

    之前的博文中有介绍关于图片轮播的实现方式,分别为(含超链接): 1.<Android中使用ViewFlipper实现屏幕切换> 2.<Android中使用ViewPager实现屏幕页 ...

  4. Android中Fragment和ViewPager那点事儿(仿微信APP)

    在之前的博文<Android中使用ViewPager实现屏幕页面切换和引导页效果实现>和<Android中Fragment的两种创建方式>以及<Android中Fragm ...

  5. Android中Fragment与Activity之间的交互(两种实现方式)

    (未给Fragment的布局设置BackGound) 之前关于Android中Fragment的概念以及创建方式,我专门写了一篇博文<Android中Fragment的两种创建方式>,就如 ...

  6. Android中的五大布局

    Android中的五大布局 1.了解布局 一个丰富的界面总是要由很多个控件组成的,那我们如何才能让各个控件都有条不紊地 摆放在界面上,而不是乱糟糟的呢?这就需要借助布局来实现了.布局是一种可用于放置很 ...

  7. 在Android中调用C#写的WebService(附源代码)

    由于项目中要使用Android调用C#写的WebService,于是便有了这篇文章.在学习的过程中,发现在C#中直接调用WebService方便得多,直接添加一个引用,便可以直接使用将WebServi ...

  8. Android中FTP服务器、客户端搭建以及SwiFTP、ftp4j介绍

    本文主要内容: 1.FTP服务端部署---- 基于Android中SwiFTP开源软件介绍: 2.FTP客户端部署 --- 基于ftp4j开源jar包的客户端开发 : 3.使用步骤 --- 如何测试我 ...

  9. Android中如何像 360 一样优雅的杀死后台服务而不启动

    Android中,虽然有很多方法(API或者shell命令)杀死后台`service`,但是仍然有很多程序几秒内再次启动,导致无法真正的杀死.这里主要着重介绍如何像 360 一样杀死Android后台 ...

随机推荐

  1. 八月份 CUGBACM_Summer_Tranning 题解

    CUGBACM_Summer_Tranning4 比赛链接:http://vjudge.net/contest/view.action?cid=52230#overview 题解链接: F . HDU ...

  2. SQL:多表关联采取这一纪录迄今为止最大

    笔者:iamlasong 1.需求 两个表,投递记录表和封发开拆记录表,如今想知道投递日期距最后一次封发日期天数分布情况. 对这个需求,须要先查询出投递明细,同一时候要知道相应的邮件最后一次封发情况. ...

  3. unity3d 数学的数学基础和辅助类

    转载注明smartdot:http://my.oschina.net/u/243648/blog/67193 1.  数学(点乘/叉乘)/unity3d的数学辅助类 2.  坐标系统(本地/世界/屏幕 ...

  4. 图像特效——摩尔纹 moir

    %%% Moir %%% 摩尔纹 clc; clear all; close all; addpath('E:\PhotoShop Algortihm\Image Processing\PS Algo ...

  5. MIT 操作系统实验 MIT JOS lab2

    MIT JOS lab2 首先把内存分布理清楚,由/boot/main.c可知这里把kernel的img的ELF header读入到物理地址0x10000处 这里能够回想JOS lab1的一个小问.当 ...

  6. 谈论高并发(二十二)解决java.util.concurrent各种组件(四) 深入了解AQS(二)

    上一页介绍AQS其基本设计思路以及两个内部类Node和ConditionObject实现 聊聊高并发(二十一)解析java.util.concurrent各个组件(三) 深入理解AQS(一) 这篇说一 ...

  7. Spring的文件上传

    Spring在发现包括multipart的请求后,会使用MultipartResolver的实现bean处理文件上传操作,现有採用Servlet3的 org.springframework.web.m ...

  8. JAVA IP地址转成长整型方法

    JAVA IP地址转成长整型方法 代码例如以下: /** * IP转成整型 * @param ip * @return */ public static Long ip2int(String ip) ...

  9. Android搜索芽发展clientVersion1.0结束(过程和结果显示)

    本文原:http://blog.csdn.net/minimicall 转载标明. 博士生.找我,我希望有一个合作伙伴.为了帮助他解决了移动终端产品.他给了我他的想法的叙述性说明,搜索布.要搜索布图像 ...

  10. WINDOWS7,8和os x yosemite 10.10.1懒人版双系统安装教程

    安装过程 磁盘划分 懒人版如果不是整盘单系统或者双硬盘双系统安装我们需要在当前系统磁盘划分两块磁盘空间,一个用来做安装盘,一个作为系统盘. 我这里是单硬盘,想从最后一个盘符压缩出80GB的空来安装黑苹 ...