Android ProgressBar preliminary application, androidprogressbar
Here, you can use ProgressBar to instantly display the download progress.
Problems encountered on the way:
1. URLs cannot be opened in the main thread, and Toast can only be used in the main thread.
2. the UI cannot be modified by the sub-Thread
3. Allow network protocols
4. Pause download and continue download
........
Fragment_main layout File
1 <RelativeLayout xmlns: 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 <! -- The default value of indeterminate is false. --> 10 <ProgressBar11 android: id = "@ + id/progressBar1" 12 style = "? Android: attr/progressBarStyleHorizontal "13 android: layout_width =" match_parent "14 android: layout_height =" wrap_content "15 android: layout_centerInParent =" true "16 android: max = "100" 17 android: progress = "0" 18 android: indeterminate = "true"/> 19 20 <Button21 android: id = "@ + id/button1" 22 android: layout_width = "wrap_content" 23 android: layout_height = "wrap_content" 24 android: layout_alignParentTop = "true" 25 android: layout_centerHorizontal = "true" 26 android: onClick = "startLoad" 27 android: layout_marginTop = "86dp" 28 android: background = "#009133" 29 android: text = "@ string/start" 30 android: textColor = "# ffffff"/> 31 32 <TextView33 android: id = "@ + id/textView1" 34 android: layout_width = "wrap_content" 35 android: layout_height = "wrap_content" 36 android: layout_above = "@ + id/progressBar1" 37 android: background = "@ null" 38 android: layout_alignParentLeft = "true"/> 39 40 </RelativeLayout>Fragment_main
Strings. xml
1 <? Xml version = "1.0" encoding = "UTF-8"?> 2 <resources> 3 4 <string name = "app_name"> hwdownload </string> 5 <string name = "hello_world"> Hello world! </String> 6 <string name = "action_settings"> Settings </string> 7 <string name = "start"> start </string> 8 <string name = "stop"> suspend </string> 9 <string name = "contin"> continue </string> 10 11 </resources>Strings
(Question 3) Configure in the AndroidManifest File
1 <! -- Request network permissions --> 2 <uses-permission android: name = "android. permission. INTERNET"/>
MainActivity (question 1, 2)
Package com. dragon. android. textbar; import java. io. IOException; import java. io. inputStream; import java.net. httpURLConnection; import java.net. URL; import android. app. activity; import android. OS. bundle; import android. view. view; import android. widget. button; import android. widget. progressBar; import android. widget. textView; import android. widget. toast;/*** only the thread that creates a View can change the UI of this View !!! The main thread is also called the UI thread */public class MainActivity extends Activity {private ProgressBar progressBar1; private Button button1; private TextView textView1; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. fragment_main); progressBar1 = (ProgressBar) findViewById (R. id. progressBar1); button1 = (Button) findViewById (R. id. button1); textVie W1 = (TextView) findViewById (R. id. textView1);} public void startLoad (View view) {String text = (String) button1.getText (); // set the content of the button ---- It is useless... 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); new Thread (new Runnable () {private int percent; @ Override public void run () {try {// open URL must be in the subthread URL = new url ("http:// B .zol-img.com.cn/sjbizhi/images/9/540x960/1472549276394.jpg"); HttpURLConnection conn = (HttpURLConnection) URL. openConnection (); // conn. setRequestMethod ("GET"); // conn. setReadTimeout (5000); // co Nn. setConnectTimeout (5000); int contentLength = conn. getContentLength (); if (conn. getResponseCode () = HttpURLConnection. HTTP_ OK) {InputStream is = conn. getInputStream (); byte [] buffer = new byte [1024]; int len =-1; int sum = 0; while (len = is. read (buffer ))! =-1) {sum + = len; // pay attention to the forced conversion mode, to prevent the value from always 0 percent = (int) (100.0 * sum/contentLength );
// RunOnUiThread (new Runnable () {@ Override public void run () {progressBar1.setProgress (percent); textView1.setText (percent + "% "); if (percent = progressBar1.getMax () {Toast. makeText (MainActivity. this, "Download complete! ", Toast. LENGTH_SHORT ). show () ;}}) ;}is. close (); conn. disconnect () ;}} catch (IOException e) {e. printStackTrace ();}}}). start ();}}
* ************ The problem is not solved. 4. resumable data transfer is required, but the assets resources are not stored ..... ***************