We can see that many Android applications have the automatic update function. You can update the software with one click. Thanks to the software package management and installation mechanism of the Android system, this function is quite simple to implement. Let's take a look at it. First, the interface effect is provided:
1. Preparation knowledge
The version ID of each Android apk is defined in AndroidManifest. xml:
<Manifest xmlns: android = "http://schemas.android.com/apk/res/android" <br/> package = "com. myapp "<br/> android: versionCode =" 1 "<br/> android: versionName = "1.0.0"> <br/> <application> </application> <br/> </manifest>
The android: versionCode and android: versionName fields represent the version code and version name respectively. VersionCode is an integer and versionName is a string. Because the version is for users, it is not easy to compare the size. During the upgrade check, you can check versionCode to compare the size before and after the publication.
So, how do I read versionCode and versionName in AndroidManifest. xml? You can use the PackageManager API by referring to the following code:
Public static int getvercode (context) {<br/> int vercode =-1; <br/> try {<br/> vercode = context. getpackagemanager (). getpackageinfo (<br/> "com. myApp ", 0 ). versioncode; <br/>}catch (namenotfoundexception e) {<br/> log. E (TAG, E. getmessage (); <br/>}< br/> return vercode; <br/>}</P> <p> Public static string getvername (context) {<br/> string vername = ""; <br/> try {<br/> vername = context. getpackagemanager (). getpackageinfo (<br/> "com. myApp ", 0 ). versionname; <br/>}catch (namenotfoundexception e) {<br/> log. E (TAG, E. getmessage (); <br/>}< br/> return vername; <br/>}
You can also write android: versionName = "1.2.0" in AndroidManifest as android: versionName = "@ string/app_versionName", and then in values/strings. add the corresponding string to the xml file. After implementation, you can use the following code to obtain the version name:
Public static string getvername (context) {<br/> string vername = context. getresources () <br/>. gettext (R. string. app_versionname ). tostring (); <br/> return vername; <br/>}
Similarly, the apk Application name can be obtained as follows:
Public static string getappname (context) {<br/> string vername = context. getresources () <br/>. gettext (R. string. app_name ). tostring (); <br/> return vername; <br/>}
2. process framework
3. Version Check
Place the latest apk file on the server, such as http: // localhost/myapp/myapp.apk.
At the same time, place the corresponding apk version information on the server to call the interface or file, for example, http: // localhost/myapp/ver. json
The content in ver. json is:
[{"Appname": "jtapp12", "apkname": "jtapp-12-updateapksamples.apk", "vername": 1.0.1, "vercode": 2}]
Then, read and check the version on the mobile client:
Private boolean getServerVer () {<br/> try {<br/> String verjson = NetworkTool. getContent (Config. UPDATE_SERVER <br/> + Config. UPDATE_VERJSON); <br/> JSONArray array = new JSONArray (verjson); <br/> if (array. length ()> 0) {<br/> JSONObject obj = array. getJSONObject (0); <br/> try {<br/> newVerCode = Integer. parseInt (obj. getString ("verCode"); <br/> newVerName = obj. getString ("verName"); <br/>}catch (Exception e) {<br/> newVerCode =-1; <br/> newVerName = ""; <br/> return false; <br/>}< br/>} catch (Exception e) {<br/> Log. e (TAG, e. getMessage (); <br/> return false; <br/>}< br/> return true; <br/>}
Compare the server and client versions and update them.
If (getserververcode () {<br/> int vercode = config. getvercode (this); // use the method written in the previous section <br/> If (newvercode> vercode) {<br/> donewversionupdate (); // update the new version <br/>}else {<br/> notnewversionshow (); // The latest version is displayed. <br/>}< br/>}
Detailed method:
Private void notNewVersionShow () {<br/> int verCode = Config. getVerCode (this); <br/> String verName = Config. getVerName (this); <br/> StringBuffer sb = new StringBuffer (); <br/> sb. append ("current version:"); <br/> sb. append (verName); <br/> sb. append ("Code:"); <br/> sb. append (verCode); <br/> sb. append (",/n is the latest version. No updates are required! "); <Br/> Dialog dialog = new AlertDialog. builder (Update. this ). setTitle ("Software Update") <br/>. setMessage (sb. toString () // set content <br/>. setPositiveButton ("OK", // set the OK button <br/> new DialogInterface. onClickListener () {<br/> @ Override <br/> public void onClick (DialogInterface dialog, <br/> int which) {<br/> finish (); <br/>}< br/> }). create (); // create <br/> // display dialog box <br/> dialog. show (); <br/>}< br/> private void doNewVersionU Pdate () {<br/> int verCode = Config. getVerCode (this); <br/> String verName = Config. getVerName (this); <br/> StringBuffer sb = new StringBuffer (); <br/> sb. append ("current version:"); <br/> sb. append (verName); <br/> sb. append ("Code:"); <br/> sb. append (verCode); <br/> sb. append (", new version found:"); <br/> sb. append (newVerName); <br/> sb. append ("Code:"); <br/> sb. append (newVerCode); <br/> sb. append (", update? "); <Br/> Dialog dialog = new AlertDialog. builder (Update. this) <br/>. setTitle ("Software Update") <br/>. setMessage (sb. toString () <br/> // set content <br/>. setPositiveButton ("Update", // set the OK button <br/> new DialogInterface. onClickListener () {<br/> @ Override <br/> public void onClick (DialogInterface dialog, <br/> int which) {<br/> pBar = new ProgressDialog (Update. this); <br/> pBar. setTitle ("downloading"); <br/> pBar. setMessage ("Please wait... "); <br/> pBar. setProgressStyle (ProgressDialog. STYLE_SPINNER); <br/> downFile (Config. UPDATE_SERVER + Config. UPDATE_APKNAME); <br/>}< br/>}) <br/>. setNegativeButton ("not updated now", <br/> new DialogInterface. onClickListener () {<br/> public void onClick (DialogInterface dialog, <br/> int whichButton) {<br/> // click the "cancel" button to exit the Program <br/> finish (); <br/>}< br/> }). create (); // create <br/> // display dialog box <br/> dialog. show (); <br/>}
4. Download Module
Note, this part refer to the previous implementation, see http://apps.hi.baidu.com/share/detail/24172508
Void downFile (final String url) {<br/> pBar. show (); <br/> new Thread () {<br/> public void run () {<br/> HttpClient client = new DefaultHttpClient (); <br/> HttpGet get = new HttpGet (url); <br/> HttpResponse response; <br/> try {<br/> response = client.exe cute (get ); <br/> HttpEntity entity = response. getEntity (); <br/> long length = entity. getContentLength (); <br/> InputStream is = entity. getConte Nt (); <br/> FileOutputStream fileOutputStream = null; <br/> if (is! = Null) {<br/> File file = new File (<br/> Environment. getExternalStorageDirectory (), <br/> Config. UPDATE_SAVENAME); <br/> fileOutputStream = new FileOutputStream (file); <br/> byte [] buf = new byte [1024]; <br/> int ch =-1; <br/> int count = 0; <br/> while (ch = is. read (buf ))! =-1) {<br/> fileOutputStream. write (buf, 0, ch); <br/> count + = ch; <br/> if (length> 0) {<br/>}< br/> fileOutputStream. flush (); <br/> if (fileOutputStream! = Null) {<br/> fileOutputStream. close (); <br/>}< br/> down (); <br/>} catch (ClientProtocolException e) {<br/> e. printStackTrace (); <br/>}catch (IOException e) {<br/> e. printStackTrace (); <br/>}< br/> }. start (); <br/>}
After the download is completed, handler notifies the main ui thread to cancel the download dialog box.
Void down () {<br/> handler. post (new Runnable () {<br/> public void run () {<br/> pBar. cancel (); <br/> update (); <br/>}< br/>}); <br/>}
5. Install the application
Void update () {<br/> Intent intent = new Intent (Intent. ACTION_VIEW); <br/> intent. setDataAndType (Uri. fromFile (new File (Environment <br/>. getExternalStorageDirectory (), Config. UPDATE_SAVENAME), <br/> "application/vnd. android. package-archive "); <br/> startActivity (intent); <br/>}
If you publish an apk application to the market, you will find that a similar module is built in the market, which can automatically update or remind you whether to update the application. If your application needs to be updated automatically, is it more convenient to create one on your own? Most of the Code mentioned in this article is implemented in UpdateActivity. java. To make the update process more friendly, you can create a thread in the initial launcher Activity to check whether the server has updates. When there is an update, start UpdateActivity, which makes the user experience smoother.
View/download the source code of this routine:
Http://code.google.com/p/androidex/source/browse/trunk/jtapp-12-updateapksamples
The copyright belongs to the individual. For more information, see the source.
Http://blog.csdn.net/xjanker2/archive/2011/04/06/6303937.aspx