Automatic update and upgrade of Android applications (self-upgrade, through Tomcat)

Source: Internet
Author: User

I have just started Android for more than a month. I wrote this demo test because the company needs to submit technical documents. I want to save it for later use! If anything is wrong, please let us know. This example is based on others' demos on the Internet.

Upgrade of Android applications (self-Upgrade)

I. Introduction:

Many Android applications have Version Detection and automatic update functions. You can upgrade and update the software with one click. The upgrade of Android applications uses the software package management and installation mechanism of the Linux system, which is easy for the development of the Upper-layer function, you only need to use the android API.

Ii. Function Description:

1. This example is used to upgrade a single application.

2. When the program starts, connect to the Tomcat 7 Web server for version check. If a new version is available, an update prompt is displayed.

3. Place the new APK file downloaded from the Web server to the sdcard

4. Check whether the new APK application is installed. If yes, delete the downloaded APK file from sdcard.

Iii. Procedure Framework process:


Iv. Environment Description:

1,Server: Ubuntu tomcat7web server. After installation, the default port is 8080. when accessing the android simulator, place the APK file in the/var/lib/tomcat7/webapps/root/directory, the access method of the android simulator is http: // 10.0.2.2/newdeskample.apk.

2,Android simulator development environment:

Ubuntu + eclipse + ADT

5. Detailed procedures and key points:

(1) preparations for the new application (new‑ample:

A) create an android project and edit the version code to 2. The version name is 1.0.1, which is later than the old version.

B) edit the version information file version. JSON corresponding to the application.

(JSON files are a lightweight data exchange format, which is much faster than XML files. It is suitable for small-sized Data Network exchanges and is similar to key-value pairs in essence, the key is expressed as a string separated by a colon and can store multiple data types.

(2) Application preparation for the old version:

1. In its androidmanifest. the version code defined in XML is versioncode = "1" for automatic generation. We mainly use the version code of the program to determine whether a new version is available for update.

2. When an application is started, the system automatically checks whether a new version is available, that is, the oncreate () function performs a networking check.

A) Get the version information file version. JSON from the server. We have written a class to implement it separately and use its getupdateinfo static method to return the Read version. JSON. The return form is a string. The Code is as follows:

B) obtain the current old application version information. We encapsulate a class CurrentVersion separately and use the static method to obtain the current application version information, including the program name version, code version, and application name.

The Code is as follows:

C) parse the string obtained from the server version. JSON to obtain the required version information.

D) Compare the code version and check whether the current application is updated.

(3) display the update prompt box

 


(4) download the new APK File


When the download is complete, cancel the progress bar dialog box and prompt whether to install the new application.

(5) install new applications:


One Parameter of setdataandtype of intent is the absolute path of the application (in sdcard), the second parameter is the MIME type corresponding to the file, and the APK file in Android system is application/vnd by default. android. package-archive. The MIME type of this file corresponds to the/var/lib/atat7/CONF file on the Tomcat server.

(6) Delete the network detection code and the APK file in sdcard

 

Key Note: If the downloaded APK file is deleted directly in the installed code without the need for broadcasting and receiving, the APK file will be deleted before the installation is complete. The system prompts you to install the new APK file step by step, so we cannot determine when the application is fully installed. We use a listener (installed or replaced by an application) to implement broadcast. When the application receives a broadcast with an added or a broadcast with a replaced, we will delete the APK file.

Vi. Demo example:

1. Update prompt

2. download the new version of the application

3. Prompt whether to install

4. Enter the system installation prompt

5. Installing

6. Installation Complete

7.Open a new application

7. Problems and key points during the completion process:

1. When the android simulator connects to the Tomcat 7 server for download, the access address IP address cannot use localhost. Because the android simulator regards localhost as its own, it should be tested using 10.0.2.2.

2. The downloaded APK file and the JSON file of the version information should be stored in the/var/lib/tomcat7/webapps/root/directory or cannot be accessed.

