程序中执行shell命令行,获取system目录读写权限

////执行shell命令行
public void execShell(String cmd){
        try{  
            //权限设置
            Process p = Runtime.getRuntime().exec("su");  
            //获取输出流
            OutputStream outputStream = p.getOutputStream();
            DataOutputStream dataOutputStream = new DataOutputStream(outputStream);
           
            //将命令写入
            dataOutputStream.writeBytes(cmd);
            //提交命令
            dataOutputStream.flush();
            //关闭流操作
            dataOutputStream.close();
            outputStream.close();
       }  
       catch(Throwable t)  
        {  
             t.printStackTrace();
            }
    }

//权限配置

 
    //sd卡操作权限
   
   


////比如执行将应用cp到system/app

private String com_cp = "cp " + "/sdcard/myapp.apk" + " /system/app/";  

execShell(com_cp );


////获取system目录的读写权限

private final String com_rw = "mount -o remount " + "/dev/block/mtdblock8" + " /system"; 

execShell(com_rw);

////设置只读权限
private final String com_ro = "mount -o remount,ro " + "/dev/block/mtdblock8" + " /system";

execShell(com_ro);

////查询system分区名和权限:在shell命令行执行mount

 # adb shell

root@android:/ # mount
rootfs / rootfs ro,relatime 0 0
tmpfs /dev tmpfs rw,nosuid,relatime,mode=755 0 0
devpts /dev/pts devpts rw,relatime,mode=600 0 0
proc /proc proc rw,relatime 0 0
sysfs /sys sysfs rw,relatime 0 0
none /acct cgroup rw,relatime,cpuacct 0 0
tmpfs /mnt/secure tmpfs rw,relatime,mode=700 0 0
tmpfs /mnt/asec tmpfs rw,relatime,mode=755,gid=1000 0 0
tmpfs /mnt/obb tmpfs rw,relatime,mode=755,gid=1000 0 0
none /dev/cpuctl cgroup rw,relatime,cpu 0 0
/dev/block/mtdblock8 /system ext4 rw,relatime,barrier=1,data=ordered,noa
lloc 0 0
/dev/block/mtdblock6 /data ext4 rw,nosuid,nodev,noatime,nodiratime,barri
a=ordered,noauto_da_alloc 0 0
/dev/block/mtdblock5 /cache ext4 rw,nosuid,nodev,noatime,nodiratime,barr
ta=ordered,noauto_da_alloc 0 0
/sys/kernel/debug /sys/kernel/debug debugfs rw,relatime 0 0
/dev/block/vold/8:1 /mnt/usb_storage/USB_DISK0/HDD fuseblk rw,relatime,u
,group_id=0,allow_other,blksize=4096 0 0
/dev/block/vold/31:9 /mnt/sdcard vfat rw,dirsync,nosuid,nodev,noexec,noa
iratime,uid=1000,gid=1015,fmask=0002,dmask=0002,allow_utime=0020,codepag
iocharset=iso8859-1,shortname=mixed,utf8,errors=remount-ro 0 0
/dev/block/vold/31:9 /mnt/secure/asec vfat rw,dirsync,nosuid,nodev,noexe
e,nodiratime,uid=1000,gid=1015,fmask=0002,dmask=0002,allow_utime=0020,co
p437,iocharset=iso8859-1,shortname=mixed,utf8,errors=remount-ro 0 0
tmpfs /mnt/sdcard/.android_secure tmpfs ro,relatime,size=0k,mode=000 0 0



参考 http://blog.csdn.net/tabactivity/article/details/12629873


你可能感兴趣的:(Android)