How to get Root Access for android app?

2019-09-05 09:29发布

问题:

I am working on an android app, and I want the user to be able to access the data directory. I know you can do, Runtime.getRuntime().exec("su");, but I tried that and it opens up the screen that asks if you want to get permission, but it does not allow me to retrieve the directory after words. Here is the code:

try {
                Runtime.getRuntime().exec("su");
                final File dataFile = Environment.getDataDirectory();
                FileAdapter adapter = new FileAdapter(getActivity());
                adapter.setFiles(dataFile.listFiles());
                setListAdapter(adapter);
                setListShown(true);
            } catch (IOException e) {
                Toast.makeText(getActivity(),"Sorry, Root access was denied",Toast.LENGTH_LONG).show();
            }

回答1:

The Runtime.exec() documentations says Executes the specified program in a separate native process. Which means that, its the new process that gets the root access. I think you have to use the Process object that is returned by the exec() and open output stream and execute the remaining commands that accomplishes your job. You can refer to Root Tools source code for more info on how to do that.