Android Learning Series (3)-custom progress view and internal storage for automatic app update

Source: Internet
Author: User

The friendly visual perception and stable and error-free performance come from our pursuit of aesthetics and comprehensive consideration. I have always enjoyed it from a technical point of view.
This articleArticleIt is a must-have knowledge for Android Developers. It is not perfect but useful for everyone.

This article is a supplement to the previous "android Learning Series (2) -- download in the notification bar of APP Auto Update", so it is only for the point and ends.
1. Internal Storage
In consideration of the possibility that the user has disabled sdcard or the computer is temporarily inserted on the computer and is in the disk connection status, we should also ensure that ourProgramIt can also run normally. Therefore, we need to consider internal storage.
I have set the internal storage to the/data/xxxxxappxxxx/Files directory.CodeAs follows:

 
// Create a directory and file if (Android. OS. environment. media_mounted.equals (Android. OS. environment. getexternalstoragestate () {updatedir = new file (environment. getexternalstoragedirectory (), Global. downloaddir);} else {// Files directory updatedir = getfilesdir ();} updatefile = new file (updatedir. getpath (), getresources (). getstring (titleid) + ". APK ");

 2. Internal Storage Permissions
All of them run normally, but when we execute the running operation for the APK file, the prompt is as follows,
 
"Parsing package error "?? In fact, the file you downloaded may not necessarily be bad or wrong, or the permission of the Android system may be improper. Add the following core code before execution:

 
String cmd = "chmod + X" + updatefile. getpath (); try {runtime.getruntime(.exe C (CMD);} catch (ioexception e) {e. printstacktrace ();}

3. A bug of the progress bar component is displayed in the notification bar.
Setting the visibility of the progress bar in the notification bar will crash for no reason.

 
// There is no syntax error in the following sentence, but it will cause a program error // to solve this problem, we will wrap a layer of linearlayout outside the progressview to control the visibility updatenotification. contentview. setviewvisibility (progressviewid, view. gone );

4. view the custom progress bar.
Layout file updata_nitification.xml:

<? XML version = "1.0" encoding = "UTF-8"?> <Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" Android: layout_width = "fill_parent" Android: layout_height = "fill_parent" Android: Orientation = "vertical" Android: layout_weight = "2" Android: paddingleft = "5dip"> <linearlayout Android: layout_width = "fill_parent" Android: layout_height = "fill_parent" Android: gravity = "Left | weight" Android: Orientation = "horizontal" Android: layout _ Weight = "1"> <imageview Android: src = "@ drawable/icon" Android: layout_width = "24dip" Android: layout_height = "fill_parent" Android: scaletype = "fitcenter"/> <textview Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: text = "@ string/app_name" Android: textcolor = "#000000" Android: paddingleft = "5dip" Android: textsize = "16dip"/> </linearlayout> <linearlayout Android: layout_width = "fi Ll_parent "Android: layout_height =" fill_parent "Android: gravity =" Left "Android: Orientation =" horizontal "Android: layout_weight =" 1 "> <textview Android: id = "@ + ID/update_icationication_progresstext" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: textcolor = "# 8f8f8f" Android: textsize = "14dip"/> <linearlayout Android: Id = "@ + ID/update_icationication_progressblock" Android: Layout _ Width = "fill_parent" Android: layout_height = "wrap_content" Android: Orientation = "horizontal"> <progressbar Android: Id = "@ + ID/update_icationication_progressbar" Android: layout_width = "fill_parent" Android: layout_height = "wrap_content" style = "? Android: ATTR/progressbarstylehorizontal "/> </linearlayout>

Start download:

 
Updatenotification. contentintent = updatependingintent; updatenotification. contentview. setprogressbar (COM. cnblogs. tianxia. subway. r. id. update_icationication_progressbar, 100, 0, false); updatenotification. contentview. settextviewtext (COM. cnblogs. tianxia. subway. r. id. update_icationication_progresstext, "0% ");

Downloading. The download progress bar is displayed:

 
Updatenotification. contentview. setprogressbar (COM. cnblogs. tianxia. subway. r. id. update_notification_progressbar, 100, (INT) (totalsize * 100/updatetotalsize), false); updatenotification. contentview. settextviewtext (COM. cnblogs. tianxia. subway. r. id. update_notification_progresstext, (INT) (totalsize * 100/updatetotalsize) + "%"); updatenotificationmanager. notify (0, updatenotification );

download complete. Click to install:

 // click to install pendingintent 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 notification updatenotification. c Ontentintent = updatependingintent; // install the updatenotification interface. contentview. setviewvisibility (COM. cnblogs. tianxia. subway. r. id. update_icationication_progressblock, view. gone); updatenotification. contentview. settextviewtext (COM. cnblogs. tianxia. subway. r. id. update_icationication_progresstext, "Download complete. Click" Install! "); Updatenotificationmanager. Y (0, updatenotification); 

as follows:
if you like it, please recommend it. Thank you for your support!

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.