編輯:關於Android編程
這裡利用 ProgressBar 即時顯示下載進度。
途中碰到的問題:
1、主線程中不能打開 URL,和只能在主線程中使用 Toast 等
2、子線程不能修改 UI
3、允許網絡協議
4、暫停下載和繼續下載
........
fragment_main 布局文件
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.dragon.android.textbar.MainActivity$PlaceholderFragment" >
<!-- prigressBar 進度條 -->
<!-- progress 當前進度 -->
<!-- indeterminate 不明確的 默認false -->
<ProgressBar
android:id="@+id/progressBar1"
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:id="@+id/button1"
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:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/progressBar1"
android:background="@null"
android:layout_alignParentLeft="true" />
</RelativeLayout>
strings.xml
<?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">開始</string> <string name="stop">暫停</string> <string name="contin">繼續</string> </resources>
(問題3)在 AndroidManifest 文件中配置
<!-- 請求網絡權限 -->
<uses-permission android:name="android.permission.INTERNET"/>
MainActivity(問題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;
/**
* 只有創建一個 View 的線程才可以改變這個 View 的UI!!! 主線程也叫 UI 線程
*/
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);
textView1 = (TextView) findViewById(R.id.textView1);
}
public void startLoad(View view) {
String text = (String) button1.getText();
// 設置按鈕內容 ----並沒有用...
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 {
// 打開 URL 必須在子線程
URL 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);
// conn.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;
// 注意強轉方式,防止一直為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,
"下載完成!", Toast.LENGTH_SHORT)
.show();
}
}
});
}
is.close();
conn.disconnect();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
}
}
**************然而並沒有解決問題4,要用斷點續傳,但是還不會存放assets資源.....***************
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持本站。
android 百度地圖系列之添加覆蓋物和覆蓋物的點擊事件
之前講了百度地圖定位和地圖基本操作,這篇博客講一下,怎麼去給地圖添加覆蓋物,並當點擊覆蓋物的時候顯示詳細信息。要給地圖添加覆蓋物,首先需要覆蓋物的經緯度,如果還要實現點擊
詳解Android6.0運行時權限管理
自從Android6.0發布以來,在權限上做出了很大的變動,不再是之前的只要在manifest設置就可以任意獲取權限,而是更加的注重用戶的隱私和體驗,不會再強迫用戶因拒絕
Android開發教程之shape和selector的結合使用
shape和selector是Android UI設計中經常用到的,比如我們要自定義一個圓角Button,點擊Button有些效果的變化,就要用到shape和select
簡析Android多種AlertDialog對話框效果
android提供了四類常用的對話框,本文分享具體實現方法: 1.AlertDialog,功能最豐富,實際運用最廣泛 2.progressDialog,進度條對