First, sort out the idea of automatic APK update: 1. Compare the server version with the local APK version; 2. If the server version is newer, send a notification to the user for download; 3. Click Start download; 4. After the download is complete, the user is prompted to click "Install". 5. Click "Install" to complete the entire process;
Below is, you can first check whether it is the effect you want, kiss!
OK, and then directly add the code. This document will not be mentioned after version comparison. It will start with the notification.
// Summary string tickertext = context. getresources (). gettext (R. String. app_name). tostring () + "new version found. We recommend that you update it! "; // Title String title = context. getresources (). gettext (R. string. app_name ). tostring () + "Update"; // content string content = "click to update" + context. getresources (). gettext (R. string. app_name ). tostring (); // logoint icon = R. drawable. logo; Notification = new notification (icon, tickertext, system. currenttimemillis (); intent updateinte = new intent (context, UpdateService. class); updateinte. putextra ("url", RE); pendingintent = pendingintent. getservice (context, 0, updateinte, 0); // click open service to download notification. setlatesteventinfo (context, title, content, pendingintent); notification. defaults = notification. default_sound; notification. flags = notification. flag_auto_cancel; icationicationmanager manager = (icationicationmanager) context. getsystemservice (context. notification_service); manager. Y (101, notification );
The following is the service download class. The following code has two problems. The notification version compatibility issue is reported!
/*** Download the Update Service ** @ author Tom. cai **/public class UpdateService extends Service {private string url = NULL; // notification bar private icationicationmanager updatenotificationmanager = NULL; private notification updatenotification = NULL; private string appname = NULL; private string filename = NULL; private string updatedir = NULL; // The notification bar jumps to intent @ overridepublic int onstartcommand (intent, int flags, int startid ){/ /Get the value url = intent. getstringextra ("url"); appname = getapplication (). getresources (). gettext (R. string. app_name ). tostring (); If (URL! = NULL) {filename = URL. substring (URL. lastindexof ("/") + 1); updatedir = environment. getdatadirectory () + "/data/" + this. getpackagename () + "/files/"; intent nullintent = new intent (); pendingintent = pendingintent. getactivity (getapplicationcontext (), 0, nullintent, 0); // create the file updatenotificationmanager = (icationicationmanager) getsystemservice (icationication_service); updatenotification = new Notification (); updatenotification. icon = R. drawable. logo; updatenotification. tickertext = "Updating" + appname; updatenotification. setlatesteventinfo (getapplication (), "downloading" + appname, "0%", null); updatenotification. defaults = notification. default_sound; updatenotification. flags = notification. flag_auto_cancel; updatenotification. contentintent = pendingintent; updatenotificationmanager. 127y (101, updatenoti Fication); // enable the thread now new thread (New updaterunnable ()). start ();} return Super. onstartcommand (intent, 0, 0);} private class updaterunnable implements runnable {message = updatehandler. obtainmessage (); Public void run () {message. what = 0; try {long downloadsize = downloadupdatefile (URL); log. I ("Cai", downloadsize/1024 + ""); If (downloadsize> 0) {// download successful updatehandler. sendmessage (Message) ;}} catch (exce Ption ex) {ex. printstacktrace (); message. what = 1; // download failed updatehandler. sendmessage (Message) ;}} private handler updatehandler = new handler () {@ overridepublic void handlemessage (Message MSG) {Switch (MSG. what) {Case 0:/* Try restarting runtime.getruntime(cmd.exe C ("chmod 777" + updatedir.getabsolutepath({)runtime.getruntime(cmd.exe C ("chmod 777" + updatefile. getabsolutepath ();} catch (ioexception e) {e. printstac Ktrace ();} * // click to install pendingintentintent installintent = new intent (intent. action_view); // installintent. addflags (intent. flag_activity_reset_task_if_needed | intent. flag_activity_new_task); installintent. setdataandtype (URI. fromfile (new file (updatedir, filename), "application/vnd. android. package-Archive "); // getapplication (). startactivity (installintent); */pendingintent updatependingintent = pendingintent. Getactivity (UpdateService. this, 0, installintent, 0); updatenotification. defaults = notification. default_sound; // ringtone notification updatenotification. flags = notification. flag_auto_cancel; updatenotification. setlatesteventinfo (UpdateService. this, appname, "Download complete, click Install", updatependingintent); updatenotificationmanager. Y (101, updatenotification); // stop the service stopself (); break; Case 1: intent nullintent = new intent (); pend Ingintent pendingintent = pendingintent. getactivity (UpdateService. this, 10, nullintent, 0); // download failed updatenotification. setlatesteventinfo (UpdateService. this, appname, "network connection is not normal, download failed! ", Pendingintent); updatenotification. flags = notification. flag_auto_cancel; updatenotificationmanager. Y (101, updatenotification); break; default: stopself () ;}}; // download @ suppresslint ("worldreadablefiles") Public long downloadupdatefile (string DownLoadURL) throws exception {int downloadcount = 0; int currentsize = 0; long totalsize = 0; int updatetotalsize = 0; httpurlconnection httpconnection = NULL; Inputstream is = NULL; fileoutputstream Fos = NULL; try {URL url = new URL (DownLoadURL); httpconnection = (httpurlconnection) URL. openconnection (); If (currentsize> 0) {httpconnection. setrequestproperty ("range", "bytes =" + currentsize + "-");} httpconnection. setconnecttimeout (10000); httpconnection. setreadtimeout (20000); updatetotalsize = httpconnection. getcontentlength (); If (httpconnection. get Responsecode () = 404) {Throw new exception ("fail! ");} Is = httpconnection. getinputstream ();/* Fos = new fileoutputstream (SaveFile, false); */FOS = openfileoutput (filename, mode_world_readable); byte buffer [] = new byte [4096]; int readsize = 0; while (readsize = is. read (buffer)> 0) {FOS. write (buffer, 0, readsize); totalsize + = readsize; // to Prevent the application from being tightened due to frequent notifications, if (downloadcount = 0) is notified only once when the percentage increases by 10) | (INT) (totalsize * 100/updatetotalsize)-10> downloadcou NT) {downloadcount + = 10; intent nullintent = new intent (); pendingintent = pendingintent. getactivity (getapplicationcontext (), 0, nullintent, 0); updatenotification. contentintent = pendingintent; updatenotification. setlatesteventinfo (UpdateService. this, appname + "downloading", (INT) totalsize * 100/updatetotalsize + "%", null); updatenotificationmanager. policy (101, updatenotification) ;}} Fina Lly {If (httpconnection! = NULL) {httpconnection. Disconnect ();} If (is! = NULL) {is. Close () ;}if (FOS! = NULL) {FOS. Close () ;}return totalsize ;}@ overridepublic ibinder onbind (intent) {return NULL ;}}
The above classes are not optimized and can only be used as a learning reference. Welcome to leave a message for discussion...