Android version update notification bar update download install _android

Source: Internet
Author: User

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

• Check the current version number

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

PackageInfo PackageInfo = Context.getpackagemanager (). Getpackageinfo (Context.getpackagename (), 0);

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

• Download APK files

/**
* 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 files used
Notify.contentview = new Remoteviews (Context.getpackagename (),
R.layout.view_notify_ Item);
Manager.notify (notify);
Set up the downloaded APK file
fileinstall = Fileoperate.mkdirsdcardfile ("DownLoad", Apk_name
+ version + ". APK");
Downloadschedule (Apkuri, Completehandler, context,
fileinstall);
}

Fileoperate is the file tool class that you wrote

The layout shown 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_iv" android:gravity= "center_vertical" android:text= "0%" Android:textcolor= "@color/white" android:textsize= "12sp"/> <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 the network, download a file, and return the progress * * @param URI * @param handler * @param conte XT * @param file */public static void Downloadschedule (Final String URI, final Handler Handler, context, final Fil
D () {@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 file Out.write (buffer, 0, Len);//Current Progress int schedule = (int) (File.length () *)/length);  Notify update progress (10,7,4 is not required to update progress every time) if (temp!= schedule && (Schedule% = 0 | | Schedule% 4 = 0 | | Schedule% 7
= = 0) {//Ensure that the same data is only issued once temp = schedule; Handler.sendemptymessage (schedule);}
Out.flush ();
Out.close ();
In.close ();
catch (IOException e) {e.printstacktrace ();}}
}.start (); }

Handler update according to 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 (MB, 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 Notification bar
Manager.cancel (m);
INSTALLAPK (Fileinstall);
}
;

Call the system installation when the download is complete.

• Install APK

/**
* Installation 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);
}

Setup is 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.