Android implementation notification bar download update app example

1. Design idea: use versioncode as the version upgrade parameter. Android provides us with two attributes for defining the version:

2. In order to provide practical guidance for real projects or enterprise application, I simulate an independent project. The project directory setting should be reasonable and rigorous, rather than just a demo. Suppose we take Shanghai Metro as the project, named "subway", and the engineering structure is as follows,

3. Comparison of version initialization and version number. First, define the variables localversion and serverversion in the global file global.java to store the local version number and server version number respectively.

OK, let's string these things now: the first step is to execute initglobal() in the oncreate() method of subwayapplication to initialize the version variable.

Now the portal has been opened. It can be seen from the code in line 18 of the checkversion method that when the user clicks update, we start the update service and download the latest version from the server. 4. Use the service to download from the server in the background. After that, the user will be prompted to complete the download and close the service. Define a service updateservice.java. First, define variables related to download and notification:

Prepare relevant download work in onstartcommand() method:

It can be seen from the code that the updaterunnable class is the real downloaded class. For the sake of user experience, this class is executed in the background by a single thread. The download process has two tasks: 1. Download data from the server; 2. Inform the user of the download progress. For thread notification, we first define an empty updatehandler. [/code] private Handler updateHandler = new Handler(){ @Override public void handleMessage(Message msg) {

} }; [/ code] then create the real implementation of updaterunnable class:

After the download is completed, we will prompt the user to complete the download and click Install. Let's complete the previous handler. First, define two constants in updateservice.java to represent the download status:

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>