Package Net.gimite.nativeexe;
Import Java.io.BufferedReader;
Import Java.io.FileOutputStream;
Import java.io.IOException;
Import Java.io.InputStream;
Import Java.io.InputStreamReader;
Import java.net.HttpURLConnection;
Import java.net.MalformedURLException;
Import Java.net.URL;
Import NET.GIMITE.NATIVEEXE.R;
Import android.app.Activity;
Import Android.os.Bundle;
Import Android.os.Handler;
Import Android.view.View;
Import Android.view.View.OnClickListener;
Import android.widget.*;
public class Mainactivity extends activity {private TextView Outputview;
Private Button Localrunbutton;
Private EditText Localpathedit;
Private Handler Handler = new Handler ();
Private EditText Urledit;
Private Button Remoterunbutton; /** called the activity is a.
* * @Override public void onCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.main);
Outputview = (TextView) Findviewbyid (R.id.outputview);
Localpathedit = (edittext) Findviewbyid (R.id.localpathedit);
Localrunbutton = (Button) Findviewbyid (R.id.localrunbutton);
Localrunbutton.setonclicklistener (Onlocalrunbuttonclick); Private Onclicklistener Onlocalrunbuttonclick = new Onclicklistener () {public void OnClick (View
V) {String output = EXEC (Localpathedit.gettext (). toString ());
Output (output); try {/////Process Process = Runtime.getruntime (). EXEC (Localpathedit.gettext (). Tostrin
g ()); catch (IOException e) {////TODO auto-generated catch block//E.
Printstacktrace ();
// }
}
};
Executes UNIX command. Private String Exec (string command) {try {ProcESS process = Runtime.getruntime (). exec (command);
BufferedReader reader = new BufferedReader (New InputStreamReader (Process.getinputstream ()));
int read;
char[] buffer = new char[4096];
StringBuffer output = new StringBuffer ();
while (read = Reader.read (buffer)) > 0) {output.append (buffer, 0, read);
} reader.close ();
Process.waitfor ();
return output.tostring ();
catch (IOException e) {throw new RuntimeException (e);
catch (Interruptedexception e) {throw new RuntimeException (e); }} private void Download (string urlstr, String localpath) {try {URL url = ne
W URL (URLSTR);
HttpURLConnection urlconn = (httpurlconnection) url.openconnection ();
Urlconn.setrequestmethod ("get"); UrlConn.setinstancefollowredirects (TRUE);
Urlconn.connect ();
InputStream in = Urlconn.getinputstream ();
FileOutputStream out = new FileOutputStream (localpath);
int read;
byte[] buffer = new byte[4096];
while (read = In.read (buffer)) > 0) {out.write (buffer, 0, read);
} out.close ();
In.close ();
Urlconn.disconnect ();
catch (Malformedurlexception e) {throw new RuntimeException (e);
catch (IOException e) {throw new RuntimeException (e); } private void output (final String str) {Runnable proc = new Runnable () {PU
Blic void Run () {outputview.settext (str);
}
};
Handler.post (proc); }
}
To join a permission
See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/OS/extra/
<uses-permission android:name= "Android.permission.INTERNET" ></uses-permission>
Effect chart
Source Download Address : http://download.csdn.net/detail/gshengod/6932845