今天敲了一个断点续传
说的直白一点,其实就是在读取循环中设卡,并且在继续的时候能拿到之前暂停时的位置,并用自由文件流进行seek,就可以继续了,清楚了原理就可以直接动手了哦
布局
<ProgressBar
android:layout_above="@id/ll"
android:id="@+id/prb"
style="@style/Widget.AppCompat.ProgressBar.Horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<LinearLayout
android:id="@+id/ll"
android:orientation="vertical"
android:layout_centerInParent="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="@+id/text123456"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:text="开始"
android:id="@+id/btn"
android:layout_centerInParent="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:layout_alignParentBottom="true"
android:text="暂停"
android:id="@+id/btn1"
android:layout_centerHorizontal="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
活动代码
public class MainActivity extends AppCompatActivity {
TextView textView;
int sum = 0;
boolean flag = true;
ProgressBar progressBar
int max;
int time = 0;
long start = 0;
long end = 1024 * 1024;
Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
if (msg.what == 100 && msg.obj != null&&flag==true) {
if (time < 5) {
new DownloadThread().start();
time++;
Log.d("####", msg.obj.toString());
} else {
handler.sendEmptyMessage(1);
Log.d("####", msg.obj.toString());
}
} else if (msg.what == 1) {
}else if(msg.what==200){
textView.setText(msg.obj.toString());
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView=findViewById(R.id.text123456);
progressBar = findViewById(R.id.prb);
Button button = findViewById(R.id.btn);
Button button1 = findViewById(R.id.btn1);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
flag = true;
start=sum;
new DownloadThread().start();
}
});
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
flag = false;
}
});
}
class DownloadThread extends Thread {
@Override
public void run() {
super.run();
try {
URL url = new URL("http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4");
URL url1 = new URL("http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4");
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
HttpURLConnection httpURLConnection1 = (HttpURLConnection) url1.openConnection();
int contentLength = httpURLConnection1.getContentLength();
progressBar.setMax(contentLength);
httpURLConnection.setRequestMethod("GET");
httpURLConnection.setDoInput(true);
httpURLConnection.setDoOutput(true);
String path = Environment.getExternalStorageDirectory().getPath() + "/test.mp4";
RandomAccessFile randomAccessFile = new RandomAccessFile(path, "rw");
randomAccessFile.seek(start);
httpURLConnection.setRequestProperty("Range", "bytes=" + start + "-" + end);
if (httpURLConnection.getResponseCode() == 206) {
max = httpURLConnection.getContentLength();
// 总长度 range bytes=0-1024*1024
InputStream inputStream = httpURLConnection.getInputStream();
byte b[] = new byte[1024];
int len = 0;
while ((len = inputStream.read(b)) != -1) {
randomAccessFile.write(b, 0, len);
sum += len;
progressBar.setProgress(sum);
if (flag == false) {
break;
}
String s = sum * 100 / progressBar.getMax() + "%";
Message message = Message.obtain();
message.what = 200;
message.obj = s;
handler.sendMessage(message);
}
//从上次结束的位置+1开始读
start = end + 1;
end += 1024 * 1024;
Message message = Message.obtain();
message.what = 100;
String str = "文件" + time + "下载成功:" + start + "-" + end;
Log.d("###", "run: " + str);
message.obj = str;
handler.sendMessage(message);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
INTERESTING!!!