Get the App
SLTechnology News&Howtos  ›  Development  › 

How to realize automatic update in Android

Shulou Source: shulou.com Published: 2022-05-31 22:02:06 10月22日 Update

This article introduces the relevant knowledge of "how to achieve automatic update in Android". Many people will encounter this dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

The code is as follows:

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.AlertDialog; 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 (Bundle savedInstanceState) {super.onCreate (savedInstanceState); setContentView (R.layout.update) Dialog dialog = new AlertDialog.Builder (Update.this) .setTitle ("system update") .setMessage ("New version found, please update!") / / set content .setPositiveButton ("OK", / / set 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 () {public void onClick (DialogInterface dialog) Int whichButton) {/ / exit the program} after clicking the cancel button) .create () / / create / / display dialog box dialog.show ();} void downFile (final String url) {pBar.show (); new Thread () {public void run () {HttpClient client = new DefaultHttpClient () / / params [0] represents the connected url HttpGet get = new HttpGet (url); HttpResponse response; try {response = client.execute (get); HttpEntity entity = response.getEntity (); 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);}} "how to update automatically in Android" ends here. Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Tags: Updates content buttons more knowledge practicality learning next code representation dilemma reality dialog box situation article case being program system ribbon Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MariaDB Shulou Tech Info macOS Shulou Information Apple