Implementation of Automatic app update for android

Source: Internet
Author: User

If all applications have version updates, how can they be updated? Today, we will introduce the entire process of automatic application updates.

The procedure for automatically updating a program is roughly as follows:

Program startup --> timely background check for updates ---> link remote server --> get new version information

--> Compare the current version --> if (updated) --> display the update prompt dialog box and display the updated content --> submit and select.


Below is my demo. You can refer to it:

The layout is relatively simple, so no code is available.

Main program code:

Package com. cloay. update; import java. io. IOException; import java.net. httpURLConnection; import java.net. malformedURLException; import java.net. URL; import android. app. activity; import android. app. alertDialog; import android. content. dialogInterface; import android. content. pm. packageManager. nameNotFoundException; import android. OS. bundle; import android. view. view; import android. view. view. onClickListener; impor T android. widget. button; import android. widget. toast;/*** automatically updates * UpdateTestActivity. java * @ author Cloay * 2011-11-23 */public class UpdateTestActivity extends Activity {private Button button; @ Override public void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. main); button = (Button) findViewById (R. id. check); button. setOnClickListener (new OnCl IckListener () {@ Overridepublic void onClick (View v) {try {checkVersion ();} catch (NameNotFoundException e) {// TODO Auto-generated catch blocke. printStackTrace () ;}});}/*** check whether the program needs to be updated * @ throws NameNotFoundException */private void checkVersion () throws NameNotFoundException {UpdateInfo updateInfo = new UpdateInfo (); URL url; try {url = new URL ("http: // localhost: 8080/update. xml "); HttpURLConnecti On connection = (HttpURLConnection) url. openConnection (); // connection. setConnectTimeout (5000); updateInfo = ParseXmlUtils. parseXml (connection. getInputStream ();} catch (MalformedURLException e) {e. printStackTrace ();} catch (IOException e) {e. printStackTrace ();} if (updateInfo. getVersion (). equals (VersionUtil. getVersionName (this) {Toast. makeText (this, "version is the same, no upgrade is required! ", Toast. LENGTH_SHORT ). show () ;}else {showUpdateDialog (updateInfo) ;}} private void showUpdateDialog (UpdateInfo updateInfo) {AlertDialog alertDialog = new AlertDialog. builder (this ). setTitle ("prompt to detect the new version. Are you sure you want to upgrade? "). SetIcon (R. drawable. ask ). setMessage (updateInfo. getDescription ()). setPositiveButton ("OK", new DialogInterface. onClickListener () {@ Overridepublic void onClick (DialogInterface dialog, int which) {dialog. cancel ();}}). setNegativeButton ("cancel", new DialogInterface. onClickListener () {@ Overridepublic void onClick (DialogInterface dialog, int which) {dialog. cancel ();}}). create (); alertDialog. show ();}}


An xml file is placed on the remote server to describe the information of the latest version. Including version number, new version function description, New Version Download link


Xml parsing tool code:

Package com. cloay. update; import java. io. IOException; import java. io. inputStream; import javax. xml. parsers. documentBuilder; import javax. xml. parsers. documentBuilderFactory; import javax. xml. parsers. parserConfigurationException; import org. w3c. dom. document; import org. w3c. dom. element; import org. w3c. dom. nodeList; import org. xml. sax. SAXException;/*** parse the Xml file * ParseXmlUtils. java * @ author Cloay * 2011-11-7 */public class ParseXmlUtils {/*** parses the file through InputStream * @ param in * @ return */public static UpdateInfo parseXml (InputStream in) {UpdateInfo updateInfo = new UpdateInfo (); DocumentBuilderFactory dbf = DocumentBuilderFactory. newInstance (); DocumentBuilder db = null; try {db = dbf. newDocumentBuilder (); Document doc = null; doc = db. parse (in); Element root = doc. getDocumentElement (); NodeList resultNode = root. getElementsByTagName ("info"); for (int I = 0; I <resultNode. getLength (); I ++) {Element res = (Element) resultNode. item (I); updateInfo. setVersion (res. getElementsByTagName ("version "). item (0 ). getFirstChild (). getNodeValue (); updateInfo. setUrl (res. getElementsByTagName ("url "). item (0 ). getFirstChild (). getNodeValue (); updateInfo. setDescription (res. getElementsByTagName ("description "). item (0 ). getFirstChild (). getNodeValue () ;}} catch (ParserConfigurationException e) {e. printStackTrace ();} catch (SAXException e) {e. printStackTrace ();} catch (IOException e) {e. printStackTrace ();} return updateInfo ;}}


UpdateInfo object:

Package com. cloay. update;/*** update information entity class * UpdateInfo. java * @ author Cloay * 2011-11-23 */public class UpdateInfo {private String version; // version number private String url; // The new version contains the url path private String description. // update description, for example, public String getVersion () {return version;} public void setVersion (String version) {this. version = version;} public String getUrl () {return url;} public void setUrl (String url) {this. url = url;} public String getDescription () {return description;} public void setDescription (String description) {this. description = description ;}}


Obtain information about the currently installed version:

Package com. cloay. update; import android. content. context; import android. content. pm. packageInfo; import android. content. pm. packageManager; import android. content. pm. packageManager. nameNotFoundException;/*** version tool class * VersionUtil. java * @ author Cloay * 2011-11-23 */public class VersionUtil {/*** get the version number * @ param context * Context * @ return * @ throws NameNotFoundException */public static String getVersionName (context context) throws NameNotFoundException {// obtain PackageManager packageManager = context. getPackageManager (); // obtain the package name of the class to which the context belongs. 0 indicates obtaining the PackageInfo packageInfo = packageManager. getPackageInfo (context. getPackageName (), 0); return packageInfo. versionName ;}}


In fact, the whole process is not very troublesome. The comments must be noted in detail, so I will write so much. If you have any questions, please leave a message for us to share and learn!

Note: Please indicate the source for reprinting!

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.