Executing Shell commands is useful to perform deep operating system operations. In this tutorial we are going to see how to execute Linux shell commands with your Android Application.
Creating Project
Create a new Android Application project in eclipse with package as com.learn2crack.androidshell. Create the main layout as activity_main and main Activity as MainActivity.
Creating Layout
Our main layout consists of a EditText widget to get shell command as input. It also has a Button widget to execute the command. It also has a TextView widget to display the outupt of the command which is executed.
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_gravity="center"
tools:context=".MainActivity" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textAlignment="center"
android:text="Shell Executer" />
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/txt"
android:hint="Example - ls" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/btn"
android:text="Execute" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/out"
android:textAlignment="textStart" />
</LinearLayout>
Creating Activity
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_gravity="center"
tools:context=".MainActivity" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textAlignment="center"
android:text="Shell Executer" />
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/txt"
android:hint="Example - ls" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/btn"
android:text="Execute" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/out"
android:textAlignment="textStart" />
</LinearLayout>
Creating Activity
Before proceeding to MainActivity class we need to create ShellExecuter class which has the function to execute shell commands. It has a function Executer which has a parameter command. The command is executed using the function Runtime.getRuntime().exec(command). Then we use BufferReader to read the output and parse it.
ShellExecuter.java
package com.learn2crack.androidshell;
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class ShellExecuter {
public ShellExecuter() {
}
public String Executer(String command) {
StringBuffer output = new StringBuffer();
Process p;
try {
p = Runtime.getRuntime().exec(command);
p.waitFor();
BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = "";
while ((line = reader.readLine())!= null) {
output.append(line + "\n");
}
} catch (Exception e) {
e.printStackTrace();
}
String response = output.toString();
return response;
}
}
In MainActivity we are initially importing the layout items. When the button is pressed the Executer() function is invoked and the output is displayed in a TextView.
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class ShellExecuter {
public ShellExecuter() {
}
public String Executer(String command) {
StringBuffer output = new StringBuffer();
Process p;
try {
p = Runtime.getRuntime().exec(command);
p.waitFor();
BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = "";
while ((line = reader.readLine())!= null) {
output.append(line + "\n");
}
} catch (Exception e) {
e.printStackTrace();
}
String response = output.toString();
return response;
}
}
In MainActivity we are initially importing the layout items. When the button is pressed the Executer() function is invoked and the output is displayed in a TextView.
MainActivity.java
package com.learn2crack.androidshell;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.app.Activity;
import com.learn2crack.androidshell.ShellExecuter;
public class MainActivity extends Activity {
EditText input;
Button btn;
TextView out;
String command;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
input = (EditText)findViewById(R.id.txt);
btn = (Button)findViewById(R.id.btn);
out = (TextView)findViewById(R.id.out);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
ShellExecuter exe = new ShellExecuter();
command = input.getText().toString();
String outp = exe.Executer(command);
out.setText(outp);
Log.d("Output", outp);
}
});
}
}
Creating Manifest
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.app.Activity;
import com.learn2crack.androidshell.ShellExecuter;
public class MainActivity extends Activity {
EditText input;
Button btn;
TextView out;
String command;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
input = (EditText)findViewById(R.id.txt);
btn = (Button)findViewById(R.id.btn);
out = (TextView)findViewById(R.id.out);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
ShellExecuter exe = new ShellExecuter();
command = input.getText().toString();
String outp = exe.Executer(command);
out.setText(outp);
Log.d("Output", outp);
}
});
}
}
Creating Manifest
We do not need any special permissions for our Project.
Screenshots
Any questions comment here.
No comments:
Post a Comment