Android手机如何扫码修改手机本地host绑定

上一篇 / 下一篇  2015-10-26 19:20:18 / 个人分类:step by step android测试


前提条件:手机必须是root过的。
ps:过程中,我也搜索了好多类似的文章,被注释掉的部分是我之前尝试过的,但是不行,我也不清楚为什么,若谁晓得,也麻烦告知我下。
/**
String command1 = "mount -o rw,remount /system";
String cmd = "su";
String command2 = "chmod 777 /etc/hosts";
String command3 = "echo \"999.31.17.184 github.global.ssl.fastly.net\" >> /etc/hosts";
//String strDir, String cmd, StringBuilder buffer
Boolean result1 = ShellUtils.execShell("/system/etc/hosts",command1, new StringBuilder());
Boolean result = ShellUtils.execShell("/system/etc/hosts",cmd, new StringBuilder());
Boolean result2= ShellUtils.execShell("/system/etc/hosts",command2, new StringBuilder());
Boolean result3= ShellUtils.execShell("/system/etc/hosts",command3, new StringBuilder());
**/
由于目前很多app都实现了扫码的功能,在此我只介绍如何来修改hosts文件啦,以下是代码(其实它可以修改手机中的任何文件,只要传入文件的path即可):
/**
* 将内容写到文件中
*
*@paramfilePath
*@paramcontent
*/
public static voidwriteFile(String filePath, String content) {

try{
//获取对应的读写权限,出于安全考虑,记得完事后remount回只读: mount -o ro,remount /system
String cmd1="mount -o rw,remount -t yaffs2 /dev/block/mtdblock3 /system";
String cmd2="chmod 777 "+filePath;
exe(cmd1);
exe(cmd2);
//在这里先清空host绑定,以免有干扰
exe("> "+filePath);
String []contents=content.split("\n");
for(inti=0;i<contents.length;i++) {
if(contents[i].startsWith("这里是扫码的识别码")){
contents[i]="##"+contents[i];
}
String cmd3 ="echo "+"\""+contents[i] +"\""+" >> /etc/hosts";
exe(cmd3);
}
}catch(Exception e) {
e.printStackTrace();
}finally{
//这里可以把它重新变为之前的读写模式
}

}

//执行对应的命令函数
public staticProcess exe(String cmd)throwsIOException {
DataOutputStream dos =null;
Process ps =null;
try{
ps = Runtime.getRuntime().exec("su");
dos =newDataOutputStream(ps.getOutputStream());
dos.writeBytes(cmd+"\n");
dos.flush();
}catch(IOException e) {
e.printStackTrace();
}finally{
dos.close();
}
returnps;

}

TAG: Android 如何 手机

引用 删除 goodpress   /   2015-10-29 21:34:49
hi,您好,能否加一下我的微信,交个朋友.聊聊app测试的一些点,我的微信号 3O483996I
 

评分:0

我来说两句

Open Toolbar