Let's take a look at the effect diagram you want to implement:
For Android users, the mobile app market is no exaggeration to say that it's flying, such as millet, the charm family, Baidu, Jifeng, application treasure, etc., when we want to online a new version of the app, first do not say the channel packaging trouble, simply refers to upload app to the major application market workload is already very large, Finally we put the app all uploaded, suddenly found a will cause the application of the small bug, then the crash ah, obviously not very big changes, do we have to go back to the major application of the market version again upload? Trust me, the operators will kill you!!
There is a problem, naturally there will be a solution to the problem, so we will think that if the built-in Automatic Updates in the app, then we will be able to save a lot of trouble, of course, there are a lot of third-party SDK on this function.
Well, to get back to it, today we're going to implement the app update automatically.
The process is actually not complicated: when the user opens the app, we ask the app to send a check version of the network request, or use the server to push a pass message to the app to check the app's version, and if the current app version is older than the one on the servers, we'll alert the user to download and update the app, Of course, in certain circumstances, we can also force the user to upgrade, of course, it is very unfriendly, as far as possible to reduce this practice.
Okay, let's comb the flow, first of all, since it's an app update, then we need to download the new app, then we need a notification to tell the user the current download progress, and then when the app installation package is downloaded, we need to go to the system installer to install the app for updates.
Knowledge Points:
Download: Asynchronous HTTP request file download, and listen for current download progress (I used okhttp here)
Notice: Notification (please read the API documentation for your own use)
Installation: Intent (please read the API document for specific usage)
Look at the specific implementation code:
We need a backend service to support app downloads.
Import android.app.Notification;
Import Android.app.NotificationManager;
Import android.app.PendingIntent;
Import Android.app.Service;
Import android.content.Intent;
Import Android.graphics.BitmapFactory;
Import Android.net.Uri;
Import Android.os.IBinder;
Import android.support.annotation.Nullable;
Import Android.support.v7.app.NotificationCompat;
Import Com.fangku.commonlibrary.utils.StorageUtil;
Import Com.zhy.http.okhttp.OkHttpUtils;
Import Com.zhy.http.okhttp.callback.FileCallBack;
Import Java.io.File; Import OKHTTP3.
Call; /** * Automatic Download Update apk service * Create by:chenwei.li * date:2016-08-14 * time:09:50 * email:lichenwei.me@foxmail.com/Publ IC class Downloadservice extends Service {private String mdownloadurl;//apk download path private Notificationmanager Mnotifica
Tionmanager;
Private Notification mnotification;
@Override public void OnCreate () {super.oncreate ();
Mnotificationmanager = (Notificationmanager) getsystemservice (Service.notification_service);
} @Override public int Onstartcommand (Intent Intent, int flags, int startid) {if (Intent = null) {notifymsg ("Warm Reminder", "File download Failed", 0
);
Stopself (); } Mdownloadurl = Intent.getstringextra ("Apkurl");//Get Download APK link downloadFile (mdownloadurl);//Download APK return
Super.onstartcommand (Intent, flags, Startid);
@Nullable @Override public IBinder onbind (Intent Intent) {return null; private void Notifymsg (string title, string content, int progress) {Notificationcompat.builder Builder = new Notifi Cationcompat.builder (this); In order to be backward-compatible, the Notificationcompat under the V7 package is used to construct Builder.setsmallicon (r.mipmap.icon_login_ Logo). Setlargeicon (Bitmapfactory.decoderesource (Getresources (), R.mipmap.icon_login_logo)). SetContentTitle (
title);
if (Progress > 0 && Progress < 100) {//download in progress builder.setprogress (MB, progress, false);
else {builder.setprogress (0, 0, false);
} builder.setautocancel (True);
Builder.setwhen (System.currenttimemillis ());
Builder.setcontenttext (content); if (progresS >= 100) {//Download complete builder.setcontentintent (Getinstallintent ());
} mnotification = Builder.build ();
Mnotificationmanager.notify (0, mnotification); /** * Install apk file * * @return/private pendingintent getinstallintent () {File File = new file (Storageutil.downloa
D_dir + "app filename");
Intent Intent = new Intent (Intent.action_view);
Intent.setflags (Intent.flag_activity_new_task);
Intent.setdataandtype (Uri.parse ("file://" + File.getabsolutepath ()), "application/vnd.android.package-archive");
Pendingintent pendingintent = pendingintent.getactivity (this, 0, intent, pendingintent.flag_update_current);
return pendingintent; /** * Download apk file * * @param URL/private void downloadFile (String url) {okhttputils.get (). URL (URL). Build (). Cute (new Filecallback (Storageutil.download_dir, "app filename") {@Override public void OnError (called call, Exception e, int
ID) {notifymsg ("Warm Reminder", "File download Failed", 0);
Stopself (); @Override public void Onresponse (File response, int id) {//callback notifymsg after file download is complete ("Warm reminder", "File download Completed", 100);
Stopself (); @Override public void inprogress (float progress, long total, int id) {//progress*100 download progress for the current file, total is the file size if ( int) (Progress * 100)% 10 = 0) {//Avoid frequent refresh view, set up every download 10% reminder update Progress notifymsg ("Warm Reminder", "File is downloading ...", (int) (Progress * 1
00));
}
}
}); }
}
And then we just need to pick up the service when we want to update the app, like "version checking" in the system setup.
Intent Intent = new Intent (Mcontext, downloadservice.class);
Intent.putextra ("Apkurl", "apk download Address");
StartService (Intent);
Summarize
Here I just rough demo The local Automatic Update app function, in the practical application, we should cooperate with the service side to do, for instance when the user launches the app to compare the version number, if the version number is lower than the server's version number, then the service end should give the client a pass through to send, The push content here should be the download address for the new version of app, at this point can be based on this address to download a new version of the app, when a major update, no longer compatible with the old version of the time, you can force the user to upgrade, there are many schemes, such as the call System-level dialog box, so that users do not cancel the operation, There is no more description here. The above is the full content of this article, I hope to be able to help people in need.