Package Feng. f8_6.activity; import Java. io. file; import Java. io. fileoutputstream; import Java. io. inputstream; import java.net. httpurlconnection; import java.net. malformedurlexception; import java.net. URL; import android. app. activity; import android. content. intent; import android.net. uri; import android. OS. bundle; import android. OS. environment; import android. OS. handler; import android. OS. message; import android. View. menu; import android. view. view; import android. view. view. onclicklistener; import android. widget. button; import android. widget. toast; public class httpactivity extends activity {private button BTN; @ overridepublic void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); BTN = (button) findviewbyid (R. id. button1); BTN. setonclicklistener (New onclickliste NER () {@ overridepublic void onclick (view v) {New thread () {public void run () {// address available online Oh string Path = "http://res.neng.com/res/apk_sp/20120803/8124_com.mobi.filemanager.activity_163508.apk "; // string Path = "http: // 192.168.1.62: 8080/myhttptest.apk"; try {URL url = new URL (PATH); httpurlconnection conn = (httpurlconnection) URL. openconnection (); If (httpurlconnection. http_ OK! = Conn. getresponsecode () {message = message. obtain (); message. what = 1; handler. sendmessage (Message);} else {If (environment. getexternalstoragestate (). equals (environment. media_unmounted) {message = message. obtain (); message. what = 2; handler. sendmessage (Message);} else {// system. out. println ("length of the retrieved information:" + Conn. getcontentlength (); file = new file (environment. getexternalstoragedirector Y () + "/Feng"); If (! File. exists () {file. mkdir ();} // system. out. println ("file. getpath (): "// + file. getpath (); // system. out. println ("getcontentlength:" // + Conn. getcontentlength (); file cfile = new file (file. getpath (), "activity_163508.apk"); If (! Cfile. exists () {cfile. createnewfile ();} inputstream is = Conn. getinputstream (); fileoutputstream OS = new fileoutputstream (cfile); byte [] buffer = new byte [2048]; int A = 0; while (A = is. read (buffer ))! =-1) {OS. write (buffer, 0, a);} // system. out. println ("cfile. getname () "+ cfile. getname (); is. close (); OS. flush (); OS. close (); bundle = new bundle (); message = message. obtain (); message. what = 3; bundle. putstring ("MSG", cfile. getabsolutepath (); message. setdata (bundle); handler. sendmessage (Message) ;}} catch (exception e) {// todo auto-generated catch blocke. printstacktrace ();}};}. start () ;}}) ;}private Void installapk (string filename) {file = new file (filename); If (! File. exists () {return;} intent = new intent (); intent. setflags (intent. flag_activity_new_task); intent. setaction (intent. action_view); intent. setdataandtype (URI. parse ("file: //" + file. tostring (), "application/vnd. android. package-Archive "); startactivity (intent);} handler = new handler () {public void handlemessage (Android. OS. message MSG) {Switch (MSG. what) {Case 1: Toast. maketext (getapplicationcontext (), "Network disconnected", toast. length_short ). show (); break; Case 2: Toast. maketext (getapplicationcontext (), "No SD card", toast. length_short ). show (); break; Case 3: bundle = MSG. getdata (); string filename = bundle. getstring ("MSG"); installapk (filename); break; default: Break ;};};@ overridepublic Boolean oncreateoptionsmenu (menu) {getmenuinflater (). inflate (R. menu. main, menu); Return true ;}< 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"> <button Android: Id = "@ + ID/button1" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: layout_alignparentleft = "true" Android: feature = "true" Android: layout_marginleft = "43dp" Android: layout_margintop = "19dp" Android: text = "button"/> </relativelayout>