A mature business app must constantly exit the new version. So, it is impossible for users to go to the application market to download the new version of the application, we should provide the automatic upgrade within the application features. The automatic upgrade actually contains two levels, one is the entire app upgrade, that is, download the new version of the app, and then install to replace the existing. There is also an upgrade is a module upgrade, this upgrade is generally a silent upgrade, that is, users are completely unaware. This in my big thunder inside often do, take each channel to try wrong, for an internet company is more common but. And these modules, are certainly such as, analytic libraries, download libraries, libraries, these back-end libraries.
Today, however, I would like to say is the automatic upgrade of the app, the module's silent upgrade we will have time to talk about it again.
App upgrade processSteal someone else's diagram to illustrate the overall process of upgrading your app.
Yes, this picture is stolen from others. Because, this is the general process of app upgrade. So, we know, the first step, we need to go to the server query version information to determine whether to upgrade.to server query version informationSee the code for details,
Private static final class Upgradeloader extends Asynctask<void, Void, updateinfo> {private weakreference< ; Activity> Mcontext; Private Boolean Mauto; Private Iupgradecheck Mupgradechecklistener; protected Boolean mchecked; Public Upgradeloader (Activity context, Boolean auto, Iupgradecheck listener) {mcontext = new weakreference< Activity> (context); Mauto = auto; Mupgradechecklistener = listener; } @Override protected void OnPreExecute () {if (Mupgradechecklistener! = null) {m Upgradechecklistener.beforecheck (); }} @Override protected UpdateInfo doinbackground (Void ... params) {if (mupgradecheckliste NER = null) {Mupgradechecklistener.oncheck (); } Context CTX = Mcontext.get (); String channel = ""; if (CTX! = null) {channel = Util.getchannElID (CTX); } return Dataproxy.getinstance (). Getupdateinfo (Util.getversionname (myapplication.sinstance), Util.getosversion (), channel); Go to the server to query version information, this is our HTTP package, here, each company can define their own address and parameters, very simple. } @Override protected void OnPostExecute (UpdateInfo result) {//from the server query back, decide if you want to upgrade if (Mupgradecheck Listener = null) {Mupgradechecklistener.aftercheck (); } if (!iscancelled ()) {if (result = = null) {if (!mauto) { Uihelper.showtoast (myapplication.sinstance, "Check for updates failed", Toast.length_short); }} else {switch (result.type) {case Updateinfo.no_upgrade: if (!mauto) {uihelper.showtoast (kankanapplication.sinstance, "Already the latest version, no update", T Oast. Length_short); } break; Case UpdateInfo.UPDATE_NOT_TIPS:if (!mauto) {Builddialog (Resul T); } else {downloadapk (kankanapplication.sinstance, Result.latesturl); } break; Case UpdateInfo.UPDATE_TIPS:case UpdateInfo.UPDATE_FOURCE:if (!) ( Mauto && preferencemanager.instance (Mcontext.get ()). isverionskiped (result.latestversion)) { Builddialog (result); } Default:break; } } } }
Note the above code. We're just signaling, so it's impossible to make all the code available for everyone to learn. Is your first step is to go to the server to query version information. Then when you are sure that you want to download the APK, the downloadapk function is called. Download the APK.Download apkOn the Internet, many people offer the use of
httpclientTo download the apk method, I give another idea here. Both methods are available and are recommended for use by us, as this is a system method for downloading larger files.
See the code:
private static void downloadapk (context context, String URL) {Downloadmanager manager = (Downloadmanager) Context. Getsystemservice (Context.download_service); Uri uri = uri.parse (URL); String scheme = Uri.getscheme (); if (scheme = = NULL | | (!scheme.equals ("http") &&!scheme.equals ("https")) {Log.error ("only supports Http/https url={}", URL); } else {try {downloadmanager.request down = new Downloadmanager.request (URI); Down.setallowednetworktypes (DownloadManager.Request.NETWORK_MOBILE | DownloadManager.Request.NETWORK_WIFI); Down.setshowrunningnotification (TRUE); Down.setvisibleindownloadsui (TRUE); Down.setdestinationinexternalfilesdir (context, NULL, apk_name); Manager.enqueue (down);//Start download} catch (Exception e) {e.printstacktrace (); Uihelper.showtoast (Context, "Download app lostFailure, please go to apply the market upgrade ", Toast.length_long); } } }
This way, using
Downloadmanagercan be very convenient to implement the APK download. If you want to have a more in-depth understanding of this class, you can check the relevant information. You can check the download progress and other related information.
The download is complete and the next step is to install it. We need an event to trigger the installation. The way is, we should listen to the download to complete this thing, after the download is completed, there will be a broadcast, when the monitor heard this broadcast, we began to install.Install apkListen for download complete and install.
public void OnReceive (context context, Intent Intent) { if (intent.getaction (). Equals (Downloadmanager.action_ Download_complete) {//Listen download complete Long id = Intent.getlongextra (downloadmanager.extra_download_id,-1); if (Id >-1) { File File = Queryfile (context, id); if (file = null && file.exists ()) { Intent startinent = new Intent (); Startinent.setaction (Intent.action_view); Startinent.setflags (intent.flag_activity_new_task); Startinent.setdataandtype (uri.fromfile (file), "application/vnd.android.package-archive"); Context.startactivity (startinent);//Install APK}}}}
At this point, an automatic upgrade of an APK is complete. In fact, there are some aftercare work needs to do is that you need to delete the APK, the method of removing the apk should be deleted after the installation is complete, the internet has said through the broadcast monitoring method to delete, is to listen to the installation after the completion of the deletion. Let's go and have a look.Delete apk fileHere, I'll give you an example: the first thing to do is to get the app's installation state, in the form of a broadcast
The following is an application-related broadcast Action
Action_package_added A new app package has been installed on the device, the data includes the package name (the latest installed package program cannot receive this broadcast)
action_package_replaced a new version of the app is installed to the device, replacing the previously existing version
Action_package_changed an existing application package has changed, including the package name
Action_package_removed an existing application package has been removed from the device, including the package name (the package program being installed cannot receive this broadcast)
action_package_restarted the user restarts a package, all processes of the package will be killed and all runtime status associated with it should be removed, including the package name (restart package program cannot receive this broadcast)
Action_package_data_cleared user already knows the data of a package, including the package name (The Purge package program cannot receive this broadcast)
Code implementation
In
Androidmanifest.xmlThe broadcast is defined in
<receiver android:name= ". Appinstallreceiver " android:label=" @string/app_name "> <intent-filter> <action android: Name= "Android.intent.action.PACKAGE_ADDED"/> <action android:name= "android.intent.action.PACKAGE_ Replaced "/> <action android:name=" Android.intent.action.PACKAGE_REMOVED "/> <data android: Scheme= "Package"/> </intent-filter> </receiver>
Then the receiving end:
public class Appinstallreceiver extends Broadcastreceiver {@Override public void onre Ceive (context context, Intent Intent) {Packagemanager manager = Context.getpackagemanager (); if (Intent.getaction (). Equals (intent.action_package_added)) {String PackageName = Intent.getdata (). Getschemesp Ecificpart (); Toast.maketext (Context, "Installation succeeded" +packagename, Toast.length_long). Show (); } if (Intent.getaction (). Equals (intent.action_package_removed)) {String PackageName = Intent.getdata (). Getschemespecificpart (); Toast.maketext (Context, "Uninstall succeeded" +packagename, Toast.length_long). Show (); } if (Intent.getaction (). Equals (intent.action_package_replaced)) {String PackageName = Intent.getdata () . Getschemespecificpart (); Toast.maketext (context, "Replace succeeded" +packagename, Toast.length_long). Show ();
<span style= "White-space:pre" ></span>doanloadapk.delete ()//delete the APK file just downloaded from SD} }
Good. APK auto-upgrade says here.
Have time to tell everyone
Silent upgrade of modulesThe implementation.
Java4android App Auto-upgrade