The main principles are:
When the app launches, go to the server version number, compared to the current application if there is an update to download
Here is the version information for the current app
private void Getcurversion () { try { PackageInfo pInfo = Context.getpackagemanager (). Getpackageinfo ( Context.getpackagename (), 0); Curversion = Pinfo.versionname; Curversioncode = Pinfo.versioncode; } catch (Namenotfoundexception e) { log.e ("Update", E.getmessage ()); Curversion = "1.0.1"; Curversioncode = 1; } }
The following is a comparison of Get version information through the Java NET Package
The server-side format is as follows version_1.0.2
HttpURLConnection gets the input stream, and then uses the
BufferedReader Buffer stream, readline into string, then compare
Private Boolean check_update () {String getstring = null; String version=null;getcurversion (); try {URL myurl=new url (app_check); HttpURLConnection urlconnection= (httpurlconnection) myurl.openconnection (); Urlconnection.setreadtimeout (50000); Urlconnection.setconnecttimeout (50000); Urlconnection.connect (); InputStream In=urlconnection.getinputstream (); BufferedReader Buffread; Buffread=new BufferedReader (New InputStreamReader (in, "Utf-8")); String Line;line=buffread.readline (); while (Line!=null) {getstring+=line;line=buffread.readline ();} int Index=getstring.indexof ("Version_");//2.0.1version=getstring.substring (index+8, index+13); In.close (); LOG.E ("version", version);} catch (Malformedurlexception e) {//Todo auto-generated catch Blocke.printstacktrace ();} catch (IOException e) {//Todo Au To-generated catch Blocke.printstacktrace ();} if (version!=null) {if (Version.compareto (curversion) >0) return True;elsereturn false; } else return false;}
The next step is to pop up a dialog box and invoke the download thread
private void Showdowndialog () {alertdialog.builder dialog = new Alertdialog.builder (context); Dialog.settitle ("software version update"); Dialog.setmessage ("With the latest app updates"); Dialog.setnegativebutton ("Later", New Onclicklistener () {@Overridepublic void OnClick (dialoginterface arg0, int arg1) {/ /TODO auto-generated method Stubarg0.dismiss ();} }); Dialog.setpositivebutton ("OK", new Onclicklistener () {@Overridepublic void OnClick (dialoginterface dialog, int which) { TODO auto-generated method stub//determines the download thread inside the call, while displaying the download progress dialog Dialog.dismiss (); cancel=true;downapk (); showdownapk ();} } ); Dialog.show ();}
The last one is to issue a intent broadcast
private void Setinstall () {
file Apkfile = new File (apk_path);
Span style= "White-space:pre" > if (!apkfile.exists ()) {
return; &NBSP
}
intent i = new Intent (Intent.action_view); &NBSP
i.setdataandtype (Uri.parse ("file://" + Apkfile.tostring ()), "application/vnd.android.package-archive");  &NBSP
context.startactivity (i);
}