DownLoadService.java
5.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package com.cnlive.downloadfile.download.limit;
import android.annotation.SuppressLint;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
/**
* 下载服务
*/
public class DownLoadService extends Service {
private NetConnectionReceiver netConnectionReceiver;
private DownloadInfo info = null;
public static final String ACTION_START = "ACTION_START";
public static final String ACTION_WAITING = "ACTION_WAITING";
public static final String ACTION_PAUSE = "ACTION_PAUSE";
public static final String ACTION_ALLPAUSE = "ACTION_ALLPAUSE";
public static final String ACTION_ALLSTART = "ACTION_ALLSTRAT";
public static final String ACTION_DELECT = "ACTION_DELECT";
public static final String ACION_CLEAR = "ACION_CLEAR";
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
//判断Intent的值,获得Activity传来的参数
if(intent != null){
info = (DownloadInfo) intent.getSerializableExtra("fileUrl");
}else {
return super.onStartCommand(intent, flags, startId);
}
if (intent != null && intent.getAction().equals(ACTION_START)) {
//启动初始化线程
new MyThread(info).start();
}else if(intent != null && intent.getAction().equals(ACTION_PAUSE)){
//暂停
mHandle.obtainMessage(1, info).sendToTarget();
}else if(intent != null && intent.getAction().equals(ACTION_ALLPAUSE)){
//全部暂停
mHandle.obtainMessage(2).sendToTarget();
}else if(intent != null && intent.getAction().equals(ACTION_ALLSTART)){
//全部开始
mHandle.obtainMessage(3).sendToTarget();
}else if(intent != null && intent.getAction().equals(ACTION_DELECT)){
//取消
mHandle.obtainMessage(4, info).sendToTarget();
}else if(intent != null && intent.getAction().equals(ACION_CLEAR)){
//清空
mHandle.obtainMessage(5).sendToTarget();
}else if(intent != null && intent.getAction().equals(ACTION_WAITING)){
mHandle.obtainMessage(6,info).sendToTarget();
}
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onCreate() {
super.onCreate();
IntentFilter filter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);
netConnectionReceiver = new NetConnectionReceiver();
registerReceiver(netConnectionReceiver, filter);
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onDestroy() {
super.onDestroy();
unregisterReceiver(netConnectionReceiver);
}
//用于Service和MyThread之间的通信
@SuppressLint("HandlerLeak")
Handler mHandle = new Handler() {
@Override
public void handleMessage(Message msg) {
DownloadInfo info = (DownloadInfo) msg.obj;
switch (msg.what) {
case 0:
//启动一个下载任务
DownloadLimitManager.getInstance().download(info);
break;
case 1://暂停一个
DownloadLimitManager.getInstance().pauseDownload(info,false);
break;
case 2://全部暂停
DownloadLimitManager.getInstance().pauseAllDownload();
break;
case 3:
DownloadLimitManager.getInstance().startAllDownload();
break;
case 4:
DownloadLimitManager.getInstance().cancelDownload(info,true);
break;
case 5:
DownloadLimitManager.getInstance().clearDownload();
break;
case 6:
DownloadLimitManager.getInstance().pauseToWaitDownload(info);
break;
}
}
};
//子线程 获取文件长度
class MyThread extends Thread {
private DownloadInfo fileInfo = null;
public MyThread(DownloadInfo fileInfo) {
this.fileInfo = fileInfo;
}
public void run() {
mHandle.obtainMessage(0, fileInfo).sendToTarget();
}
}
private class NetConnectionReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo mobNetInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
NetworkInfo wifiNetInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
if (!mobNetInfo.isConnected() && !wifiNetInfo.isConnected()) {
//网络断开
if(DownloadLimitManager.getInstance().getCurremtDownload() !=null && DownloadLimitManager.getInstance().getCurremtDownload().getUrl() != null){
DownloadLimitManager.getInstance().pauseErrorDownload();
}
} else {
//网络连接
//启动初始化线程
if(DownloadLimitManager.getInstance().getCurremtDownload() !=null && DownloadLimitManager.getInstance().getCurremtDownload().getUrl() != null){
new MyThread(DownloadLimitManager.getInstance().getCurremtDownload()).start();
}
}
}
}
}