3. For details about how to parse JSON files, refer to the JSON appendix.

4. Permissions involved in the example:

A) permissions related to sdcard: In this example, we need the permission to create and delete files in sdcard and the read and write permissions of sdcard.

B) network-related permissions: In this example, we need to access the network and obtain the network status permissions (test whether the Network is available). In this example, we only test whether the Network is available, we can also further determine whether the network has been connected.

5. Check whether the application is installed successfully.

In the manifest. add the broadcast action to be accepted to the XML file. Here we listen to the replacement of the application itself and the addition of two actions to the application in the system. It seems that the replacement listening of the application can only listen to itself being replaced, this is to be investigated.

Source code: Download the source code in this article. 8. JSON attachment: JSON definition:

A lightweight data exchange format with good readability and ease of writing. Mainstream technologies in the industry provide a complete solution (similar to regular expressions and supported by most languages) to exchange data between different platforms. JSON adopts a highly compatible text format and has behaviors similar to the C language system.

Why JSON?

It's easy because it is ten times faster than XML.

What are the application cases?

Open APIs for Twitter, Douban, Facebook, and other companies. Generally, these services provide multiple formats for developers to choose from (XML, JSON, Atom, etc.). On mobile terminals, we naturally want to give users the best experience, so I chose the most efficient JSON format.

JSON structure:

Name/valuepairs is similar to the well-known keyedlist, hash table, disctionary, and associative array. There is another class "bundle" in the Android platform, which has similar behavior to some extent.

Org. JSON. jsonobject array, a group of ordered data lists.

JSON-related classes (4) and exceptions (1) in Android ):

LJsonarray

LJsonobject

LJsonstringer

LJsontokener

LJsonexception

 

Jsonobject:

This is the basic unit related to the json definition in the system. It contains a pair of key/value values. Its response to an external call (External: Applying the value output by the tostring () method) is a standard string (for example, {"JSON": "Hello, world "}, the outmost is enclosed by braces, and the key and value are separated by the colon ). The operation format for internal (internal) behaviors is slightly. For example, to initialize a jsonobject instance, add the value newjsonobject () by referencing the internal put () method (). put ("JSON", "Hello, world! "), Separated by commas (,) between keys and values.

Value types include: Boolean, jsonarray, jsonobject, number, string, or the default value jsonobject. null object.

There are two different value methods:

Get (): used when a specified value exists. Otherwise, an exception is thrown when the key cannot be retrieved.

Opt (): This method is relatively flexible. If the specified value cannot be obtained, a default value is returned and no exception is thrown.

Jsonarray:

It represents a group of ordered values. The format of tostring output is enclosed in square brackets. Values are separated by commas (,) ([value1, value2, value3], you can use the short code to learn more about the format ). This class has the same internal query behavior. The get () and OPT () methods can both return the specified value through the index, and the put () method is used to add or replace the value.

The Value Type of this class can also include: Boolean, jsonarray, jsonobject, number, string, or the default value jsonobject. null object.

Jsonstringer:

According to the official explanation, this class can help you quickly and conveniently create JSON text. Its biggest advantage is that it can reduce program exceptions caused by format errors. referencing this class can automatically create JSON text in strict accordance with the JSON syntax rules (syntaxrules. Each jsonstringer object can only create one JSON text.

Learn more about the following instances:

String mystring = new jsonstringer (). Object ()

. Key ("Ar"). Value ("www.androidres.com! ")

. Endobject ()

. Tostring ();

The result is a set of standard format JSON text: {"ar": "www.androidres.com !"}

The. Object () and. endobject () must be used at the same time to add a boundary to the value according to the object standard. Likewise, there are a set of standard methods for arrays to generate the boundary. Array () and. endarray ().

Jsontokener:

This is the class in which the system parses the JSON source string for the jsonobject and jsonarray constructors. It can extract numerical information from the source string. 

Jsonexception:

Is the exception information thrown by the json.org class.

Related Article

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.