Android之ProgressBar即时显示下载进度详解
这里利用ProgressBar即时显示下载进度。
途中碰到的问题:
1、主线程中不能打开URL,和只能在主线程中使用Toast等
2、子线程不能修改UI
3、允许网络协议
4、暂停下载和继续下载
........
fragment_main布局文件
<RelativeLayoutxmlns: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" 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: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
<?xmlversion="1.0"encoding="utf-8"?> <resources> <stringname="app_name">hwdownload</string> <stringname="hello_world">Helloworld!</string> <stringname="action_settings">Settings</string> <stringname="start">开始</string> <stringname="stop">暂停</string> <stringname="contin">继续</string> </resources>
(问题3)在AndroidManifest文件中配置
<!--请求网络权限-->
<uses-permission android:name="android.permission.INTERNET"/>
MainActivity(问题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;
/**
*只有创建一个View的线程才可以改变这个View的UI!!!主线程也叫UI线程
*/
publicclassMainActivityextendsActivity{
privateProgressBarprogressBar1;
privateButtonbutton1;
privateTextViewtextView1;
@Override
protectedvoidonCreate(BundlesavedInstanceState){
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);
}
publicvoidstartLoad(Viewview){
Stringtext=(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);
newThread(newRunnable(){
privateintpercent;
@Override
publicvoidrun(){
try{
//打开URL必须在子线程
URLurl=newURL(
"http://b.zol-img.com.cn/sjbizhi/images/9/540x960/1472549276394.jpg");
HttpURLConnectionconn=(HttpURLConnection)url.openConnection();
//conn.setRequestMethod("GET");
//conn.setReadTimeout(5000);
//conn.setConnectTimeout(5000);
intcontentLength=conn.getContentLength();
if(conn.getResponseCode()==HttpURLConnection.HTTP_OK){
InputStreamis=conn.getInputStream();
byte[]buffer=newbyte[1024];
intlen=-1;
intsum=0;
while((len=is.read(buffer))!=-1){
sum+=len;
//注意强转方式,防止一直为0
percent=(int)(100.0*sum/contentLength);
//在主线程上运行的子线程
runOnUiThread(newRunnable(){
@Override
publicvoidrun(){
progressBar1.setProgress(percent);
textView1.setText(percent+"%");
if(percent==progressBar1.getMax()){
Toast.makeText(MainActivity.this,
"下载完成!",Toast.LENGTH_SHORT)
.show();
}
}
});
}
is.close();
conn.disconnect();
}
}catch(IOExceptione){
e.printStackTrace();
}
}
}).start();
}
}
**************然而并没有解决问题4,要用断点续传,但是还不会存放assets资源.....***************
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。