MainActivity.java
package com.example.linuxcommand;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends ActionBarActivity {
TextView tv;
EditText et;
Button bt;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bt=(Button) findViewById(R.id.button1);
tv=(TextView) findViewById(R.id.textView2);
et=(EditText) findViewById(R.id.editText1);
bt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d("TAG", "This is onclick method");
String command= et.getText().toString();
tv.setText(command);
String output=LinuxRun(command);
tv.setText(output);
}
});
}
public String LinuxRun(String command) {
StringBuffer retu = new StringBuffer();
Log.d("TAG", "Linux coomand.."+command);
Process p;
try {
p = Runtime.getRuntime().exec(command);
p.waitFor();
BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
String name = "";
while ((name = reader.readLine())!= null) {
retu.append(name + "\n");
}
} catch (Exception e) {
e.printStackTrace();
}
String output = retu.toString();
return output;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Above file is java file it consists of initialization if elements and out will show using TextView on screen
activity_main.xml
<RelativeLayout 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"
tools:context="com.example.linuxcommand.MainActivity" >
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginTop="26dp"
android:layout_toRightOf="@+id/textView1"
android:ems="10" >
<requestFocus />
</EditText>
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/editText1"
android:layout_alignBottom="@+id/editText1"
android:layout_alignParentLeft="true"
android:text="Enter Command" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/editText1"
android:layout_marginTop="24dp"
android:layout_toRightOf="@+id/textView1"
android:text="output" />
<ScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/scrollView1"
android:layout_below="@+id/button1"
android:layout_marginTop="38dp" >
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</ScrollView>
/>
</RelativeLayout>
This is layout of main activity it having Text-view,Edit-text and Button after entering command in Edit-text when ever user press button it show out put on screen following are my screenshot of above code.
The above screenshot is showing of "ls" command out put and i applied ScrollView to TextView it will increase based on output
The above screenshot is showing of "cat /proc/cpuinfo" it will show CPU information of Android mobile it will show CPU information
Thank for studying please put comments and doubts of post i will improve next posts it will be very use-full for me.
No comments:
Post a Comment