Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2441911
  • 博文数量: 293
  • 博客积分: 2660
  • 博客等级: 少校
  • 技术积分: 3632
  • 用 户 组: 普通用户
  • 注册时间: 2009-11-03 17:50
文章分类

全部博文(293)

文章存档

2015年(13)

2014年(58)

2013年(73)

2012年(25)

2011年(30)

2010年(86)

2009年(8)

分类: Android平台

2014-03-23 09:46:53


http://blog.csdn.net/jingwen3699/article/details/8175937
要在android应用程序中使用root权限,那么运行程序的设备必须具有root权限
  public static boolean runRootCommand(String command) {
  Process process = null;
  DataOutputStream os = null;
  try {
  process = Runtime.getRuntime().exec("su");
  os = new DataOutputStream(process.getOutputStream());
  os.writeBytes(command+"\n"); \"Do I have root?\" >/system/sd/temporary.txt\n");
   os.writeBytes("exit\n");
  os.flush();
  process.waitFor();
  } catch (Exception e) {
  Log.d(TAG, "the device is not rooted, error message: " + e.getMessage());
  return false;
  } finally {
  try {
  if (os != null) {
  os.close();
  }
  if(process != null) {
  process.destroy();
  }
  } catch (Exception e) {
  e.printStackTrace();
  }
  }
  return true;
  }

如果设备获取了root权限,那么程序执行su命令时,就会提示用户进行授权,如图:

写个小程序需要用到获取ROOT权限,在网上找了好久,发现这种方法可行,前提时设备必须已经破解过!能执行su命令
一、建一个方法:代码如下:

package cn.ycmoon.utility;
import java.io.DataOutputStream;
import android.app.Activity;
import android.util.Log;
public class SystemManager extends Activity
{
    /**
     * 应用程序运行命令获取 Root权限,设备必须已破解(获得ROOT权限)
     * @param command 命令:String apkRoot="chmod 777 "+getPackageCodePath(); RootCommand(apkRoot);
     * @return 应用程序是/否获取Root权限
     */
    public static boolean RootCommand(String command)
    {
        Process process = null;
        DataOutputStream os = null;
        try
        {
            process = Runtime.getRuntime().exec("su");
            os = new DataOutputStream(process.getOutputStream());
            os.writeBytes(command + "\n");
            os.writeBytes("exit\n");
            os.flush();
            process.waitFor();
        } catch (Exception e)
        {
            Log.d("*** DEBUG ***", "ROOT REE" + e.getMessage());
            return false;
        } finally
        {
            try
            {
                if (os != null)
                {
                    os.close();
                }
                process.destroy();
            } catch (Exception e)
            {
            }
        }
        Log.d("*** DEBUG ***", "Root SUC ");
        return true;
    }
}
二、在应用程序的MainActivity方法中:
public class MainActivity extends Activity
{
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        String apkRoot="chmod 777 "+getPackageCodePath();
        SystemManager.RootCommand(apkRoot);
    }
这样,在应用程序运行的时候,会弹出消息对话框“应用程序已获取root权限”
===================================
===================================
android应用需要调用shell命令的时候,网上找到的资料是:
String cmd = String.format("echo %s > %s\n", arg, mSwitchUsbFileTextEdit.getText().toString());
        try {
            Process exeEcho = Runtime.getRuntime().exec("su");
            exeEcho.getOutputStream().write(cmd.getBytes());
            exeEcho.getOutputStream().flush();
        } catch (IOException e) {
            showMessage("Excute exception: " + e.getMessage());
        }
注意,之所以需要执行su命令,而不是直接执行echo命令,是因为那样会跑出IOException: Working Directory: null    Environment: null。但是如果是在装有SuperRoot应用的机器上,su命令可能被拒绝,而且执行普通的命令应该不需要使用su命令才对。试验了一下,Process exeEcho = Runtime.getRuntime().exec("su");这一行改成Process exeEcho = Runtime.getRuntime().exec("sh");也可以执行。查了一下资料:
http://www.cnblogs.com/zhengwenwei/archive/2011/08/16/2141642.html
阅读(20643) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~