Yet Another Blog

January 21, 2015

Asking for root in your Android app

Filed under: Android, computer, Java, Software — Tags: , , , , , — guilleml @ 12:34 pm

If your app needs root permissions to execute any command, you can do this using something like:

Process p;
try {
   p = Runtime.getRuntime().exec("su");
   DataOutputStream os = getRootSession(p);
   os.writeBytes("any command you want\n");
} catch (IOException e1) {
   Log.d(MainActivity.class.getName(), "Error: " + e1.getMessage());

After running Runtime.getRuntime().exec(“su”) a dialog will appear to ask for permissions so the user can accept it.
What happens if you’re writing a service that will need root access anytime, even if the screen is off? The user won’t be able to accept the dialog so the app will fail.

To solve this you can do your app to ask for root permissions when it runs so, if the user accepts forever, the app will be able to run normally without asking the user anytime.
I know it’s better the user knows when an app is doing something as root but there are cases where you need to do this, for example, I’m writing an app that will disable charging for a phone when
the battery is charget at 100% in order to avoid microcharges. If you let the phone charging at night you won’t be there to disconnect the phone when it gets to 100% so this app monitors battery charge level and will use root permissions when the battery is 100%.

What I’ve done is to create an initial activiy to ask for permissions:

public class AskingRootActivity extends ActionBarActivity {
private static final String TAG = AskingRootActivity.class.getName();

protected void onCreate(Bundle savedInstanceState) {
private void nextScreen(){
   Intent intent = new Intent(getApplicationContext(), NextActivity.class);

public void checkRoot(View v) {
   Process p;
   try {
      p = Runtime.getRuntime().exec("su");
      DataOutputStream os = new DataOutputStream(p.getOutputStream());
      os.writeBytes("ls /data\n");
      try {
         if (p.exitValue() != 1) {/
         Log.d(TAG, "success getting root");
      else {
         TextView tv = (TextView) findViewById(;
         Log.d(TAG, "failing getting root");
   } catch (InterruptedException e) {
      Log.d(TAG, "failing getting root");
} catch (IOException e) {
   Log.d(TAG, "failing getting root");

When the user clicks on the button, the app will try to get a root session so a dialog asking for root will appear.

After the user accepts or denies it, we try to do something we can only do as root, like listing files in /data directory. If the command runs succesfully, we navigate to the next screen/activity/logic/whatever, if not, we can show an error or finish the app.
As the app won’t do anything untill the user accepts or denies it, we can be sure when the app gets to the point when it needs root access, it will have it, at least if the user didn’t set a timeout for the permission.


<RelativeLayout xmlns:android=&quot;;
    xmlns:tools=&quot;; android:layout_width=&quot;match_parent&quot;
    android:layout_height=&quot;match_parent&quot; android:paddingLeft=&quot;@dimen/activity_horizontal_margin&quot;

        android:layout_height=&quot;wrap_content&quot; />



This layout only defines an info message and a button to ask for root permissions.

Create a free website or blog at