Friday, 11 May 2012

Run Linux commands on Android

It is possible for you to run a few Linux commands on your Android device. Actually, if you root it, you can run a lot more, but for now, let me just tell you how to run the commands which do not require any super-user access.

Well, it is not too different from executing a shell command in an ordinary Java program.

However, as you would like to see the console output of the command you executed, let us create a simple layout as follows,


Once that is done, you will have to create an activity that runs the command, and fills the TextView from the InputStream of the process. It is best to run the process in a thread of its own, to make sure your app does not become non-responsive. Here are the contents of the activity,

Your output will be something like this,

Note that not all commands that you can typically run on a Linux terminal will be available to you on Android. Nor, is the directory system exactly like that of a desktop Linux flavor. To find what commands you can run on your phone, you will have run the command,
 ls /system/bin 

Some of the commands that you might want to try are (which are sure to be available in almost all devices),
  • ls
  • ps
  • df
  • printenv
  • dumpsys (takes a while to complete)

No comments:

Post a Comment