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");
   os.flush();
} 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:

AskingRootActivity.java

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

@Override
protected void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.activity_asking_root);
}
private void nextScreen(){
   Intent intent = new Intent(getApplicationContext(), NextActivity.class);
   startActivity(intent);
}

public void checkRoot(View v) {
   Process p;
   try {
      p = Runtime.getRuntime().exec("su");
      DataOutputStream os = new DataOutputStream(p.getOutputStream());
      os.writeBytes("ls /data\n");
      os.writeBytes("exit\n");
      os.flush();
      try {
         p.waitFor();
         if (p.exitValue() != 1) {/
         nextScreen();
         Log.d(TAG, "success getting root");
      }
      else {
         TextView tv = (TextView) findViewById(R.id.actionMsg);
         tv.setText(R.string.root_error);
         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.

activity_asking_root.xml

<RelativeLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
    xmlns:tools=&quot;http://schemas.android.com/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:paddingRight=&quot;@dimen/activity_horizontal_margin&quot;
    android:paddingTop=&quot;@dimen/activity_vertical_margin&quot;
    android:paddingBottom=&quot;@dimen/activity_vertical_margin&quot;
    tools:context=&quot;es.guille.sample.root.activities.AskingRootActivity&quot;>

    <TextView
        android:id=&quot;@+id/actionMsg&quot;
        android:text=&quot;@string/asking_root&quot; 
        android:layout_width=&quot;wrap_content&quot;
        android:layout_height=&quot;wrap_content&quot; />

    <Button
        android:id=&quot;@+id/nextbtn&quot;
        android:onClick=&quot;checkRoot&quot;
        android:text=&quot;@string/ask_for_root&quot;
        android:layout_width=&quot;fill_parent&quot;
        android:layout_height=&quot;wrap_content&quot;
        android:layout_below=&quot;@id/actionMsg&quot;/>

</RelativeLayout>

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

Advertisements

Create a free website or blog at WordPress.com.