Android中部署自己的su
本人博客原文
首先把你的自己的su的放到Android应用程序project的assets文件夹,为了和系统的su区分,我自己的su文件叫做sur。
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 sutry {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";}}
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 blocke.printStackTrace();}}}}
Android中部署自己的su的更多相关文章
- Android中怎样自己制作su
本文原博客:http://hubingforever.blog.163.com/blog/static/171040579201372915716149/ 在Android源代码的system\ext ...
- Android动态部署五:怎样从插件apk中启动Service
转载请注明出处:http://blog.csdn.net/ximsfei/article/details/51072332 github地址:https://github.com/ximsfei/Dy ...
- Android中使用开源框架android-image-indicator实现图片轮播部署
之前的博文中有介绍关于图片轮播的实现方式,分别为(含超链接): 1.<Android中使用ViewFlipper实现屏幕切换> 2.<Android中使用ViewPager实现屏幕页 ...
- Android中Fragment和ViewPager那点事儿(仿微信APP)
在之前的博文<Android中使用ViewPager实现屏幕页面切换和引导页效果实现>和<Android中Fragment的两种创建方式>以及<Android中Fragm ...
- Android中Fragment与Activity之间的交互(两种实现方式)
(未给Fragment的布局设置BackGound) 之前关于Android中Fragment的概念以及创建方式,我专门写了一篇博文<Android中Fragment的两种创建方式>,就如 ...
- Android中的五大布局
Android中的五大布局 1.了解布局 一个丰富的界面总是要由很多个控件组成的,那我们如何才能让各个控件都有条不紊地 摆放在界面上,而不是乱糟糟的呢?这就需要借助布局来实现了.布局是一种可用于放置很 ...
- 在Android中调用C#写的WebService(附源代码)
由于项目中要使用Android调用C#写的WebService,于是便有了这篇文章.在学习的过程中,发现在C#中直接调用WebService方便得多,直接添加一个引用,便可以直接使用将WebServi ...
- Android中FTP服务器、客户端搭建以及SwiFTP、ftp4j介绍
本文主要内容: 1.FTP服务端部署---- 基于Android中SwiFTP开源软件介绍: 2.FTP客户端部署 --- 基于ftp4j开源jar包的客户端开发 : 3.使用步骤 --- 如何测试我 ...
- Android中如何像 360 一样优雅的杀死后台服务而不启动
Android中,虽然有很多方法(API或者shell命令)杀死后台`service`,但是仍然有很多程序几秒内再次启动,导致无法真正的杀死.这里主要着重介绍如何像 360 一样杀死Android后台 ...
随机推荐
- 基于karma和jasmine的Angularjs 单元测试
Angularjs 基于karma和jasmine的单元测试 目录: 1. 单元测试的配置 2. 实例文件目录解释 3. 测试controller 3.1 测试controller中变量值是否 ...
- Xamarin For Visual Studio 3.7.165 完整离线破解版
原文 Xamarin For Visual Studio 3.7.165 完整离线破解版 Xamarin For Visual Studio就是原本的Xamarin For Android 以及 Xa ...
- 黄聪:Microsoft Enterprise Library 5.0 系列教程(九) Policy Injection Application Block
原文:黄聪:Microsoft Enterprise Library 5.0 系列教程(九) Policy Injection Application Block 代理对象(Proxy Object) ...
- 【SICP归纳】2 高阶函数和数据抽象
上一篇博文相应的是书中的第一章的一二两节,我们已经大致的有了一种构造的感觉不是么. 书中展示了非常多有趣的句法(syntax). 如今我们要让思想进一步的抽象.写这篇博客的时候并未学完整本书.更不敢说 ...
- 《Head First 设计模式》学习笔记——迭代模式 + 组合模式
迭代模式设置共生死亡,一般来说.我们只是想实现一个集,我们需要的同时提供这个集合的迭代器,喜欢java中间Collection.List.Set.Map等,这些集合都有自己的迭代器.假如我们要实现一个 ...
- Swift语言教程中文文档
Swift语言教程中文文档 Swift语言教程(一)基础数据类型 Swift语言教程(二)基础数据类型 Swift语言教程(三)集合类型 Swift语言教程(四) 集合类型 Swift语言教程(五)控 ...
- 【转】Qt事件循环与线程 二
转自:http://blog.csdn.net/changsheng230/article/details/6153449 续上文:http://blog.csdn.net/changsheng230 ...
- Javascript设计模式系列二
创建对象的基本模式,一.门户大开型,二.采用下划线来表示属性和方法的私用性,三.使用闭包来创建私用的成员. 一.门户大开型.只能提供公用成员.所有属性和方法都公开的.可访问的.这些共用属性都要使用th ...
- 【原创】纯OO:从设计到编码写一个FlappyBird (三)
第二部分请点这里 下面首先来实现Bing接口! 实现Bing接口的类取名SimpleBing. 容易发现,SimpleBing类总的来说要向下,但点击一下又得向上,向上到了一定界限又得向下,但我们又只 ...
- 举例说, Decorator模式(Decorator Pattern)
前言 在食品工业中的装饰图案具有比较广泛的应用,大多数的两个图案和在网上的例子饮食相关的,一旦被称为电影的手表,点咖啡要加糖要加奶昔要加这加那的时候.感觉好有派~好高大上啊~.为啥我在小卖部都是 ...