下载线程:
public class MyThread extends Thread {
@RequiresApi ( api = Build. VERSION_CODES. N)
@Override
public void run ( ) {
try {
URL url = new URL ( "https://media.w3.org/2010/05/sintel/trailer.mp4" ) ;
HttpURLConnection connection = ( HttpURLConnection) url. openConnection ( ) ;
connection. setRequestMethod ( "GET" ) ;
connection. setDoInput ( true ) ;
connection. setDoOutput ( true ) ;
connection. setRequestProperty ( "Range" , "bytes=" + start+ "-" + end) ;
Log. e ( "###" , "判断" ) ;
if ( connection. getResponseCode ( ) == 206 ) {
Log. e ( "###" , "进来了" ) ;
int length = connection. getContentLength ( ) ;
String path = Environment. getExternalStorageDirectory ( ) . getAbsolutePath ( ) + "/" + "video" + ".mp4" ;
RandomAccessFile randomAccessFile = new RandomAccessFile ( path, "rw" ) ;
Log. e ( "START###" , start+ "" ) ;
randomAccessFile. seek ( start) ;
InputStream is = connection. getInputStream ( ) ;
byte [ ] b = new byte [ 1024 ] ;
int len = - 1 ;
while ( ( len = is. read ( b) ) != - 1 && flag) {
Message m = new Message ( ) ;
randomAccessFile. write ( b, 0 , len) ;
sum+= len;
m. arg1 = sum;
m. what = 3 ;
handler. sendMessage ( m) ;
}
Log. e ( "end###" , end+ "" ) ;
if ( randomAccessFile != null) {
randomAccessFile. close ( ) ;
}
if ( is != null) {
is. close ( ) ;
}
Message message = new Message ( ) ;
if ( flag) {
message. what = 1 ;
start = end+ 1 ;
end += 1024 * 1024 ;
} else {
message. what = 0 ;
start = sum;
}
handler. sendMessage ( message) ;
}
} catch ( Exception e) {
e. printStackTrace ( ) ;
}
}
}
获取进度条最大值:
public class PbThread extends Thread {
@Override
public void run ( ) {
URL url = null;
try {
url = new URL ( "https://media.w3.org/2010/05/sintel/trailer.mp4" ) ;
HttpURLConnection connection = ( HttpURLConnection) url. openConnection ( ) ;
int length = connection. getContentLength ( ) ;
Log. e ( "length###" , length+ "" ) ;
Message message = new Message ( ) ;
message. what = 2 ;
message. arg1 = length;
handler. sendMessage ( message) ;
} catch ( Exception e) {
e. printStackTrace ( ) ;
}
}
}
handler类方法重写:
Handler handler = new Handler ( ) {
@Override
public void handleMessage ( Message msg) {
super . handleMessage ( msg) ;
if ( msg. what == 1 ) {
new MyThread ( ) . start ( ) ;
} else if ( msg. what == 0 ) {
Log. e ( "###pause" , "暂停" ) ;
} else if ( msg. what == 2 ) {
pb. setMax ( msg. arg1) ;
} else if ( msg. what == 3 ) {
pb. setProgress ( msg. arg1) ;
}
}
} ;
主类:
@Override
protected void onCreate ( Bundle savedInstanceState) {
super . onCreate ( savedInstanceState) ;
setContentView ( R. layout. activity_baidu_cloud) ;
initView ( ) ;
}
private void initView ( ) {
findViewById ( R. id. btn1) . setOnClickListener ( this ) ;
findViewById ( R. id. pause) . setOnClickListener ( this ) ;
findViewById ( R. id. again) . setOnClickListener ( this ) ;
pb = findViewById ( R. id. pb) ;
}
按钮监听方法:
@Override
public void onClick ( View v) {
switch ( v. getId ( ) ) {
case R. id. btn1:
new MyThread ( ) . start ( ) ;
new PbThread ( ) . start ( ) ;
break ;
case R. id. pause:
flag = false ;
break ;
case R. id. again:
flag = true ;
new MyThread ( ) . start ( ) ;
break ;
}
}