Android tips: How to execute the ADB shell command in an Android program

Source: Internet
Author: User
Tags gettext stringbuffer
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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.