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.

March 19, 2008

Cleaning your computer

Filed under: computer — Tags: , , , , , , — guilleml @ 5:20 pm

A computer is like a vacuum cleaner, it sucks all the dust in the floor and the room and collects it inside. You can see all the dust in the fans and in the heat dissipaters.

Dust make your fans make more noise, can block the air flow so your components won’t be well cooled, etc.
You will need some tools to clean all the dust, a vacuum cleaner, some screwdrivers and maybe some cleaning cloths, silicone based lubricant…

Disconnect all the wires from the computer and open it, start removing every card, fan, optical units, hard drives, floppy drives, power supply…take care to remember where were connected all the wires, fans and so.

Take a good look, there is a lot of dust that need to be removed or will become a problem in some dissipater, fan or in your lungs.

Once cleaned:

Ooooh, silence…

Blog at