Android Program version Update--Notification bar update download installation

Source: Internet
Author: User

Android app check version update, download in the notification bar, update download progress, download complete automatic installation, as follows:

    • Check the current version number

The Versioncode in the Androidmanifest file is used to identify the version, where the server puts a new version of Apk,versioncode greater than the current version, and the following code is used to get the value of Versioncode

PackageInfo PackageInfo = Context.getpackagemanager (). Getpackageinfo (Context.getpackagename (), 0); int localVersion = Packageinfo.versioncode;

Compare with current Versioncode and server side, if less than, make version update

    • Download APK file
/**   * Download apk   *    * @param apkuri   * *
private void downloadnewapk (String Apkuri, string version) { manager = (notificationmanager) context . Getsystemservice (context. Notification_service)); notify = new Notification (); Notify.icon = R.drawable.ic_launcher; The notification bar displays the layout file used Notify.contentview = new Remoteviews (Context.getpackagename (), R.layout.view_notify_item ); Manager.notify (notify); Build the downloaded APK file fileinstall = Fileoperate.mkdirsdcardfile ("DownLoad", Apk_name + version + ". apk"); Downloadschedule (Apkuri, Completehandler, context, fileinstall); }
Fileoperate is a file tool class that you write yourself

The layout displayed by the notification bar, View_notify_item.xml

<?xml version= "1.0" encoding= "Utf-8"? ><relativelayout xmlns:android= "http://schemas.android.com/apk/res/ Android "Android:layout_width=" Match_parent "android:layout_height=" match_parent "android:layout_marginleft=" 10DP         "Android:background=" #00000000 "android:padding=" 5DP "> <imageview android:id=" @+id/notify_icon_iv "    Android:layout_width= "25DP" android:layout_height= "25DP" android:src= "@drawable/ic_launcher"/> <textview android:id= "@+id/notify_updata_values_tv" android:layout_width= "Wrap_content" Android:        layout_height= "Wrap_content" android:layout_centerhorizontal= "true" android:layout_marginbottom= "6DP" android:layout_marginleft= "15DP" android:layout_margintop= "5DP" android:layout_torightof= "@id/notify_icon_i V "android:gravity=" center_vertical "android:text=" 0% "android:textcolor=" @color/white "Androi D:textsize= "12SP"/> &LT;PROgressbar android:id= "@+id/notify_updata_progress" style= "@android: Style/widget.progressbar.horizontal" Android:layout_width= "Fill_parent" android:layout_height= "wrap_content" android:layout_below= "@id/notify_ Icon_iv "android:layout_margintop=" 4DP "android:max="/></relativelayout>
/** * Connect to the network, download a file, and return the progress * * @param URI * @param handler * @param context * @param file */
public static void Downloadschedule (Final String URI, final Handler Handler, context context, final file file) {if (!file.exists ()) {handler.sendemptymessage (-1); Return }//Read the length of the file each time final int perlength = 4096; New Thread () {@Override public void run () {super.run (); try {URL url = new URL (URI); HttpURLConnection conn = (httpurlconnection) URL. OpenConnection (); Conn.setdoinput (TRUE); Conn.connect (); InputStream in = Conn.getinputstream (); 2865412 Long length = Conn.getcontentlength (); 1k byte[per read] buffer = new Byte[perlength]; int len =-1; FileOutputStream out = new FileOutputStream (file); int temp = 0; while (len = in.read (buffer))! =-1) {//write to file out.write (buffer, 0, Len); Current progress int schedule = (int) ((File.length () * +)/length); Notifies update progress (10,7,4 evenly divisible before notification, no need to update progress every time) if (temp! = Schedule &am p;& (Schedule% = = 0 | | Schedule% 4 = 0 | | Schedule% 7 = 0)) {//Ensure that the same data is only sent once temp = schedule; Handler.sendemptymessage (schedule); }} out.flush (); Out.close (); In.close (); } catch (IOException e) {e.printstacktrace (); }}}.start (); }

Handler updates based on download progress

    • Update Notification bar progress bar
/**  * UPDATE Notification bar * *  
Private Handler Completehandler = new Handler () {public void Handlemessage (Android.os.Message msg) { //update Notification bar if (Msg.what <) { Notify.contentView.setTextViewText ( r.id.notify_updata_values_tv, Msg.what + "%"); Notify.contentView.setProgressBar (r.id.notify_updata_progress, msg.what, false); Manager.notify (notify); } else { Notify.contentView.setTextViewText ( r.id.notify_updata_values_tv, "Download Complete"); Notify.contentView.setProgressBar (r.id.notify_updata_progress, msg.what, false);//clear the notification bar Manager.cancel (+); INSTALLAPK (Fileinstall);};};

The system installation is called after the download is complete.

    • Install APK
/**  * Install apk  *   * @param file  * *
private void installapk (file file) { Intent Intent = new Intent (); Intent.addflags (intent.flag_activity_new_task); Intent.setaction (Android.content.Intent.ACTION_VIEW); Intent.setdataandtype (uri.fromfile (file), "application/vnd.android.package-archive"); Context.startactivity (intent); }

Installation done.

Related Article

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.