Android enables app auto-update function

Source: Internet
Author: User

Now the general Android software needs to be constantly updated, when you open an app, if there is a new version, it will prompt you to have a new version needs to be updated. This applet implements this function.

The feature of this applet is that when there is an update, a prompt will pop up, click OK, then in the notification to create a progress bar to download, click Cancel, then cancel the update.

The following is the detailed code:

1. Create a layout file, Notification_item.xml, to generate a progress bar and download icon in the notification bar.

<?xml version= "1.0" encoding= "Utf-8"? ><relativelayout xmlns:android= "http://schemas.android.com/apk/res/ Android "Android:layout_width=" Fill_parent "android:layout_height=" fill_parent "android:padding=" 3DP "> &lt ; ImageView android:id= "@+id/notificationimage" android:layout_width= "Wrap_content" Android:layout_hei ght= "Wrap_content" android:src= "@android:d rawable/stat_sys_download"/> <textview android:id= "@+id/ Notificationtitle "android:layout_width=" wrap_content "android:layout_height=" Wrap_content "android:l        Ayout_alignparentright= "true" android:layout_torightof= "@id/notificationimage" android:paddingleft= "6DP" Android:textcolor= "#FF000000"/> <textview android:id= "@+id/notificationpercent" android:layout        _width= "Wrap_content" android:layout_height= "wrap_content" android:layout_below= "@id/notificationimage"      android:paddingtop= "2DP"  Android:textcolor= "#FF000000"/> <progressbar android:id= "@+id/notificationprogress" style= "@styl        E/progressbarhorizontal_color "android:layout_width=" wrap_content "android:layout_height=" Wrap_content " android:layout_alignleft= "@id/notificationtitle" android:layout_alignparentright= "true" Android:layout_al        igntop= "@id/notificationpercent" android:layout_below= "@id/notificationtitle" android:paddingleft= "6DP" android:paddingright= "3DP" android:paddingtop= "2DP"/></relativelayout>

2. Create the Appcontext class, which inherits from application.

