Original: http://blog.csdn.net/jasper_success/article/details/7984065
First step: Use Java.net's URLConnection object to create a connection
Step Two: Write the downloaded file to the memory card cache via InputStream
Step three: After the download is complete, open the file by customizing the OpenFile () method, determine the file type, and, if the APK, start the installation
Fourth step: Ready to leave the installer program, through the self-made Delfile () method, delete the files in the cache
/*** Remote Download installs Android program * * @ClassName installonlineactivity *@authorJet * @date 2012-9-14*/ Public classInstallonlineactivityextendsActivity {PrivateTextView Mtextview; PrivateEditText Medittext; PrivateButton Mbutton; PrivateString currentFilePath = ""; PrivateString Currenttempfilepath = ""; PrivateString strURL = ""; PrivateString Fileex = ""; PrivateString fileName = ""; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.installonline); Mtextview=(TextView) Findviewbyid (R.ID.INSTALLONLINE_TEXT1); Medittext=(EditText) Findviewbyid (R.ID.INSTALLONLINE_EDITTEXT1); Mbutton=(Button) Findviewbyid (R.id.installonline_button1); Mbutton.setonclicklistener (NewOnclicklistener () {@Override Public voidOnClick (View v) {//download the file to a localMtextview.settext ("Download ..."); strURL=Medittext.gettext (). toString (); //intercept file suffixFileex = strurl.substring (Strurl.lastindexof ('. ') + 1, Strurl.length ()). toLowerCase (); //intercepting file namesFileName = strurl.substring (Strurl.lastindexof ('/') + 1, Strurl.lastindexof (‘.‘)); GetFile (strURL); } }); } Private voidGetFile (FinalString strpath) { if(Currentfilepath.equals (strpath)) {Getdatasource (strpath); } currentFilePath=strpath; Runnable R=NewRunnable () {@Override Public voidrun () {Getdatasource (strpath); } }; NewThread (R). Start (); } Private voidgetdatasource (String url) {if(!urlutil.isnetworkurl (URL)) {Mtextview.settext ("Please fill in the correct URL"); } Else { Try{URL Myurl=Newurl (URL); //Get ConnectedURLConnection conn =myurl.openconnection (); //ConnectionConn.connect (); //Get input streamInputStream is =Conn.getinputstream (); if(IS = =NULL) { Throw NewRuntimeException ("Stream is null"); } //Create a temporary fileFile mytempfile = File.createtempfile (FileName, "." +Fileex); //Get temporary file storage pathCurrenttempfilepath =Mytempfile.getabsolutepath (); FileOutputStream Fos=NewFileOutputStream (Mytempfile); byte[] buf =New byte[128]; Do { //returns the size of the currently read buffer intNumread =Is.read (BUF); if(Numread <= 0) { Break; } fos.write (BUF,0, Numread); } while(true); //open a file for installationOpenFile (Mytempfile); Is.close (); } Catch(malformedurlexception e) {e.printstacktrace (); } Catch(IOException e) {e.printstacktrace (); } } } Private voidopenFile (file file) {Intent Intent=NewIntent (); Intent.addflags (Intent.flag_activity_new_task); Intent.setaction (Android.content.Intent.ACTION_VIEW); String type=getmimetype (file); Intent.setdataandtype (uri.fromfile (file), type); StartActivity (Intent); } Privatestring getmimetype (file file) {String type= ""; String fname=File.getname (); //Get extensionString end =fname. Substring (Fname.lastindexof (‘.‘) + 1, Fname.length ()). toLowerCase (); //depends on the type of extension mimetype if("M4A". Equals (end) | | "MP3". Equals (end) | | "Mid". Equals (end)|| "XMF". Equals (end) | | "Ogg". Equals (end) | | "WAV". Equals (end)) {Type= "Audio"; } Else if("3gp". Equals (end) | | "MP4". Equals (end)) {Type= "Video"; } Else if("JPG". Equals (end) | | "GIF". Equals (end) | | "PNG". Equals (end)|| "JPEG". Equals (end) | | "BMP". Equals (end)) {Type= "image"; } Else if("APK". Equals (end)) {Type= "Application/vnd.android.package-archive"; } Else{type= "*"; } if("APK". Equals (end)) { } Else{type+= "/*"; } returntype; } Private voiddelfile (String fileName) {File file=NewFile (fileName); if(File.exists ()) {file.delete (); }} @Overrideprotected voidOnPause () {Mtextview=(TextView) Findviewbyid (R.ID.INSTALLONLINE_TEXT1); Mtextview.settext ("Download Succeeded"); Super. OnPause (); } @Overrideprotected voidOnresume () {//Delete temporary filesDelfile (Currenttempfilepath); Super. Onresume (); }}
Android Online update Remote installation program