General steps to apply the upgrade:
Check for updates (Read server config file, version number)
If you find a high version read the update file Updateinfo.xml get information about downloading updates
After verifying the information to confirm the upgrade, download apk
After downloading the APK, do MD5 check the APK integrity
Install APK
Upgrade Portal
Private void upgrade () {//need access to the network to avoid clogging the main thread new thread () {Public void run () {if ( Checkupdate ()) {//Check for updates handler.sendemptymessage (20);//notification interface prompting for version update}};}. Start ();} Private boolean checkupdate () {string url = path_server + "Upgrade/config"; /Read version information from config file, and Updateinfo.xml file address Try {updateinfomap = parseupdatefile.getconfiginfo (URL );} catch (exception e) {e.printstacktrace ();} Gets the version number of the current APK packageinfo packageinfo = null;try {packageinfo = MainActivity.this.getPackageManager (). Getpackageinfo (MainActivity.this.getPackageName (), 0);} catch (exception e) {e.printstacktrace ();} Int updatevcode = integer.valueof (Updateinfomap.get ("version"));//server-side APK version is higher than the current version, Read the Updateinfo.xml file if (updatevcode > packageinfo.versioncode) {url = path_server+ "upgrade /updateinfo.xml "; Try {updateinfomap.putall (Parsexmlutil.parsEXML (URL));} catch (exception e) {e.printstacktrace ();} Output read result Set<string> set = updateinfomap.keyset (); System.out.println ("Map.size ():" +updateinfomap.size ());for (iterator<string> iterator = set.iterator (); iterator.hasnext ();) {String string = (String) Iterator.next (); System.out.println (string + "-To" + updateinfomap.get (string));} Check the legality of the information by sending the updatable message return checkupdateinfo (UPDATEINFOMAP);} Return false;}
Parse config file
Public static map<string,string> getconfiginfo (String strurl) throws Exception {map<string,string> configmap = new hashmap<string, string > (); Url url = new url (strURL); Urlconnection conn = url.openconnection ();if (conn == null) {return Configmap;} Inputstream inputstream = conn.getinputstream (); inputstreamreader inputstreamreader = new inputstreamreader (InputStream); Bufferedreader bufferedreader = new bufferedreader (InputStreamReader); string str = null;while (null != (Str=bufferedreader.readline ())) {if (str != null) {if (Str.contains ("version=")) {configmap.put ("Version", Str.substring (str.indexof ("=") +1));} if (Str.contains ("Versionserver")) {configmap.put ("Versionserver", str.substring (Str.indexOf (": : ") +2));}}} BuFferedreader.close (); return configmap;}
Checkupdateinfo () The legality of the main verification information
Private Boolean Checkupdateinfo (map<string, string> updateinfomap) {String Downloadpath = Updateinfomap.get (" Downloadpath "); String PackageName = Updateinfomap.get ("PackageName"); String Versioncode = Updateinfomap.get ("Versioncode"); String Updatevcode = Updateinfomap.get ("Version"); if (Checkurl (Downloadpath)//detection is accessible && versioncode.equals ( Updatevcode) The version number in//config and updateinfoxml files is consistent && packagename.equals (Getpackagename ())) {//package name return true;} return false;}
Permission required to download files to device
<uses-permission android:name= "android.permission.WRITE_EXTERNAL_ STORAGE " /><uses-permission android:name=" Android.permission.MOUNT_UNMOUNT_FILESYSTEMS "/>
private void downloadapk () {new thread () {public void run () {String Downloadpath = updateinfomap.get ("Downloadpath"); string downloaddir = "/acfg/"; File filedir = new file (Downloaddir);if (!filedir.exists ()) {fileDir.mkdir (); String filename = downloaddir + downloadpath.substring (DownLoadPath.lastIndexOf ("/") + 1); File file = new file (FileName);if (file.exists ()) {file.delete (); Try {file.createnewfile ();// construction Urlurl url = new url (downLoadPath);// Open Connection Urlconnection con = url.openconnection ();// to get the length of the file int contentlength = Con.getcontentlength (); System.out.println ("Length :" + contentlength);// input stream inputstream is = Con.getinputstream ();// 1k data buffer byte[] bs = new byte[1024];// The length of data read int len;// output of File stream outputstReam os = new fileoutputstream (FileName);// begins reading while (Len = is.read ( BS)) != -1) {os.write (Bs, 0, len);} Complete, close all links os.close (); Is.close (); Updateinfomap.put ("FileName", filename);} catch (exception e) {e.printstacktrace (); Handler.sendemptymessage (22);} Handler.sendemptymessage (21);//Notification interface download complete};}. Start ();}
Check the apk MD5 value after download is complete
File File = new file (fileName); String fileMD5 = md5util.getmd5offile (file), if (Filemd5.equals (Activity.updateInfoMap.get ("md5sum")) { Toast.maketext (activity, "Download finished. It's ready to update! ", Toast.length_long). Show (); Activity.update (fileName);}
Gets the MD5 value of the file
public static string Getmd5offile (file file) {string value = null; FileInputStream in = null;try {in = new FileInputStream (file); Mappedbytebuffer Bytebuffer = In.getchannel (). Map (FileChannel.MapMode.READ_ONLY, 0, File.length ()); MessageDigest MD5 = messagedigest.getinstance ("MD5"); Md5.update (Bytebuffer); BigInteger bi = new BigInteger (1, Md5.digest ()); value = bi.tostring (+). toUpperCase (locale.english);} catch (Exception e) {e.printstacktrace ();} finally {if (null! = in) {try {in.close ();} catch (IOException e) {E.printstack Trace ();}}} return value;}
Install the download good apk
private void Update (String filePath) {Intent Intent = new Intent (Intent.action_view); Intent.setdataandtype (Uri.fromfile (New File (FilePath)), "application/vnd.android.package-archive"); StartActivity (intent);}
Enter the application after installation is complete
Android app upgrade, detect updates, download, test, install