Objective c () cannot be redirected. it has been entangled for two days. Fortunately, google has found a useful article: String [] cmd {sh,-c, tcpdump-vv-l/sdcard/lishuo2.txt}; Runtime. getRun
Objective c () cannot be redirected. it has been entangled for two days. Fortunately, google has found a useful article:
String [] cmd = {"sh", "-c ","Tcpdump-Vv-l>/sdcard/lishuo2.txt "};
Runtime.getruntime(cmd.exe c (cmd );Key period, used to store the captured packet data in the lishuo2.txt file
Private void find_and_modify_text_view () throws IOException{
FileReader fr = new FileReader ("/mnt/sdcard/lishuo2.txt ");
BufferedReader br = new BufferedReader (fr );
String str = null;
TextView text_view = (TextView) findViewById (R. id. text_view );
While (str = br. readLine ())! = Null ){
Str = br. readLine ();
Text_view.append ("\ n" + str );
}
} Key sentence, used to read data into the text_view control
How to End tcpdump is still under study. the specific interface is ugly. you need to add functions later.
MainActivity. java:
Package lishuo. hook;
Import java. io. IOException;
Import android. app. Activity;
Import android. content. Intent;
Import android. OS. Bundle;
Import android. util. Log;
Import android. view. View;
Import android. widget. Button;
Public class MainActivity extends Activity {
@ Override
Public void onCreate (BundlesavedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
Start_button ();
Stop_button ();
Read_button ();
}
Private void start_button (){
Button button = (Button) findViewById (R. id. start_button );
Button. setOnClickListener (start_button_listener );
}
Private void stop_button (){
Button button = (Button) findViewById (R. id. stop_button );
Button. setOnClickListener (stop_button_listener );
}
Private void read_button (){
Button button = (Button) findViewById (R. id. read_button );
Button. setOnClickListener (read_button_listener );
}
Private Button. OnClickListenerstart_button_listener = new Button. OnClickListener (){
Public void onClick (View v ){
Try {
String [] cmd = {"sh", "-c", "tcpdump-Vv-l>/sdcard/lishuo2.txt "};
Runtime.getruntime(cmd.exe c (cmd );
} Catch (IOException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
}
}
};
Private Button. OnClickListenerstop_button_listener = new Button. OnClickListener (){
Public void onClick (View v ){
Try {
Runtime.getruntime(cmd.exe c ("killall tcpdump ");
} Catch (IOException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
}
}
};
Private Button. OnClickListenerread_button_listener = new Button. OnClickListener (){
Public void onClick (View v ){
//Try {
//String [] cmd = {"sh", "-c", "tcpdump-r/sdcard/lishuo1.txt>/sdcard/lishuo8.txt "};
// Runtime.getruntime(cmd.exe c (cmd );
//} Catch (IOException e ){
// TODO Auto-generated catch block
// E. printStackTrace ();
//}
Intent intent = new Intent ();
Intent. setClass (MainActivity. this, ViewTextActivity. class );
StartActivity (intent );
}
};
}
ViewTextActivity. java
Package lishuo. hook;
Import android. app. Activity;
Import android. content. Intent;
Import android. OS. Bundle;
Import android. util. Log;
Import android. view. View;
Import android. widget. Button;
Import android. widget. CheckBox;
Import android. widget. TextView;
Import java. io .*;
Public class ViewTextActivity extends Activity {
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetTitle ("ViewTextActivity ");
SetContentView (R. layout. text );
Try {
Find_and_modify_text_view ();
} Catch (IOException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
}
}
Private void find_and_modify_text_view () throws IOException{
FileReader fr = new FileReader ("/mnt/sdcard/lishuo2.txt ");
BufferedReader br = new BufferedReader (fr );
String str = null;
TextView text_view = (TextView) findViewById (R. id. text_view );
// CharSequence text_view_old = text_view.getText ();
While (str = br. readLine ())! = Null ){
Str = br. readLine ();
Text_view.append ("\ n" + str );
}
}
}
In this case, you are too lazy to typeset. you need to improve the functions later.