Prompt the user to update the version and release a notification to monitor version downloads

Source: Internet
Author: User

Preface:

After we release our app, we will not be able to upgrade and add some new features. Generally, after entering the software, we will detect and release notifications for download. Of course, you should also use the same key to pack the updates. Then optimize it. Now let's look at its implementation method.

 

Effect:



 

First, check the processing of mainactivity in the previous Code:

 

Import COM. jay. verioncheck. versionconfig; import COM. jay. versionservice. versionservice; import android. OS. bundle; import android. app. activity; import android. app. alertdialog; import android. content. dialoginterface; import android. content. intent; import android. content. PM. packagemanager. namenotfoundexception; public class mainactivity extends activity {@ overrideprotected void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. activity_main); initversionglobal (); // initialize the local version and server version checkversion (); // check the version and release notification update} // initialize the version information, assign public void initversionglobal () {try {// obtain the local software version versionconfig. localversion = This. getpackagemanager (). getpackageinfo (this. getpackagename (), 0 ). versioncode; // obtain the server version number versionconfig. serverversion = 2;} catch (namenotfoundexception e) {e. printstacktrace () ;}/// check for version updates public void checkversion () {// compare local and server versions if (versionconfig. localversion <versionconfig. serverversion) {// when an update occurs, the alertdialog dialog box is displayed. builder Alert = new alertdialog. builder (this); alert. settitle ("Upgrade "). setmessage ("new versions are found. We recommend that you update them now "). setpositivebutton ("Update", new dialoginterface. onclicklistener () {@ overridepublic void onclick (dialoginterface dialog, int which) {// send a request to the download service intent = new intent (); intent. setclass (mainactivity. this, versionservice. class); intent. putextra ("titleid", R. string. app_name); startservice (intent );}}). setnegativebutton ("cancel", new dialoginterface. onclicklistener () {@ overridepublic void onclick (dialoginterface dialog, int which) {// do some cleanup work }}). seticon (R. drawable. ic_launcher); // The title iconalert. create (). show (); // display this dialog box} else {// No updates }}}

 

 

Version Control class:

/***** Local version control class * @ author zhanglei **/public class versionconfig {public static int localversion = 1; public static int serverversion = 2; public static string downloaddir = "/downloaddir /";}

 

Versionservice download service code:

 

Import Java. io. file; import COM. jay. verioncheck. versionconfig; import COM. jay. versionactivity. mainactivity; import COM. jay. versionactivity. r; import android. app. activity; import android. app. notification; import android. app. icationicationmanager; import android. app. pendingintent; import android. app. service; import android. content. intent; import android. OS. environment; import android. OS. ibinder;/***** defines a download service * @ author zhanglei **/public class versionservice extends Service {private int titleid; private file updatedir = NULL; private file updatefile = NULL; // notification bar private icationicationmanager updatenotificationmanager = NULL; private notification updatenotification = NULL; // The notification bar jumps to intentprivate intent updateintent = NULL; private pendingintent identifier = NULL; @ overridepublic ibinder onbind (intent) {return NULL ;}@ overridepublic void oncreate () {super. oncreate () ;}@ overridepublic void onstart (intent, int startid) {titleid = intent. getintextra ("titleid", 0);/***** 1. Android. OS. environment. media_mounted indicates the installation status * 2. Android. OS. environment. getexternalstoragestate () obtains the current software status */If (Android. OS. environment. media_mounted.equals (Android. OS. environment. getexternalstoragestate () {// configure the directory + download directory configured through the current running environment to the path of the excessive file updatedir = new file (environment. getexternalstoragedirectory (), versionconfig. downloaddir); updatefile = new file (updatedir. getpath (), getresources (). getresourcename (titleid);} // get the notification manager and assign a value to the notification object this. updatenotificationmanager = (icationicationmanager) This. getsystemservice (icationication_service); this. updatenotification = new notification (); // set the download process, click the notification bar, and return to the main interface updateintent = new intent (this, mainactivity. class); updatependingintent = pendingintent. getactivity (this, 0, updateintent, 0); // configure the notification content updatenotification. tickertext = "Start download"; updatenotification. icon = R. drawable. ic_launcher; updatenotification. setlatesteventinfo (this, "Download test", "0%", updatependingintent); // publish the updatenotificationmanager notification. Y (0, updatenotification); super. onstart (intent, startid );}}

Register and download service:

 

 

<! -- Manually register a service --> <service android: Name = "com. Jay. versionservice. versionservice" Android: enabled = "true"> </service>

 

 

Note:

Currently, this blog only supports publishing notifications. Because there is no server address, and no real-time update download monitoring is performed.

 

Implementation ideas:

The code is highly readable and has a lot of comments. I will not explain it here. This implementation is after the app is running and after the activity. In the oncreate method, compare the local version with the server version. To check whether any updates are available. If yes, the prompt dialog box is displayed. If the user updates, the release notification is updated.

Source code:

Http://pan.baidu.com/share/link? Consumer id = 456005 & UK = 1997312776

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.