First get the local APK version:
/**
* Get local software version
*/
public static int getlocalversion (Context ctx) {
int localversion = 0;
try {
PackageInfo PackageInfo = Ctx.getapplicationcontext ()
. Getpackagemanager (). Getpackageinfo (Ctx.getpackagename (), 0);
Localversion = Packageinfo.versioncode;
LOG.D ("TAG", "version of this software: "+localversion);
} catch (Namenotfoundexception e) {
E.printstacktrace ();
}
return localversion;
}
Then get the server version, which can be obtained in many ways on your own play
Get the server version followed by the local apk version to compare:
If the update is less than the server version: (I wrote for reference only, I was asynchronously downloaded, and then notified the way to show progress)
/** * for updating the app version * * @param CTX * Context object * @param URL * Updated version of address */private static pendingintent Pendin Gintent;public static void Updateversion (Final Context ctx, String url) {createnotification (CTX);//create file, read App_ Namecreatefile (Ctx.getresources (). getString (R.string.app_name)); New asynctask<string, Void, string> () {@ overrideprotected string Doinbackground (String ... params) {int down_step = 1;//hint stepint totalsize;//file total size int DOWNLOADC Ount = 0;//has downloaded a good size int updatecount = 0;//has uploaded file size inputstream inputstream;outputstream outputstream; URL url; HttpURLConnection httpurlconnection = null;try {url = new URL (params[0]); httpurlconnection = (httpurlconnection) Url.openconnection (); Httpurlconnection.setconnecttimeout (timeout); Httpurlconnection.setreadtimeout (timeout);// Get the downloaded file Sizetotalsize = httpurlconnection.getcontentlength ();//LOG.D ("TAG", "TotalSize" +totalsize); if ( Httpurlconnection.getresponsecode () = = 404) {throw new Exception ("fail!");} InputStream = HttpurlconNection.getinputstream ();//File Appfile=file.createtempfile ("Medium extension Steel", ". apk"), OutputStream = new FileOutputStream ( Updatefile, false);//file exists overwrite byte buffer[] = new Byte[1024];int readsize = 0;while ((readsize = inputstream.read (buffer)) ! =-1) {outputstream.write (buffer, 0, readsize);d Ownloadcount + = readsize;//always get the size downloaded to//LOG.D ("TAG", "Downloadcount" + Downloadcount)/** * 1% */if each time (Updatecount = 0| | (Downloadcount * 100/totalsize-down_step) >= updatecount) {updatecount + = down_step;//Change notification bar//LOG.D ("TAG", "Start download. 。 Builder = new Notification.builder (CTX). Setsmallicon (R.drawable.logo). Setcontenttext ("Downloading (" + updatecount + "%) ... "). Setprogress (Updatecount, False) Manager.notify (8, Builder.build ());}} if (httpurlconnection! = null) {Httpurlconnection.disconnect ();} Inputstream.close (); Outputstream.close ();} catch (Malformedurlexception e) {e.printstacktrace (),} catch (IOException e) {e.printstacktrace ();} catch (Exception e) { E.printstacktrace ();} return null;} @OverrideproteCTED void OnPostExecute (String result) {//download complete, click Install System.out.println ("Updatefile.." + Updatefile); Uri uri = uri.fromfile (updatefile), Intent Intent = new Intent (Intent.action_view); Intent.setdataandtype (URI, " Application/vnd.android.package-archive ");p endingintent = pendingintent.getactivity (ctx, 0, intent, 0); builder = new Notification.builder (CTX). Setsmallicon (R.drawable.logo). Setcontentintent (pendingintent). SetContentTitle (" APK download Update "). Setcontenttext (" Download successful, click Install "). Setprogress (8, Builder.build ());}. Execute (URL);} private static void Createnotification (Context ctx) {//The most common notification bar manager = (Notificationmanager) ctx.getsystemservice (CTX . Notification_service); builder = new Notification.builder (CTX). Setsmallicon (R.drawable.logo). Setcontenttitle (" APK download Update "). Setcontenttext (" Ready to download ... "). Setprogress (0, false); Manager.notify (8, Builder.build ());}
When the download is complete, you can click Install.
Note: When your apk is signed, if the phone above the APK is not signed, then the installation will prompt the signing of different package conflicts, can not be installed, so that can only uninstall the unsigned apk and then install