2013-09-26 104 views

回答

1

你需要采取一些步骤才能做到这一点。

首先(当然)该设备需要扎根。您可以通过多种方式查看。

以下代码将检查“su”命令是否返回命令未找到错误(su二进制存在),并且安装了超级用户应用程序以在请求它们后授予权限。

private boolean isDeviceRooted() { 

      // check for SU command in shell 
      if ((new ExecShell().executeCommand(ExecShell.SHELL_COMMAND.su_check) != null) && (appInstalled("eu.chainfire.supersu.nonag") || appInstalled("eu.chainfire.supersu") || appInstalled("com.noshufou.android.su") || appInstalled("com.koushikdutta.superuser"))) { 
       Log.i(TAG, "Device Rooted"); 
       return true; 
      } 

      // check for SU application installed 
      if (appInstalled("eu.chainfire.supersu.nonag") || appInstalled("eu.chainfire.supersu") || appInstalled("com.noshufou.android.su") || appInstalled("com.koushikdutta.superuser")) { 
       Log.i(TAG, "Device Rooted"); 
       return true; 
      } 

      Log.i(TAG, "Device Not Rooted"); 
      return false; 
     } 


     private boolean appInstalled(String uri) { 
      PackageManager pm = getPackageManager(); 
      boolean app_installed = false; 
      try { 
       pm.getPackageInfo(uri, PackageManager.GET_ACTIVITIES); 
       app_installed = true; 
      } catch (PackageManager.NameNotFoundException e) { 
       app_installed = false; 
      } 
      return app_installed; 
     } 

如果此代码返回false,则可以设置标志或显示和错误,否则继续。

然后,一旦您知道该设备已生根,您就需要执行必要的root命令来执行您所需的操作。

以下代码以命令的形式输入String [],并以root身份依次执行它们。

public boolean RunAsRoot(String[] cmds) { 
    Process p; 
    try { 
     p = Runtime.getRuntime().exec("su"); 
     DataOutputStream os = new DataOutputStream(p.getOutputStream()); 
     try { 
      for (String tmpCmd : cmds) { 
       os.writeBytes(tmpCmd + "\n"); 
      } 
      os.writeBytes("exit\n"); 
      os.flush(); 
      return true; 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
      return false; 
     } 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
     return false; 
    } 
} 

在你的情况下,你将首先想要挂载/ system为rw。有很多的网络上的信息,以帮助您找到您想要的命令,但它看起来像 mount -o remount rw /system mount -o remount rw /system

然后你要移动你想要使用任何mvcp文件。

使用根命令的一个例子是

String[] cmds = {"mount -o remount rw /system mount -o remount rw /system", "cp /sdcard/myfile /system/framework/myfile"}; 
if(!RunAsRoot(cmds)){ 
    //Commands failed to run, show an error/retry 
} 

这包括“硬”位,该位是根功能。

该按钮的简单教程可以找到here

程序流程可能是

onCreate(){ 
    checkIsRooted(); 
    Button x = (Button) findViewById(R.id.x); 
    x.setOnClickListener(onClickListener()); 
} 

onClickListener(){ 
    onClick(){ 
     String[] cmds = {...}; 
     if(!runAsRoot(cmds)) 
      AlertDialog.Builder.makeText(...).show(); 
    } 
} 

注意,这是伪代码,您不能复制并粘贴此代码,使其工作,您需要正确做你自己!