Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize automatic update in Android

2025-04-11 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report