Here we use ProgressBar to display the download progress instantly.
Problems encountered on the way:
1. The URL cannot be opened in the main thread, and the Toast can only be used in the main thread.
2. The child thread cannot be modified UI
3. Allow network protocols
4. Pause and continue downloading
........
fragment_main layout file
<RelativeLayout xmlns:andro xmlns:tools="/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="$PlaceholderFragment" > <!-- prigressBar Progress bar --> <!-- progress Current progress --> <!-- indeterminate Not clear defaultfalse --> <ProgressBar android: style="?android:attr/progressBarStyleHorizontal" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_centerInParent="true" android:max="100" android:progress="0" android:indeterminate="true"/> <Button android: android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:onClick="startLoad" android:layout_marginTop="86dp" android:background="#009FEE" android:text="@string/start" android:textColor="#ffffff" /> <TextView android: android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_above="@+id/progressBar1" android:background="@null" android:layout_alignParentLeft="true" /> </RelativeLayout>
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">hwdownload</string> <string name="hello_world">Hello world!</string> <string name="action_settings">Settings</string> <string name="start">start</string> <string name="stop">pause</string> <string name="contin">continue</string> </resources>
(Question 3) Configure in AndroidManifest file
<!-- Request network permissions -->
<uses-permission android:name=""/>
MainActivity (Question 1, 2)
package ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; /** * Only by creating a View thread can you change the UI of this View!!! The main thread is also called UI thread */ public class MainActivity extends Activity { private ProgressBar progressBar1; private Button button1; private TextView textView1; @Override protected void onCreate(Bundle savedInstanceState) { (savedInstanceState); setContentView(.fragment_main); progressBar1 = (ProgressBar) findViewById(.progressBar1); button1 = (Button) findViewById(.button1); textView1 = (TextView) findViewById(.textView1); } public void startLoad(View view) { String text = (String) (); // Set button content ----It's useless... ((getResources().getString()) ? : ((getResources().getString()) ? : )); (false); new Thread(new Runnable() { private int percent; @Override public void run() { try { // Open URL must be in the child thread URL url = new URL( "/sjbizhi/images/9/540x960/"); HttpURLConnection conn = (HttpURLConnection) (); // ("GET"); // (5000); // (5000); int contentLength = (); if (() == HttpURLConnection.HTTP_OK) { InputStream is = (); byte[] buffer = new byte[1024]; int len = -1; int sum = 0; while ((len = (buffer)) != -1) { sum += len; // Pay attention to the strong rotation method to prevent it from being 0 all the time percent = (int) (100.0 * sum / contentLength); // Child thread running on the main thread runOnUiThread(new Runnable() { @Override public void run() { (percent); (percent + "%"); if (percent == ()) { (, "Download is complete!", Toast.LENGTH_SHORT) .show(); } } }); } (); (); } } catch (IOException e) { (); } } }).start(); } }
****************** However, the problem 4 is not solved. We need to use a breakpoint to continue transmission, but the assets resources are not stored...************************
The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.