1.首先配置GreenDao网上好多博客,参照就好了
2.配置网络权限及其他依赖
3.布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.ccm.morethreaddownload.activity.MainActivity">
<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="下载"
/>
<ProgressBar
android:id="@+id/pro"
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="?android:attr/progressBarStyleHorizontal"
android:layout_margin="20dp"
/>
<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
4.GreenDao的bean实体类
@Entity
public class Bean {
@Id(autoincrement = true)
long id;
String downpath;
int threadid;
int downlength;
public int getDownlength() {
return this.downlength;
}
public void setDownlength(int downlength) {
this.downlength = downlength;
}
public int getThreadid() {
return this.threadid;
}
public void setThreadid(int threadid) {
this.threadid = threadid;
}
public String getDownpath() {
return this.downpath;
}
public void setDownpath(String downpath) {
this.downpath = downpath;
}
public long getId() {
return this.id;
}
public void setId(long id) {
this.id = id;
}
@Generated(hash = 635468303)
public Bean(long id, String downpath, int threadid, int downlength) {
this.id = id;
this.downpath = downpath;
this.threadid = threadid;
this.downlength = downlength;
}
@Generated(hash = 80546095)
public Bean() {
}
}
public class MainActivity extends AppCompatActivity {
private FileDownloader loader;
//声明控件
@InjectView(R.id.btn)
Button btn;
@InjectView(R.id.pro)
ProgressBar progressBar;
@InjectView(R.id.tv)
TextView tv;
/**
* 当Handler被创建会关联到创建它的当前线程的消息队列,该类用于往消息队列发送消息
* 消息队列中的消息由当前线程内部进行处理
*/
private Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case 1:
long size = msg.getData().getInt("size");
long fileSize = loader.getFileSize();
float v = size * 100 / fileSize;
progressBar.setProgress((int) size);
tv.setText(v+"%");
if(progressBar.getProgress()==progressBar.getMax()){
Toast.makeText(MainActivity.this, R.string.success, Toast.LENGTH_SHORT).show();
}
break;
case -1:
Toast.makeText(MainActivity.this, R.string.error, Toast.LENGTH_SHORT).show();
break;
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//初始化
ButterKnife.inject(this);
//切换文字
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(btn.getText().toString().equals("下载")){
btn.setText("