当前位置:编程学习 > JAVA >>

android如何在程序中获得ROOT权限?

我正在尝试制作一款安卓APP,需要查看data文件夹下的很多文件。
但是每次打开都读不到。。
查原因说是因为没有ROOT权限。。
怎么获得ROOT权限呢?在程序中。。最好能直接给代码。。 --------------------编程问答--------------------
前提是你的手机已经有root权限,否则没辙。你看其他一些app,要使用root权限,但是手机没有(未破解),还是永不了

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了。
如果是自己root的,应该有超级用户选项,在这个选项中把你的工程添加最高权限到就可以了。
如果在程序中获得的话,可以使用su命令进行操作。这时候应该手机上会提示你是否赋给root权限。

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String command="";//这里可以添加命令
runRootCommand();
System.out.println("命令执行结束");
try {
otherContext = getTestBContext();
} catch (NameNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
setContentView(new Show(this));

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}


 
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");
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;
}
}
补充:Java ,  Java相关
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,