Android automatically updates code

Source: Internet
Author: User

Import java. io. file; import java. io. fileOutputStream; import java. io. IOException; import java. io. inputStream; import org. apache. http. httpEntity; import org. apache. http. httpResponse; import org. apache. http. client. clientProtocolException; import org. apache. http. client. httpClient; import org. apache. http. client. methods. httpGet; import org. apache. http. impl. client. defaultHttpClient; import android. app. ale RtDialog; import android. app. dialog; import android. app. progressDialog; import android. content. dialogInterface; import android. content. intent; import android.net. uri; import android. OS. bundle; import android. OS. environment; import android. OS. handler; public class Update extends BaseActivity {public ProgressDialog pBar; private Handler handler = new Handler (); @ Override protected void onCreate (Bun Dle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. update); Dialog dialog = new AlertDialog. builder (Update. this ). setTitle ("system update "). setMessage ("new version found. Please update it! ") // Set the content. setPositiveButton ("OK", // set the OK button new DialogInterface. onClickListener () {@ Override public void onClick (DialogInterface dialog, int which) {pBar = new ProgressDialog (Update. this); pBar. setTitle ("downloading"); pBar. setMessage ("Please wait... "); pBar. setProgressStyle (ProgressDialog. STYLE_SPINNER); downFile ("http: // url: 8765/OA.apk ");}}). setNegativeButton ("cancel", new DialogInterface. onClickListener () {p Ublic void onClick (DialogInterface dialog, int whichButton) {// click the "cancel" button to exit the program }}). create (); // create // display the dialog box dialog. show ();} void downFile (final String url) {pBar. show (); new Thread () {public void run () {HttpClient client = new DefaultHttpClient (); // params [0] indicates the connected url HttpGet get = new HttpGet (url); HttpResponse response; try {response = client.exe cute (get); HttpEntity entity = response. getE Ntity (); long length = entity. getContentLength (); InputStream is = entity. getContent (); FileOutputStream fileOutputStream = null; if (is! = Null) {File file = new File (Environment. getExternalStorageDirectory (), "OA.apk"); fileOutputStream = new FileOutputStream (file); byte [] buf = new byte [1024]; int ch =-1; int count = 0; while (ch = is. read (buf ))! =-1) {// baos. write (buf, 0, ch); fileOutputStream. write (buf, 0, ch); count + = ch; if (length> 0) {}} fileOutputStream. flush (); if (fileOutputStream! = Null) {fileOutputStream. close () ;}down () ;}catch (ClientProtocolException e) {// TODO Auto-generated catch block e. printStackTrace ();} catch (IOException e) {// TODO Auto-generated catch block e. printStackTrace ();}}}. start ();} void down () {handler. post (new Runnable () {public void run () {pBar. cancel (); update () ;}}) ;}void update () {Intent intent = new Intent (Intent. ACTION_VIEW); intent. setDataAndType (Uri. fromFile (new File ("/sdcard/OA.apk"), "application/vnd. android. package-archive "); startActivity (intent );}}

 

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.