Tuesday, March 31, 2015

Run Linux command on Android

Android system have to Terminal application in default. This example provide user to run Linux command using ProcessBuilder, and display the result. But most Linux command not support in Android.

(If you want try the APK only, scroll down to the bottom.)


package com.example.androidrunlinuxcmd;

import java.io.IOException;
import java.io.InputStream;

import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;


public class MainActivity extends ActionBarActivity {
 
 EditText cmdBox;
 Button btnRun;
 TextView textResult;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        cmdBox = (EditText)findViewById(R.id.cmdbox);
     btnRun = (Button)findViewById(R.id.run);
     textResult = (TextView)findViewById(R.id.result);
     textResult.setTypeface(Typeface.MONOSPACE);
        
  btnRun.setOnClickListener(new OnClickListener(){

   @Override
   public void onClick(View v) {
    //Split String from EditText to String Array
    String[] cmd = cmdBox.getText().toString().split("\\s+"); 
    try {
     String cmdResult = runLinuxCmd(cmd);
     textResult.setTextColor(Color.WHITE);
     textResult.setText(cmdResult);
    } catch (IOException e) {
     e.printStackTrace();
     textResult.setTextColor(Color.RED);
     textResult.setText("Something Wrong!\n"
      + e.getMessage());
    }
   }});
    }

    //Run a Linux command and return result
 private String runLinuxCmd(String[] command) throws IOException {

    StringBuilder cmdReturn = new StringBuilder();

    ProcessBuilder processBuilder = new ProcessBuilder(command);
     Process process = processBuilder.start();

     InputStream inputStream = process.getInputStream();
     int c;

     while ((c = inputStream.read()) != -1) {
      cmdReturn.append((char) c);
     }

     return cmdReturn.toString();
   }
}


<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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:orientation="vertical"
    tools:context="com.example.androidrunlinuxcmd.MainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:autoLink="web"
        android:text="http://android-er.blogspot.com/"
        android:textStyle="bold" />

    <EditText
        android:id="@+id/cmdbox"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:singleLine="true" />

    <Button
        android:id="@+id/run"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Run Linux Command" />

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#A0A0A0" >

        <TextView
            android:id="@+id/result"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="10dp" />
    </ScrollView>

</LinearLayout>


download filesDownload the files.

Download and Try the APK.

No comments: