学习Android的时候写个小程序需要用到获取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权限”