Commit a2463c3718d141b3b26ba48a701eefdb64a7eaf0

Authored by 韩亚云
1 parent eb4cbe50

处理断网后各个控件的点击事件

.gitignore
... ... @@ -6,6 +6,3 @@
6 6 /build
7 7 /captures
8 8 .externalNativeBuild
9   -.README.md
10   -
11   -README.md
... ...
README.md deleted 100644 → 0
1   -# DownloadFile
2   -Android OkHttp 下载多个文件 断点下载
3   -## 介绍
4   -demo的主要逻辑是,利用okhttp 和 RxJava 在子线程中下载文件,通关观察者模式监听下载的进度,再回调到主线程中,然后利用EventBus 通知页面刷新,更新进度。
5   -### step1 导入依赖库
6   -```Java
7   -// OKHttp RxJava
8   - implementation 'com.squareup.okhttp3:okhttp:3.6.0'
9   - implementation 'io.reactivex.rxjava2:rxjava:2.1.3'
10   - implementation 'io.reactivex.rxjava2:rxandroid:2.0.1'
11   - // eventbus
12   - implementation 'org.greenrobot:eventbus:3.0.0'
13   - implementation 'com.android.support:recyclerview-v7:27.1.1'
14   - implementation 'com.google.code.gson:gson:2.8.1'
15   - //数据保存
16   - implementation 'org.greenrobot:greendao:3.2.2'
17   -```
18   -### step 2 定义下载bean
19   -```Java DownloadInfo
20   -
21   -### step 3 DownLoadService 开启一个服务,进行删除,下载,暂停,等待等操作
22   -
23   -### step 4 下载管理类 也是主要的内容 (DownloadManager)具体的数据保存方法
24   -```Java
25   - public void download(final DownloadInfo downloadInfo) {
26   - List<DownloadInfo> list = MyApplication.getInstances().getDaoSession().loadAll(DownloadInfo.class);
27   - downWait.clear();
28   - downWait = list;
29   - if (currentDownInfo != null) pauseDownload(currentDownInfo, true);
30   - if (downCalls.containsKey(downloadInfo.getUrl())) downCalls.remove(downloadInfo.getUrl());
31   - Observable.just(downloadInfo.getUrl())
32   - .filter(new Predicate<String>() { // 过滤 call的map中已经有了,就证明正在下载,则这次不下载
33   - @Override
34   - public boolean test(String s) {
35   - boolean flag = downCalls.containsKey(s);
36   - if (flag) {
37   - // 如果已经在下载,并且不是暂停的数据,查找下一个文件进行下载
38   - downNext();
39   - return false;
40   - } else {
41   - // 判断如果正在下载的个数达到最大限制,存到等下下载列表中
42   - return true;
43   - }
44   - }
45   - })
46   - .flatMap(new Function<String, ObservableSource<?>>() { // 生成 DownloadInfo
47   - @Override
48   - public ObservableSource<?> apply(String s) {
49   - return Observable.just(createDownInfo(downloadInfo));
50   - }
51   - })
52   - .map(new Function<Object, DownloadInfo>() { // 如果已经下载,重新命名
53   - @Override
54   - public DownloadInfo apply(Object o) {
55   - return getRealFileName((DownloadInfo) o);
56   - }
57   - })
58   - .flatMap(new Function<DownloadInfo, ObservableSource<DownloadInfo>>() { // 下载
59   - @Override
60   - public ObservableSource<DownloadInfo> apply(DownloadInfo downloadInfo) {
61   - currentDownInfo = downloadInfo;
62   - return Observable.create(new DownloadSubscribe(downloadInfo));
63   - }
64   - })
65   - .observeOn(AndroidSchedulers.mainThread()) // 在主线程中回调
66   - .subscribeOn(Schedulers.io()) // 在子线程中执行
67   - .subscribe(new DownloadLimitObserver()); // 添加观察者,监听下载进度
68   - }
69   -
70   -```
71   -这里用到的是Rxjava,第一步filter,过滤下载,已经下载的url再点击下载是不会另起下载任务的;第二步flatMap,通过url生成Bean类,这个按需求来设计,也可以直接传一个Bean进来也是可以的;第三步map,如果这个文件已经下载了,再次下载重新命名文件,这也是根据需求改变,如果下载过的文件不需要下载,这就可以省了;第四步flatMap,去下载具体去看下载方法;剩下的就是切换线程和添加观察者了。
72   -### step4 观察者
73   -```Java
74   -public class DownloadObserver implements Observer<DownloadInfo> {
75   -
76   - public Disposable d;//可以用于取消注册的监听者
77   - public DownloadInfo downloadInfo;
78   -
79   - @Override
80   - public void onSubscribe(Disposable d) {
81   - this.d = d;
82   - }
83   -
84   - @Override
85   - public void onNext(DownloadInfo value) {
86   - this.downloadInfo = value;
87   - downloadInfo.setDownloadStatus(DownloadInfo.DOWNLOAD);
88   - EventBus.getDefault().post(downloadInfo);
89   - }
90   -
91   - @Override
92   - public void onError(Throwable e) {
93   -
94   - }
95   -
96   - @Override
97   - public void onComplete() {
98   - if (downloadInfo != null){
99   - downloadInfo.setDownloadStatus(DownloadInfo.DOWNLOAD_OVER);
100   - EventBus.getDefault().post(downloadInfo);
101   - }
102   - }
103   -}
104   -
105   -```
106   -这里是用到EventBus来通知页面刷新
app/src/main/java/com/cnlive/downloadfile/LimitActivity.java
... ... @@ -82,7 +82,6 @@ public class LimitActivity extends AppCompatActivity implements View.OnClickList
82 82 public void update(DownloadInfo info) {
83 83 if (DownloadStatus.DOWNLOAD.equals(info.getDownloadStatus())) {
84 84 allStartPause.setText("全部暂停");
85   - Log.e("ssssss", "update: "+"dowing");
86 85 mAdapter.updateProgress(info);
87 86 } else if (DownloadStatus.DOWNLOAD_OVER.equals(info.getDownloadStatus())) {
88 87 for (int i = 0; i < mData.size(); i++) {
... ... @@ -127,21 +126,7 @@ public class LimitActivity extends AppCompatActivity implements View.OnClickList
127 126 public void onClick(View v) {
128 127 switch (v.getId()) {
129 128 case R.id.start_pause:
130   - if(NetWorkUtil.getNetworkConnectionType(LimitActivity.this).equals(TYPE_UN_NETWORK)){
131   - Toast.makeText(LimitActivity.this,"无网络",Toast.LENGTH_SHORT).show();
132   - return;
133   - }
134   -
135   - if(NetWorkUtil.getNetworkConnectionType(LimitActivity.this).equals(TYPE_WIFI)){
136   - allPause();
137   - return;
138   - }
139   -
140   - if(NetWorkUtil.getNetworkConnectionType(LimitActivity.this).equals(TYPE_MOBILE)){
141   - //弹框...保存用户习惯,拒绝或者是允许,允许的话是仅允许一次还是都允许
142   -
143   - }
144   -
  129 + allPause();
145 130 break;
146 131 case R.id.clear:
147 132 Intent intent = new Intent(LimitActivity.this, DownLoadService.class);
... ... @@ -155,25 +140,13 @@ public class LimitActivity extends AppCompatActivity implements View.OnClickList
155 140  
156 141 private void allPause(){
157 142 if (allStartPause.getText().toString().equals("全部开始")) {
158   - allStartPause.setText("全部暂停");
159   - Log.e("ssssss", "update: "+"全部暂停");
160 143 Intent intent = new Intent(LimitActivity.this, DownLoadService.class);
161 144 intent.setAction(DownLoadService.ACTION_ALLSTART);
162 145 startService(intent);
163   - for(int i = 0;i < mData.size();i++){
164   - mData.get(i).setDownloadStatus(DownloadStatus.DOWNLOAD_WAIT);
165   - }
166   - mAdapter.notifyDataSetChanged();
167 146 } else {
168   - //全部暂停,先进行暂停,再开始
169   - Log.e("ssssss", "update: "+"全部开始");
170 147 Intent intent = new Intent(LimitActivity.this, DownLoadService.class);
171 148 intent.setAction(DownLoadService.ACTION_ALLPAUSE);
172 149 startService(intent);
173   - for(int i = 0;i < mData.size();i++){
174   - mData.get(i).setDownloadStatus(DownloadStatus.DOWNLOAD_PAUSE);
175   - }
176   - mAdapter.notifyDataSetChanged();
177 150 }
178 151 }
179 152 }
... ...
app/src/main/java/com/cnlive/downloadfile/download/limit/DownLoadService.java
... ... @@ -11,6 +11,13 @@ import android.net.NetworkInfo;
11 11 import android.os.Handler;
12 12 import android.os.IBinder;
13 13 import android.os.Message;
  14 +import android.widget.Toast;
  15 +
  16 +import com.cnlive.downloadfile.download.util.NetWorkUtil;
  17 +
  18 +import static com.cnlive.downloadfile.download.util.CheckNetworkStatusChangeListener.Status.TYPE_MOBILE;
  19 +import static com.cnlive.downloadfile.download.util.CheckNetworkStatusChangeListener.Status.TYPE_UN_NETWORK;
  20 +import static com.cnlive.downloadfile.download.util.CheckNetworkStatusChangeListener.Status.TYPE_WIFI;
14 21  
15 22 /**
16 23 * 下载服务
... ... @@ -34,26 +41,40 @@ public class DownLoadService extends Service {
34 41 }else {
35 42 return super.onStartCommand(intent, flags, startId);
36 43 }
37   - if (intent != null && intent.getAction().equals(ACTION_START)) {
38   - //启动初始化线程
39   - new MyThread(info).start();
40   - }else if(intent != null && intent.getAction().equals(ACTION_PAUSE)){
41   - //暂停
42   - mHandle.obtainMessage(1, info).sendToTarget();
43   - }else if(intent != null && intent.getAction().equals(ACTION_ALLPAUSE)){
44   - //全部暂停
45   - mHandle.obtainMessage(2).sendToTarget();
46   - }else if(intent != null && intent.getAction().equals(ACTION_ALLSTART)){
47   - //全部开始
48   - mHandle.obtainMessage(3).sendToTarget();
49   - }else if(intent != null && intent.getAction().equals(ACTION_DELECT)){
  44 +
  45 + if(intent.getAction().equals(ACTION_DELECT)){
50 46 //取消
51 47 mHandle.obtainMessage(4, info).sendToTarget();
52   - }else if(intent != null && intent.getAction().equals(ACION_CLEAR)){
  48 + }else if(intent.getAction().equals(ACION_CLEAR)){
53 49 //清空
54 50 mHandle.obtainMessage(5).sendToTarget();
55   - }else if(intent != null && intent.getAction().equals(ACTION_WAITING)){
56   - mHandle.obtainMessage(6,info).sendToTarget();
  51 + }
  52 +
  53 + if(NetWorkUtil.getNetworkConnectionType(this).equals(TYPE_UN_NETWORK)){
  54 + Toast.makeText(this,"无网络",Toast.LENGTH_SHORT).show();
  55 + return super.onStartCommand(intent, flags, startId);
  56 + }
  57 +
  58 + if(NetWorkUtil.getNetworkConnectionType(this).equals(TYPE_WIFI)){
  59 + if (intent.getAction().equals(ACTION_START)) {
  60 + //启动初始化线程
  61 + new MyThread(info).start();
  62 + }else if(intent.getAction().equals(ACTION_PAUSE)){
  63 + //暂停
  64 + mHandle.obtainMessage(1, info).sendToTarget();
  65 + }else if(intent.getAction().equals(ACTION_ALLPAUSE)){
  66 + //全部暂停
  67 + mHandle.obtainMessage(2).sendToTarget();
  68 + }else if(intent.getAction().equals(ACTION_ALLSTART)){
  69 + //全部开始
  70 + mHandle.obtainMessage(3).sendToTarget();
  71 + }else if(intent.getAction().equals(ACTION_WAITING)){
  72 + mHandle.obtainMessage(6,info).sendToTarget();
  73 + }
  74 + }
  75 +
  76 + if(NetWorkUtil.getNetworkConnectionType(this).equals(TYPE_MOBILE)){
  77 + //弹框...保存用户习惯,拒绝或者是允许,允许的话是仅允许一次还是都允许
57 78 }
58 79 return super.onStartCommand(intent, flags, startId);
59 80 }
... ...
app/src/main/java/com/cnlive/downloadfile/download/limit/DownloadLimitManager.java
... ... @@ -234,10 +234,10 @@ public class DownloadLimitManager {
234 234 downCalls.put(downWait.get(i).getUrl(),null);
235 235 }
236 236 downWait.get(i).setDownloadStatus(DownloadStatus.DOWNLOAD_PAUSE);//改变状态
  237 + EventBus.getDefault().post(downWait.get(i));//通知页面
237 238 MyApplication.getInstances().getDaoSession().update(downWait.get(i));//更新数据库状态
238 239 }
239 240 if(currentDownInfo != null){
240   - EventBus.getDefault().post(currentDownInfo);//通知页面
241 241 currentDownInfo = null;
242 242 }
243 243 }
... ... @@ -254,7 +254,7 @@ public class DownloadLimitManager {
254 254 for (int i = 0; i < downWait.size(); i++) {
255 255 downWait.get(i).setDownloadStatus(DownloadStatus.DOWNLOAD_WAIT);//改变状态
256 256 MyApplication.getInstances().getDaoSession().update(downWait.get(i));//更新数据库状态
257   -// EventBus.getDefault().post(downWait.get(i));//通知页面
  257 + EventBus.getDefault().post(downWait.get(i));//通知页面
258 258 }
259 259 download(downWait.get(0));
260 260 }
... ...