Package Com.test.application;import Android.app.application;import Android.content.context;import Com.test.update.config.config;public class AppContext extends application {private static, AppContext appinstance; Private Context context;public static AppContext getinstance () {return appinstance;} @Overridepublic void OnCreate () {//TODO auto-generated method Stubsuper.oncreate (); appinstance = This;context = This.get Basecontext ();////gets the current version number//try {//packageinfo packageinfo = Getapplicationcontext ()//.getpackagemanager (). Getpackageinfo (Getpackagename (), 0);//config.localversion = Packageinfo.versioncode;//config.serverversion = 1;// Assuming the server version is 2, the local version defaults to 1//} catch (Namenotfoundexception e) {//e.printstacktrace ();//}initglobal ();} public void Initglobal () {try {config.localversion = Getpackagemanager (). Getpackageinfo (Getpackagename (), 0). Versioncode; Set the local version number Config.serverversion = 2;//Assume that the server version is 2, the local version defaults to the actual development is to get the latest version from the server, Android specific and back-end interaction See I am another///outside the Post} catch ( Exception ex) {ex.printstacktrace ();}}} 

3. Create a Profile class Config.java, define some version-related constants in this class

Package Com.test.update.config;public class Config {//version information public    static int localversion = 0;    public static int serverversion = 0;    /* Download Package Installation path */public      static final String Savepath = "/sdcard/test/";        public static final String savefilename = Savepath + "test.apk";  }


4. Write the update service class Updateservcie.java

Package Com.test.update;import Java.io.file;import Java.io.fileoutputstream;import java.io.inputstream;import Java.net.httpurlconnection;import Java.net.url;import Android.annotation.suppresslint;import Android.app.notification;import Android.app.notificationmanager;import Android.app.pendingintent;import Android.app.service;import Android.content.intent;import Android.net.uri;import Android.os.Environment;import Android.os.handler;import Android.os.ibinder;import Android.os.message;import Android.widget.RemoteViews;import  Com.test.update.config.config;public class Updateservice extends Service {//title private int TitleID = 0;//file Stores private files Updatedir = null;private File updatefile = null;//Download status private final static int download_complete = 0;private final Stati c int download_fail = 1;//notification bar private Notificationmanager Updatenotificationmanager = null;private Notification updatenot ification = null;//notification bar Jump intentprivate Intent updateintent = null;private pendingintent updatependingintent = null;/*** * Create a notification bar */remoteviews contentview;//such a lot of download code, I do not do too much explanation int downloadcount = 0;int CurrentSize = 0;long Total Size = 0;int Updatetotalsize = 0;//Prepare the relevant download work in the Onstartcommand () method: @SuppressWarnings ("deprecation") @Overridepublic int Onstartcommand (Intent Intent, int flags, int startid) {//Get pass value TitleID = Intent.getintextra ("TitleID", 0);//Create File if (Androi D.os.environment.media_mounted.equals (Android.os.Environment.getExternalStorageState ())) {Updatedir = new File ( Environment.getexternalstoragedirectory (), config.savefilename), updatefile = new File (Updatedir.getpath (), Getresources (). getString (TitleID) + ". apk");} This.updatenotificationmanager = (Notificationmanager) getsystemservice (Notification_service); This.updatenotification = new Notification ();//Set download process, click on the notification bar, back to the main interface updateintent = new Intent (This, Updateactivity.class); updatependingintent = pendingintent.getactivity (this, 0, updateintent,0);// Set the notification bar to display content Updatenotification.icon = R.drawable.ic_launcher;updatenotification.tickerteXT = "Start Download"; Updatenotification.setlatesteventinfo (This, "QQ", "0%", updatependingintent);// Notification updatenotificationmanager.notify (0, updatenotification);//Open a new thread to download, if using the service synchronization download, will cause the ANR problem, The service itself also blocks the new Thread (New Updaterunnable ()). Start ();//This is the focus of the download, is the process of the download return Super.onstartcommand (Intent, flags, Startid);} @Overridepublic ibinder onbind (Intent arg0) {//TODO auto-generated method Stubreturn null;} @SuppressLint ("Handlerleak") private Handler Updatehandler = new Handler () {@Overridepublic void Handlemessage (Message msg) {switch (msg.what) {case download_complete://click install Pendingintenturi uri = Uri.fromfile (updatefile); Intent Installintent = new Intent (Intent.action_view); Installintent.setdataandtype (URI, "application/ Vnd.android.package-archive "); updatependingintent = pendingintent.getactivity (updateservice.this, 0, InstallIntent , 0); updatenotification.defaults = notification.default_sound;//ringtone Alert Updatenotification.setlatesteventinfo ( Updateservice.this, "QQ", "Download complete, click Install." ", Updatependingintent); updatenotificationmanager.notify (0, updatenotification);//Stop Service StopService (updateintent); case DOWNLOAD_ fail://Download Failed Updatenotification.setlatesteventinfo (updateservice.this, "QQ", "Download complete, click Install. ", updatependingintent); updatenotificationmanager.notify (0, updatenotification);d Efault:stopservice (updateIntent );}}}; Public long Downloadupdatefile (String downloadurl, File saveFile) throws Exception {HttpURLConnection httpconnection = Null;inputstream is = null; FileOutputStream fos = null;try {URL url = new URL (downloadurl); httpconnection = (httpurlconnection) url.openconnection () ; Httpconnection.setrequestproperty ("User-agent", "pacifichttpclient"); if (CurrentSize > 0) { Httpconnection.setrequestproperty ("RANGE", "bytes=" + currentsize + "-");} Httpconnection.setconnecttimeout (10000); Httpconnection.setreadtimeout (20000); updatetotalsize = Httpconnection.getcontentlength (); if (httpconnection.getresponsecode () = = 404) {throw new Exception ("fail!");} is = Httpconnection.getinputstream (); fos = NEW FileOutputStream (SaveFile, false); byte buffer[] = new Byte[4096];int readsize = 0;while ((readsize = is.read (buffer)) &G T 0) {fos.write (buffer, 0, readsize); TotalSize + = readsize;//To prevent frequent notifications from tightening the application, increase the percentage by 10 to notify once if ((Downloadcount = = 0) | | (int) (TotalSize * 100/updatetotalsize)-> Downloadcount) {downloadcount + = 10;updatenotification.setlatesteventinfo (U Pdateservice.this, "Downloading", (int) totalsize * 100/updatetotalsize+ "%", updatependingintent);/*** * Here we use a custom view to display notification */updatenotification.contentview = new Remoteviews (Getpackagename (), R.layout.notification_item); UpdateNotification.contentView.setTextViewText (R.id.notificationtitle, "downloading"); UpdateNotification.contentView.setProgressBar (R.id.notificationprogress, Downloadcount, false); Updatenotificationmanager.notify (0, updatenotification);}} finally {if (httpconnection! = null) {Httpconnection.disconnect ();} if (is = null) {Is.close ();} if (fos! = null) {Fos.close ();}} return totalsize;} Class Updaterunnable IMplements Runnable {Message message = Updatehandler.obtainmessage ();p ublic void Run () {message.what = Download_complete; try {//Increase permissions <uses-permission//android:name= "Android.permission.WRITE_EXTERNAL_STORAGE" >;if (! Updatedir.exists ()) {updatedir.mkdirs ();} if (!updatefile.exists ()) {updatefile.createnewfile ();} Download function to QQ for example//Add permission <uses-permission//android:name= "Android.permission.INTERNET" >;long downloadsize = Downloadupdatefile ("http://softfile.3g.qq.com:8080/msoft/179/1105/10753/MobileQQ1.0 (Android) _build0198.apk", Updatefile); if (Downloadsize > 0) {//download succeeded Updatehandler.sendmessage (message);}} catch (Exception ex) {ex.printstacktrace (); message.what = download_fail;//download failed updatehandler.sendmessage (message);}}}

5. Writing the Activity class Updateactivity

Package Com.test.update;import Com.test.update.config.config;import Android.support.v4.app.fragment;import Android.app.activity;import Android.app.alertdialog;import Android.content.dialoginterface;import Android.content.intent;import Android.os.bundle;import Android.util.log;import Android.view.LayoutInflater;public Class Updateactivity extends Activity {@Overrideprotected void onCreate (Bundle savedinstancestate) {super.oncreate ( Savedinstancestate); Setcontentview (R.layout.activity_main); Checkversion ();} /** * Check for updated version */public void Checkversion () {if (Config.localversion < config.serverversion) {log.i ("Hgncxzy", "======== ====================== ");//Discover new version, prompt user to update alertdialog.builder alert = new Alertdialog.builder (this); Alert.settitle (" Software Upgrade "). Setmessage (" New version found, recommended for immediate update use. "). Setpositivebutton ("Update", new Dialoginterface.onclicklistener () {public void OnClick (Dialoginterface dialog,int which) { Turn on update Service updateservice//here in order to better modularize the update, you can pass some updateservice dependent values//such as layout ID, resource ID, dynamically obtained title, here app_name for example intent updateintent = new Intent (updateactivity.this,updateservice.class); Updateintent.putextra ("TitleID", R.string.app_name); StartService (updateintent);}}). Setnegativebutton ("Cancel", new Dialoginterface.onclicklistener () {public void OnClick (Dialoginterface dialog,int which) { Dialog.dismiss ();}}); Alert.create (). Show (); else {//cleanup work, omit//Cheanupdatefile ()}}}

6. Add permissions and load the service statically (loaded in the configuration file).

    <uses-permission android:name= "Android.permission.INTERNET"/>    <uses-permission android:name= " Android.permission.WRITE_EXTERNAL_STORAGE "/>    <uses-permission android:name=" android.permission.MOUNT_ Unmount_filesystems "/>
Registration Services

<service android:name= "Com.test.update.UpdateService" >        </service>
The complete Androidmanifest.xml file is as follows:


<?xml version= "1.0" encoding= "Utf-8"? ><manifest xmlns:android= "http://schemas.android.com/apk/res/        Android "package=" Com.test.update "android:versioncode=" 1 "android:versionname=" 1.0 "> <uses-sdk android:minsdkversion= "8" android:targetsdkversion= "8"/> <application android:name= "com.test.appli cation.            AppContext "android:icon=" @drawable/ic_launcher "android:label=" @string/app_name "> <activity Android:name= "com.test.update.UpdateActivity" android:label= "@string/app_name" > <in tent-filter> <action android:name= "Android.intent.action.MAIN"/> <category Android Oid:name= "Android.intent.category.LAUNCHER"/> </intent-filter> </activity> <se Rvice android:name= "Com.test.update.UpdateService" > </service> </application> <uses-permis Sion Android:name= "Android.permiSsion. INTERNET "/> <uses-permission android:name=" Android.permission.WRITE_EXTERNAL_STORAGE "/> <uses-permiss Ion Android:name= "Android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/></manifest>

To the end of this encoding.

SOURCE download



Android enables app auto-update function

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.