[Android] LynXT:
Start your project

First of all, install eclipse and the android stuff

Then create a new android project, in my case its called com.xda.tutorialscript

Then we need to create the required layout items

Edit res->layout->main.xml

Add a Edittext, Button and TextView

<?xml version=”1.0″ encoding=”utf-8″?>

<LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android”

android:layout_width=”fill_parent”

android:layout_height=”fill_parent”

android:orientation=”vertical” >

<EditText

android:id=”@+input/input1″

android:layout_height=”wrap_content”

android:layout_width=”fill_parent”

/>

<Button

android:id=”@+button/run”

android:layout_height=”wrap_content”

android:layout_width=”wrap_content”

android:text=”Go”

/>

<TextView

android:id=”@+text/text1″

android:layout_width=”fill_parent”

android:layout_height=”wrap_content”

android:text=”Output” />

</LinearLayout>

After that we need to create the code for the root command, bellow there is my version with output treatment and input of a textview to be used as input also, by that you can use the same command on lots of activities and have the output on the textview

// Root Access script runner

void execCommandLine(String command, TextView tv)

{

Runtime runtime = Runtime.getRuntime();

Process proc = null;

OutputStreamWriter osw = null;

// Running the Script

try

{

proc = runtime.exec(“su”);

osw = new OutputStreamWriter(proc.getOutputStream());

osw.write(command);

osw.flush();

osw.close();

}

// If return error

catch (IOException ex)

{

// Log error

Log.e(“execCommandLine()”, “Command resulted in an IO Exception: ” + command);

return;

}

// Try to close the process

finally

{

if (osw != null)

{

try

{

osw.close();

}

catch (IOException e){}

}

}

try

{

proc.waitFor();

}

catch (InterruptedException e){}

// Display on screen if error

if (proc.exitValue() != 0)

{

Log.e(“execCommandLine()”, “Command returned error: ” + command + “\n Exit code: ” + proc.exitValue());

AlertDialog.Builder builder = new AlertDialog.Builder(ScriptrunActivity.this);

builder.setMessage(command + “\nWas not executed sucessfully!”);

builder.setNeutralButton(“OK”, null);

AlertDialog dialog = builder.create();

dialog.setTitle(“Script Error”);

dialog.show();

}

BufferedReader reader = new BufferedReader(

new InputStreamReader(proc.getInputStream()));

int read;

char[] buffer = new char[4096];

StringBuffer output = new StringBuffer();

try {

while ((read = reader.read(buffer)) > 0) {

output.append(buffer, 0, read);

}

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

String exit = output.toString();

if(exit != null && exit.length() == 0) {

exit = “Command executed Successfully but no output was generated”;

}

tv.setText(exit);

}

After that we have to create a button to grab the text inside the textedit and use as a command

// Button

/** Called when the activity is first created. */

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

Button go = (Button) findViewById(R.button.run);

go.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View view) {

// Get ID’s

TextView tv = (TextView)findViewById(R.text.text1);

EditText text_id = (EditText)findViewById(R.input.input1);

String input = text_id.getText().toString();

execCommandLine(input, tv);

}

});

A simple explanation of what happens:

The findViewById is used to get the id of the layout item, so we can use it later and by that we declare the textview id ( to be used as input of the execcommand) and button id

Then we get the text from the input1 that is the textedit and transform it to a string to be used in the execCommandLine

And for last we use the execCommandLine

The string input will be used and the output will be transformed from inputstream to string and sent to the textview choosed

Hope you all enjoyed this Do it Your Self

madteam.co