Android: how can execute a chmod on rooted devides

2019-02-15 20:42发布

问题:

I would execute a command on my rooted Android 2.1 device

String path = "/data/data/com.android.providers.settings/databases/settings.db"; Runtime.getRuntime().exec("/system/bin/chmod -f 777 " + path);

But this command does nothing on the targeted file. Any idea?

回答1:

You need to get the runtime as the root user first. There is a nice ShellInterface class that you can use from the MarketEnabler source available on Google Code. Though keep in mind this source code is released under the GPL.

Essentially what you need to do is determine where your su command is and create a kind of shell using an input stream and output stream for STDIN and STDOUT. With these you can then push your commands to your "terminal". When you are done all your commands, flush your buffer and then wait for the Runtime to complete. Once it is completed, you can then close your runtime interface.

Then take a look at the file you have tried creating/modifying/etc to see if everything worked properly.



回答2:

The RootTools library offers simple methods to check for root and issue root commands:

RootTools.isRootAvailable()
List<String> output = RootTools.sendShell(command);

http://code.google.com/p/roottools/



回答3:

Bao Le I believe you are trying to drop shell commands in an Android App, here (Running Android Native Code in your Android app)are few of many ways to run a command from an app.