Android ProgressBar Preliminary application

Source: Internet
Author: User

The download progress is instantly displayed using ProgressBar.

Problems encountered on the way:

1. The main thread cannot open URLs, use Toast, etc.

2. Child threads Cannot modify UI

3. Allow network protocols

4. Pause download and Continue download

........

Fragment_main Layout file

1 <Relativelayoutxmlns:android= "Http://schemas.android.com/apk/res/android"2 Xmlns:tools= "Http://schemas.android.com/tools"3 Android:layout_width= "Match_parent"4 Android:layout_height= "Match_parent"5 Tools:context= "Com.dragon.android.textbar.mainactivity$placeholderfragment" >6 7     <!--Prigressbar progress bar -8     <!--Progress Current Progress -9     <!--indeterminate ambiguous default false -Ten     <ProgressBar One         Android:id= "@+id/progressbar1" A style= "? Android:attr/progressbarstylehorizontal" - Android:layout_width= "Match_parent" - Android:layout_height= "Wrap_content" the android:layout_centerinparent= "true" - Android:max= "+" - android:progress= "0" - android:indeterminate= "true"/> +  -     <Button +         Android:id= "@+id/button1" A Android:layout_width= "Wrap_content" at Android:layout_height= "Wrap_content" - Android:layout_alignparenttop= "true" - Android:layout_centerhorizontal= "true" - Android:onclick= "Startload" - Android:layout_margintop= "86DP" - Android:background= "#009FEE" in Android:text= "@string/start" - Android:textcolor= "#ffffff" /> to  +     <TextView -         Android:id= "@+id/textview1" the Android:layout_width= "Wrap_content" * Android:layout_height= "Wrap_content" $ Android:layout_above= "@+id/progressbar1"Panax Notoginseng Android:background= "@null" - Android:layout_alignparentleft= "true" /> the      + </Relativelayout>
Fragment_main

Strings.xml

1 <?XML version= "1.0" encoding= "Utf-8"?>2 <Resources>3 4     <stringname= "App_name">Hwdownload</string>5     <stringname= "Hello_world">Hello world!</string>6     <stringname= "Action_settings">Settings</string>7     <stringname= "Start">Begin</string>8     <stringname= "Stop">Time out</string>9     <stringname= "Contin">Go on</string>Ten  One </Resources>
Strings

(Issue 3) configuration in the Androidmanifest file

1 <!--  -2     <uses-permission  android:name  = "Android.permission.INTERNET"/>

Mainactivity (question 1, 2)

 PackageCom.dragon.android.textbar;Importjava.io.IOException;ImportJava.io.InputStream;Importjava.net.HttpURLConnection;ImportJava.net.URL;Importandroid.app.Activity;ImportAndroid.os.Bundle;ImportAndroid.view.View;ImportAndroid.widget.Button;ImportAndroid.widget.ProgressBar;ImportAndroid.widget.TextView;ImportAndroid.widget.Toast;/*** Only the thread that creates a view can change the view's UI!!! The main thread is also called the UI thread*/ Public classMainactivityextendsActivity {PrivateProgressBar progressBar1; PrivateButton button1; PrivateTextView TextView1; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.fragment_main); ProgressBar1=(ProgressBar) Findviewbyid (R.ID.PROGRESSBAR1); Button1=(Button) Findviewbyid (R.id.button1); TextView1=(TextView) Findviewbyid (R.ID.TEXTVIEW1); }     Public voidstartload (view view) {String text=(String) button1.gettext (); //Setting the button content----is not used ...Button1.settext (Text.equals (Getresources () getString (R.string.start))?r.string.stop: (Text.equals (Getresources (). getString (r.string.stop))?r.string.contin:r.string.stop)); Progressbar1.setindeterminate (false); NewThread (NewRunnable () {Private intpercent; @Override Public voidrun () {Try {                    //The open URL must be on a child threadURL url =NewURL ("Http://b.zol-img.com.cn/sjbizhi/images/9/540x960/1472549276394.jpg"); HttpURLConnection Conn=(HttpURLConnection) url.openconnection (); //Conn.setrequestmethod ("GET"); //conn.setreadtimeout (5000); //Conn.setconnecttimeout (the);                    intContentLength =conn.getcontentlength (); if(Conn.getresponsecode () = =HTTPURLCONNECTION.HTTP_OK) {InputStream is=Conn.getinputstream (); byte[] buffer =New byte[1024]; intLen =-1; intsum = 0;  while(len = is.read (buffer))! =-1) {sum+=Len; //pay attention to the strong turn mode, prevent always 0Percent = (int) (100.0 * Sum/contentlength);
//Child threads running on the main threadRunonuithread (NewRunnable () {@Override Public voidrun () {progressbar1.setprogress (percent); Textview1.settext (Percent+ "%"); if(Percent = =Progressbar1.getmax ()) {Toast.maketext (mainactivity). This, "Download done!", Toast.length_short). Show (); } } }); } is.close (); Conn.disconnect (); } } Catch(IOException e) {e.printstacktrace (); }}). Start (); }}

However, it did not solve the problem 4, to use a breakpoint to continue, but will not be stored assets resources ... ***************

 

Android ProgressBar Preliminary application

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.