android 多線程斷點續(xù)傳下載 四 - 仿下載助手

文章出處
我們先一起簡單回顧下它的基本原理。
http://m.itdecent.cn/p/cfae9a285298 android 多線程斷點續(xù)傳下載 一
[http://m.itdecent.cn/p/a83eb6c3ccb6]
(http://m.itdecent.cn/p/a83eb6c3ccb6) android 多線程斷點續(xù)傳下載 二
http://m.itdecent.cn/p/6464e21f1038 android 多線程斷點續(xù)傳下載 三
界面效果

線程池ThreadPoolExecutor

? 在下面介紹實現(xiàn)下載原理的時候,我想嘗試倒著來說,這樣是否好理解一點? 我們都知道,下載助手,比如360, 百度的 手機助手,下載APP 的時候 ,都可以同時下載多個,所以,下載肯定是多線程的,所以我們就需要一個線程工具類 來管理我們的線程,這個工具類的核心,就是 線程池。 線程池ThreadPoolExecutor ,先簡單學(xué)習(xí)下這個線程池的使用

/** 
* Parameters: 
corePoolSize  
the number of threads to keep in the pool, even if they are idle, unless allowCoreThreadTimeOut is set 
maximumPoolSize  
the maximum number of threads to allow in the pool 
keepAliveTime 
when the number of threads is greater than the core, this is the maximum time that excess idle threads will wait for new tasks before terminating. 
unit  
the time unit for the keepAliveTime argument 
workQueue  
the queue to use for holding tasks before they are executed. This queue will hold only the Runnable tasks submitted                   by the execute method. 
handler  
the handler to use when execution is blocked because the thread bounds and queue capacities are reached 
Throws: 
IllegalArgumentException - if one of the following holds: 
corePoolSize < 0 
keepAliveTime < 0 
maximumPoolSize <= 0 
maximumPoolSize < corePoolSize 
NullPointerException - if workQueue or handler is null 
*/  
ThreadPoolExecutor threadpool=new ThreadPoolExecutor(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, hand

上面是 ThreadPoolExecutor的參數(shù)介紹
第一個參數(shù) corePoolSize : 空閑時 存在的線程數(shù)目
第二個參數(shù) maximumPoolSize :允許同時存在的最大線程數(shù)
第三個參數(shù) keepAliveTime: 這個參數(shù)是 允許空閑線程存活的時間
第四個參數(shù) unit : 是 時間的單位
第五個參數(shù) workQueue :這個是一個容器,它里面存放的是、 threadpool.execute(new Runnable()) 執(zhí)行的線程.new Runnable()
第六個參數(shù) handler:當(dāng)執(zhí)行被阻塞時,該處理程序?qū)⒈蛔枞?,因為線程的邊界和隊列容量達到了 。

工具類 ThreadManager

介紹完了 線程池參數(shù),那我們就先創(chuàng)建一個線程管理的工具類 ThreadManager

public class ThreadManager {  
public static final String DEFAULT_SINGLE_POOL_NAME = "DEFAULT_SINGLE_POOL_NAME";  
 
private static ThreadPoolProxy mLongPool = null;  
private static Object mLongLock = new Object();  
 
private static ThreadPoolProxy mShortPool = null;  
private static Object mShortLock = new Object();  
 
private static ThreadPoolProxy mDownloadPool = null;  
private static Object mDownloadLock = new Object();  
 
private static Map<String, ThreadPoolProxy> mMap = new HashMap<String, ThreadPoolProxy>();  
private static Object mSingleLock = new Object();  
 
/** 獲取下載線程 */  
public static ThreadPoolProxy getDownloadPool() {  
synchronized (mDownloadLock) {  
if (mDownloadPool == null) {  
mDownloadPool = new ThreadPoolProxy(3, 3, 5L);  
}  
return mDownloadPool;  
}  
}  
 
/** 獲取一個用于執(zhí)行長耗時任務(wù)的線程池,避免和短耗時任務(wù)處在同一個隊列而阻塞了重要的短耗時任務(wù),通常用來聯(lián)網(wǎng)操作 */  
public static ThreadPoolProxy getLongPool() {  
synchronized (mLongLock) {  
if (mLongPool == null) {  
mLongPool = new ThreadPoolProxy(5, 5, 5L);  
}  
return mLongPool;  
}  
}  
 
/** 獲取一個用于執(zhí)行短耗時任務(wù)的線程池,避免因為和耗時長的任務(wù)處在同一個隊列而長時間得不到執(zhí)行,通常用來執(zhí)行本地的IO/SQL */  
public static ThreadPoolProxy getShortPool() {  
synchronized (mShortLock) {  
if (mShortPool == null) {  
mShortPool = new ThreadPoolProxy(2, 2, 5L);  
}  
return mShortPool;  
}  
}  
 
/** 獲取一個單線程池,所有任務(wù)將會被按照加入的順序執(zhí)行,免除了同步開銷的問題 */  
public static ThreadPoolProxy getSinglePool() {  
return getSinglePool(DEFAULT_SINGLE_POOL_NAME);  
}  
 
/** 獲取一個單線程池,所有任務(wù)將會被按照加入的順序執(zhí)行,免除了同步開銷的問題 */  
public static ThreadPoolProxy getSinglePool(String name) {  
synchronized (mSingleLock) {  
ThreadPoolProxy singlePool = mMap.get(name);  
if (singlePool == null) {  
singlePool = new ThreadPoolProxy(1, 1, 5L);  
mMap.put(name, singlePool);  
}  
return singlePool;  
}  
}  
 
public static class ThreadPoolProxy {  
private ThreadPoolExecutor mPool;  
private int mCorePoolSize;  
private int mMaximumPoolSize;  
private long mKeepAliveTime;  
 
private ThreadPoolProxy(int corePoolSize, int maximumPoolSize, long keepAliveTime) {  
mCorePoolSize = corePoolSize;  
mMaximumPoolSize = maximumPoolSize;  
mKeepAliveTime = keepAliveTime;  
}  
 
/** 執(zhí)行任務(wù),當(dāng)線程池處于關(guān)閉,將會重新創(chuàng)建新的線程池 */  
public synchronized void execute(Runnable run) {  
if (run == null) {  
return;  
}  
if (mPool == null || mPool.isShutdown()) {  
mPool = new ThreadPoolExecutor(mCorePoolSize, mMaximumPoolSize, mKeepAliveTime, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>(), Executors.defaultThreadFactory(), new AbortPolicy());  
}  
mPool.execute(run);  
}  
 
/** 取消線程池中某個還未執(zhí)行的任務(wù) */  
public synchronized void cancel(Runnable run) {  
if (mPool != null && (!mPool.isShutdown() || mPool.isTerminating())) {  
mPool.getQueue().remove(run);  
}  
}  
 
/** 取消線程池中某個還未執(zhí)行的任務(wù) */  
public synchronized boolean contains(Runnable run) {  
if (mPool != null && (!mPool.isShutdown() || mPool.isTerminating())) {  
return mPool.getQueue().contains(run);  
} else {  
return false;  
}  
}  
 
/** 立刻關(guān)閉線程池,并且正在執(zhí)行的任務(wù)也將會被中斷 */  
public void stop() {  
if (mPool != null && (!mPool.isShutdown() || mPool.isTerminating())) {  
mPool.shutdownNow();  
}  
}  
 
/** 平緩關(guān)閉單任務(wù)線程池,但是會確保所有已經(jīng)加入的任務(wù)都將會被執(zhí)行完畢才關(guān)閉 */  
public synchronized void shutdown() {  
if (mPool != null && (!mPool.isShutdown() || mPool.isTerminating())) {  
mPool.shutdownNow();  
}  
}  
}  
}

這個線程池工具類 主要就是 生成一個線程池, 以及 取消線程池中的任務(wù),查詢線程池中是否包含某一任務(wù)。

下載任務(wù)DownloadTask

? 我們的現(xiàn)在線程 DownloadTask 就 通過 ThreadManager .getDownloadPool().execute() 方法 交給線程池去管理。
? 有了線程池管理我們的線程, 那我們下一步 就是 DownloadTask 這個類去下載了。

/** 下載任務(wù) */  
public class DownloadTask implements Runnable {  
private DownloadInfo info;  
 
public DownloadTask(DownloadInfo info) {  
this.info = info;  
}  
 
@Override  
public void run() {  
info.setDownloadState(STATE_DOWNLOADING);// 先改變下載狀態(tài)  
notifyDownloadStateChanged(info);  
File file = new File(info.getPath());// 獲取下載文件  
HttpResult httpResult = null;  
InputStream stream = null;  
if (info.getCurrentSize() == 0 || !file.exists()  
|| file.length() != info.getCurrentSize()) {  
// 如果文件不存在,或者進度為0,或者進度和文件長度不相符,就需要重新下載  
 
info.setCurrentSize(0);  
file.delete();  
}  
httpResult = HttpHelper.download(info.getUrl());  
// else {  
// // //文件存在且長度和進度相等,采用斷點下載  
// httpResult = HttpHelper.download(info.getUrl() + "&range=" +  
// info.getCurrentSize());  
// }  
if (httpResult == null  
|| (stream = httpResult.getInputStream()) == null) {  
info.setDownloadState(STATE_ERROR);// 沒有下載內(nèi)容返回,修改為錯誤狀態(tài)  
notifyDownloadStateChanged(info);  
} else {  
try {  
skipBytesFromStream(stream, info.getCurrentSize());  
} catch (Exception e1) {  
e1.printStackTrace();  
}  
 
FileOutputStream fos = null;  
try {  
fos = new FileOutputStream(file, true);  
int count = -1;  
byte[] buffer = new byte[1024];  
while (((count = stream.read(buffer)) != -1)  
&& info.getDownloadState() == STATE_DOWNLOADING) {  
// 每次讀取到數(shù)據(jù)后,都需要判斷是否為下載狀態(tài),如果不是,下載需要終止,如果是,則刷新進度  
fos.write(buffer, 0, count);  
fos.flush();  
info.setCurrentSize(info.getCurrentSize() + count);  
notifyDownloadProgressed(info);// 刷新進度  
}  
} catch (Exception e) {  
info.setDownloadState(STATE_ERROR);  
notifyDownloadStateChanged(info);  
info.setCurrentSize(0);  
file.delete();  
} finally {  
IOUtils.close(fos);  
if (httpResult != null) {  
httpResult.close();  
}  
}  
 
// 判斷進度是否和app總長度相等  
if (info.getCurrentSize() == info.getAppSize()) {  
info.setDownloadState(STATE_DOWNLOADED);  
notifyDownloadStateChanged(info);  
} else if (info.getDownloadState() == STATE_PAUSED) {// 判斷狀態(tài)  
notifyDownloadStateChanged(info);  
} else {  
info.setDownloadState(STATE_ERROR);  
notifyDownloadStateChanged(info);  
info.setCurrentSize(0);// 錯誤狀態(tài)需要刪除文件  
file.delete();  
}  
}  
mTaskMap.remove(info.getId());  
}  
}

下載的原理 很簡單,就是 通過 目標(biāo)的URL 拿到流,然后寫到本地。
? 因為下載在 run() 里面執(zhí)行,這個DownloadTask 類 我們就看run() 方法的實現(xiàn),所以 關(guān)鍵代碼 就是下面一點點

fos = new FileOutputStream(file, true);  
int count = -1;  
byte[] buffer = new byte[1024];  
while (((count = stream.read(buffer)) != -1)  
&& info.getDownloadState() == STATE_DOWNLOADING) {  
// 每次讀取到數(shù)據(jù)后,都需要判斷是否為下載狀態(tài),如果不是,下載需要終止,如果是,則刷新進度  
fos.write(buffer, 0, count);  
fos.flush();  
info.setCurrentSize(info.getCurrentSize() + count);  
notifyDownloadProgressed(info);// 刷新進度  
}

這個在我們剛接觸Java 的時候 肯定都寫過了。 這就是往本地寫數(shù)據(jù)的代碼。所以run()方法中的 前面 就是拿到 stream 輸入流, 以及 把file 創(chuàng)建出來。

刷新進度,狀態(tài)

? 關(guān)于控制 button中text 顯示 暫停 ,下載,還是進度,就靠 notifyDownloadProgressed(info);和 notifyDownloadStateChanged(info)兩個方法, 這兩個方法 實際上調(diào)用的是兩個接口,只要我們在我們需要改變界面的類里 實現(xiàn)這兩個接口,就可以接收到 包含最新信息的info對象。而我們在哪個類里改變button 上面 顯示的文字呢? 當(dāng)然是在 我們的adapter 里面了,大家都知道 是在 adapter 的getView() 方法里面 加載的每一條數(shù)據(jù)的布局。

那就一起看下是不是這樣子呢?

public class RecommendAdapter extends BaseAdapter implements  
DownloadManager.DownloadObserver {  
 
ArrayList<AppInfo> list;  
private List<ViewHolder> mDisplayedHolders;  
private FinalBitmap finalBitmap;  
private Context context;  
 
public RecommendAdapter(ArrayList<AppInfo> list, FinalBitmap finalBitmap,  
Context context) {  
this.list = list;  
this.context = context;  
this.finalBitmap = finalBitmap;  
mDisplayedHolders = new ArrayList<ViewHolder>();  
}  
 
 
 
public void startObserver() {  
DownloadManager.getInstance().registerObserver(this);  
}  
 
public void stopObserver() {  
DownloadManager.getInstance().unRegisterObserver(this);  
}  
 
@Override  
public int getCount() {  
return list.size();  
}  
 
@Override  
public Object getItem(int position) {  
return list.get(position);  
}  
 
@Override  
public long getItemId(int position) {  
return position;  
}  
 
@Override  
public View getView(int position, View convertView, ViewGroup parent) {  
final AppInfo appInfo = list.get(position);  
final ViewHolder holder;  
 
if (convertView == null) {  
holder = new ViewHolder(context);  
} else {  
holder = (ViewHolder) convertView.getTag();  
}  
holder.setData(appInfo);  
mDisplayedHolders.add(holder);  
return holder.getRootView();  
}  
 
@Override  
public void onDownloadStateChanged(DownloadInfo info) {  
refreshHolder(info);  
}  
 
@Override  
public void onDownloadProgressed(DownloadInfo info) {  
refreshHolder(info);  
 
}  
 
public List<ViewHolder> getDisplayedHolders() {  
synchronized (mDisplayedHolders) {  
return new ArrayList<ViewHolder>(mDisplayedHolders);  
}  
}  
 
public void clearAllItem() {  
if (list != null){  
list.clear();  
}  
if (mDisplayedHolders != null) {  
mDisplayedHolders.clear();  
}  
}  
 
public void addItems(ArrayList<AppInfo> infos) {  
list.addAll(infos);  
}  
 
private void refreshHolder(final DownloadInfo info) {  
List<ViewHolder> displayedHolders = getDisplayedHolders();  
for (int i = 0; i < displayedHolders.size(); i++) {  
final ViewHolder holder = displayedHolders.get(i);  
AppInfo appInfo = holder.getData();  
if (appInfo.getId() == info.getId()) {  
AppUtil.post(new Runnable() {  
@Override  
public void run() {  
holder.refreshState(info.getDownloadState(),  
info.getProgress());  
}  
});  
}  
}  
 
}  
 
public class ViewHolder {  
public TextView textView01;  
public TextView textView02;  
public TextView textView03;  
public TextView textView04;  
public ImageView imageView_icon;  
public Button button;  
public LinearLayout linearLayout;  
 
public AppInfo mData;  
private DownloadManager mDownloadManager;  
private int mState;  
private float mProgress;  
protected View mRootView;  
private Context context;  
private boolean hasAttached;  
 
public ViewHolder(Context context) {  
mRootView = initView();  
mRootView.setTag(this);  
this.context = context;  
 
 
}  
 
public View getRootView() {  
return mRootView;  
}  
 
public View initView() {  
View view = AppUtil.inflate(R.layout.item_recommend_award);  
 
imageView_icon = (ImageView) view  
.findViewById(R.id.imageview_task_app_cion);  
 
textView01 = (TextView) view  
.findViewById(R.id.textview_task_app_name);  
textView02 = (TextView) view  
.findViewById(R.id.textview_task_app_size);  
textView03 = (TextView) view  
.findViewById(R.id.textview_task_app_desc);  
textView04 = (TextView) view  
.findViewById(R.id.textview_task_app_love);  
button = (Button) view.findViewById(R.id.button_task_download);  
linearLayout = (LinearLayout) view  
.findViewById(R.id.linearlayout_task);  
 
button.setOnClickListener(new OnClickListener() {  
@Override  
public void onClick(View v) {  
System.out.println("mState:173    "+mState);  
if (mState == DownloadManager.STATE_NONE  
|| mState == DownloadManager.STATE_PAUSED  
|| mState == DownloadManager.STATE_ERROR) {  
 
mDownloadManager.download(mData);  
} else if (mState == DownloadManager.STATE_WAITING  
|| mState == DownloadManager.STATE_DOWNLOADING) {  
mDownloadManager.pause(mData);  
} else if (mState == DownloadManager.STATE_DOWNLOADED) {  
//                      tell2Server();  
mDownloadManager.install(mData);  
}  
}  
});  
return view;  
}  
 
 
public void setData(AppInfo data) {  
 
if (mDownloadManager == null) {  
mDownloadManager = DownloadManager.getInstance();  
 
}  
String filepath= FileUtil.getDownloadDir(AppUtil.getContext()) + File.separator + data.getName() + ".apk";  
 
boolean existsFile = FileUtil.isExistsFile(filepath);  
if(existsFile){  
int fileSize = FileUtil.getFileSize(filepath);  
 
if(data.getSize()==fileSize){  
DownloadInfo downloadInfo = DownloadInfo.clone(data);  
downloadInfo.setCurrentSize(data.getSize());  
downloadInfo.setHasFinished(true);  
mDownloadManager.setDownloadInfo(data.getId(),downloadInfo );  
}  
//                  else if(fileSize>0){  
//                      DownloadInfo downloadInfo = DownloadInfo.clone(data);  
//                      downloadInfo.setCurrentSize(data.getSize());  
//                      downloadInfo.setHasFinished(false);  
//                      mDownloadManager.setDownloadInfo(data.getId(),downloadInfo );  
//                  }  
 
}  
 
DownloadInfo downloadInfo = mDownloadManager.getDownloadInfo(data  
.getId());  
if (downloadInfo != null) {  
 
mState = downloadInfo.getDownloadState();  
mProgress = downloadInfo.getProgress();  
} else {  
 
mState = DownloadManager.STATE_NONE;  
mProgress = 0;  
}  
this.mData = data;  
refreshView();  
}  
 
public AppInfo getData() {  
return mData;  
}  
 
public void refreshView() {  
linearLayout.removeAllViews();  
AppInfo info = getData();  
textView01.setText(info.getName());  
textView02.setText(FileUtil.FormetFileSize(info.getSize()));  
textView03.setText(info.getDes());  
textView04.setText(info.getDownloadNum() + "下載量);  
finalBitmap.display(imageView_icon, info.getIconUrl());  
 
 
if (info.getType().equals("0")) {  
//              mState = DownloadManager.STATE_READ;  
textView02.setVisibility(View.GONE);  
}else{  
String  path=FileUtil.getDownloadDir(AppUtil.getContext()) + File.separator + info.getName() + ".apk";  
hasAttached = FileUtil.isValidAttach(path, false);  
 
DownloadInfo downloadInfo = mDownloadManager.getDownloadInfo(info  
.getId());  
if (downloadInfo != null && hasAttached) {  
if(downloadInfo.isHasFinished()){  
 
mState = DownloadManager.STATE_DOWNLOADED;  
}else{  
mState = DownloadManager.STATE_PAUSED;  
 
}  
 
} else {  
mState = DownloadManager.STATE_NONE;  
if(downloadInfo !=null){  
downloadInfo.setDownloadState(mState);  
}  
}  
}  
 
refreshState(mState, mProgress);  
}  
 
public void refreshState(int state, float progress) {  
mState = state;  
mProgress = progress;  
switch (mState) {  
case DownloadManager.STATE_NONE:  
button.setText(R.string.app_state_download);  
break;  
case DownloadManager.STATE_PAUSED:  
button.setText(R.string.app_state_paused);  
break;  
case DownloadManager.STATE_ERROR:  
button.setText(R.string.app_state_error);  
break;  
case DownloadManager.STATE_WAITING:  
button.setText(R.string.app_state_waiting);  
break;  
case DownloadManager.STATE_DOWNLOADING:  
button.setText((int) (mProgress * 100) + "%");  
break;  
case DownloadManager.STATE_DOWNLOADED:  
button.setText(R.string.app_state_downloaded);  
break;  
//          case DownloadManager.STATE_READ:  
//              button.setText(R.string.app_state_read);  
//              break;  
default:  
break;  
}  
}  
}  
}
何時 注冊 監(jiān)聽observer

里面代碼有點多,那就看startObserver()方法做了什么。

public void startObserver() {
    DownloadManager.getInstance().registerObserver(this);  
}

這里 是 注冊了observer, Observer 是什么東西?在DownloadManager 中我們定義了

public interface DownloadObserver {  
 
public void onDownloadStateChanged(DownloadInfo info);  
 
public void onDownloadProgressed(DownloadInfo info);  
}

一個接口,里面有兩個抽象方法 一個是 進度,另一個是下載狀態(tài)。
那回過頭來,屢一下, 我們在 下載的關(guān)鍵代碼里面調(diào)用了

DownloadObserver onDownloadProgressed() 
DownloadObserver.onDownloadStateChanged()

兩個抽象方法,而我們在 adapter

@Override  
public void onDownloadStateChanged(DownloadInfo info) {  
refreshHolder(info);  
}  
 
@Override  
public void onDownloadProgressed(DownloadInfo info) {  
refreshHolder(info);  
}

中實現(xiàn)了 這兩個方法 就可以輕松的控制 去 刷新 和改變 下載狀態(tài)了。
細心的朋友 或許 發(fā)現(xiàn)問題了,對,我們還沒有注冊O(shè)bserver,就在 DownloadManager 中去調(diào)用了。 這里 在看下DownloadManager 中 調(diào)用的方法

/** 當(dāng)下載狀態(tài)發(fā)送改變的時候回調(diào) */  
public void notifyDownloadStateChanged(DownloadInfo info) {  
synchronized (mObservers) {  
for (DownloadObserver observer : mObservers) {  
observer.onDownloadStateChanged(info);  
}  
}  
}  
 
/** 當(dāng)下載進度發(fā)送改變的時候回調(diào) */  
public void notifyDownloadProgressed(DownloadInfo info) {  
synchronized (mObservers) {  
for (DownloadObserver observer : mObservers) {  
observer.onDownloadProgressed(info);  
}  
}  
}

是的,這里我們遍歷一個observer 容器,然后去刷新 ,所以我們還需要 把 Observer 對象 添加到 集合 mObservers 中,

所以肯定有這樣一個方法 講 observer 添加到集合中 。

/* 注冊觀察者 /   
public void registerObserver(DownloadObserver observer) {   
synchronized (mObservers) {   
if (!mObservers.contains(observer)) {   
mObservers.add(observer);   
}   
}   
}
/** 反注冊觀察者 */  
public void unRegisterObserver(DownloadObserver observer) {  
synchronized (mObservers) {  
if (mObservers.contains(observer)) {  
mObservers.remove(observer);  
}  
}  
}

所以最后一步,因為 adapter 方法中有 startObserver, 所以 我們在 主界面 MainActivity 的類中調(diào)用 adapter.startObser() 將 實現(xiàn)了 接口的adapter 對象 添加到 Observer 容器中 就可以了。
OK。大功告成!


DownloadManager 代碼

這里 貼一下DownloadManager 代碼

public class DownloadManager {  
public static final int STATE_NONE = 0;  
/** 等待中 */  
public static final int STATE_WAITING = 1;  
/** 下載中 */  
public static final int STATE_DOWNLOADING = 2;  
/** 暫停 */  
public static final int STATE_PAUSED = 3;  
/** 下載完畢 */  
public static final int STATE_DOWNLOADED = 4;  
/** 下載失敗 */  
public static final int STATE_ERROR = 5;  
 
// public static final int STATE_READ = 6;  
 
private static DownloadManager instance;  
 
private DownloadManager() {  
}  
 
/** 用于記錄下載信息,如果是正式項目,需要持久化保存 */  
private Map<Long, DownloadInfo> mDownloadMap = new ConcurrentHashMap<Long, DownloadInfo>();  
/** 用于記錄觀察者,當(dāng)信息發(fā)送了改變,需要通知他們 */  
private List<DownloadObserver> mObservers = new ArrayList<DownloadObserver>();  
/** 用于記錄所有下載的任務(wù),方便在取消下載時,通過id能找到該任務(wù)進行刪除 */  
private Map<Long, DownloadTask> mTaskMap = new ConcurrentHashMap<Long, DownloadTask>();  
 
public static synchronized DownloadManager getInstance() {  
if (instance == null) {  
instance = new DownloadManager();  
}  
return instance;  
}  
 
/** 注冊觀察者 */  
public void registerObserver(DownloadObserver observer) {  
synchronized (mObservers) {  
if (!mObservers.contains(observer)) {  
mObservers.add(observer);  
}  
}  
}  
 
/** 反注冊觀察者 */  
public void unRegisterObserver(DownloadObserver observer) {  
synchronized (mObservers) {  
if (mObservers.contains(observer)) {  
mObservers.remove(observer);  
}  
}  
}  
 
/** 當(dāng)下載狀態(tài)發(fā)送改變的時候回調(diào) */  
public void notifyDownloadStateChanged(DownloadInfo info) {  
synchronized (mObservers) {  
for (DownloadObserver observer : mObservers) {  
observer.onDownloadStateChanged(info);  
}  
}  
}  
 
/** 當(dāng)下載進度發(fā)送改變的時候回調(diào) */  
public void notifyDownloadProgressed(DownloadInfo info) {  
synchronized (mObservers) {  
for (DownloadObserver observer : mObservers) {  
observer.onDownloadProgressed(info);  
}  
}  
}  
 
/** 下載,需要傳入一個appInfo對象 */  
public synchronized void download(AppInfo appInfo) {  
// 先判斷是否有這個app的下載信息  
DownloadInfo info = mDownloadMap.get(appInfo.getId());  
if (info == null) {// 如果沒有,則根據(jù)appInfo創(chuàng)建一個新的下載信息  
info = DownloadInfo.clone(appInfo);  
mDownloadMap.put(appInfo.getId(), info);  
}  
// 判斷狀態(tài)是否為STATE_NONE、STATE_PAUSED、STATE_ERROR。只有這3種狀態(tài)才能進行下載,其他狀態(tài)不予處理  
if (info.getDownloadState() == STATE_NONE  
|| info.getDownloadState() == STATE_PAUSED  
|| info.getDownloadState() == STATE_ERROR) {  
// 下載之前,把狀態(tài)設(shè)置為STATE_WAITING,因為此時并沒有產(chǎn)開始下載,只是把任務(wù)放入了線程池中,當(dāng)任務(wù)真正開始執(zhí)行時,才會改為STATE_DOWNLOADING  
info.setDownloadState(STATE_WAITING);  
notifyDownloadStateChanged(info);// 每次狀態(tài)發(fā)生改變,都需要回調(diào)該方法通知所有觀察者  
DownloadTask task = new DownloadTask(info);// 創(chuàng)建一個下載任務(wù),放入線程池  
mTaskMap.put(info.getId(), task);  
ThreadManager.getDownloadPool().execute(task);  
}  
}  
 
/** 暫停下載 */  
public synchronized void pause(AppInfo appInfo) {  
stopDownload(appInfo);  
DownloadInfo info = mDownloadMap.get(appInfo.getId());// 找出下載信息  
if (info != null) {// 修改下載狀態(tài)  
info.setDownloadState(STATE_PAUSED);  
notifyDownloadStateChanged(info);  
}  
}  
 
/** 取消下載,邏輯和暫停類似,只是需要刪除已下載的文件 */  
public synchronized void cancel(AppInfo appInfo) {  
stopDownload(appInfo);  
DownloadInfo info = mDownloadMap.get(appInfo.getId());// 找出下載信息  
if (info != null) {// 修改下載狀態(tài)并刪除文件  
info.setDownloadState(STATE_NONE);  
notifyDownloadStateChanged(info);  
info.setCurrentSize(0);  
File file = new File(info.getPath());  
file.delete();  
}  
}  
 
/** 安裝應(yīng)用 */  
public synchronized void install(AppInfo appInfo) {  
stopDownload(appInfo);  
DownloadInfo info = mDownloadMap.get(appInfo.getId());// 找出下載信息  
if (info != null) {// 發(fā)送安裝的意圖  
Intent installIntent = new Intent(Intent.ACTION_VIEW);  
installIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);  
installIntent.setDataAndType(Uri.parse("file://" + info.getPath()),  
"application/vnd.android.package-archive");  
AppUtil.getContext().startActivity(installIntent);  
}  
notifyDownloadStateChanged(info);  
}  
 
/** 啟動應(yīng)用,啟動應(yīng)用是最后一個 */  
public synchronized void open(AppInfo appInfo) {  
try {  
Context context = AppUtil.getContext();  
// 獲取啟動Intent  
Intent intent = context.getPackageManager()  
.getLaunchIntentForPackage(appInfo.getPackageName());  
context.startActivity(intent);  
} catch (Exception e) {  
}  
}  
 
/** 如果該下載任務(wù)還處于線程池中,且沒有執(zhí)行,先從線程池中移除 */  
private void stopDownload(AppInfo appInfo) {  
DownloadTask task = mTaskMap.remove(appInfo.getId());// 先從集合中找出下載任務(wù)  
if (task != null) {  
ThreadManager.getDownloadPool().cancel(task);// 然后從線程池中移除  
}  
}  
 
/** 獲取下載信息 */  
public synchronized DownloadInfo getDownloadInfo(long id) {  
return mDownloadMap.get(id);  
}  
 
public synchronized void setDownloadInfo(long id, DownloadInfo info) {  
mDownloadMap.put(id, info);  
}  
 
/** 下載任務(wù) */  
public class DownloadTask implements Runnable {  
private DownloadInfo info;  
 
public DownloadTask(DownloadInfo info) {  
this.info = info;  
}  
 
@Override  
public void run() {  
info.setDownloadState(STATE_DOWNLOADING);// 先改變下載狀態(tài)  
notifyDownloadStateChanged(info);  
File file = new File(info.getPath());// 獲取下載文件  
HttpResult httpResult = null;  
InputStream stream = null;  
if (info.getCurrentSize() == 0 || !file.exists()  
|| file.length() != info.getCurrentSize()) {  
// 如果文件不存在,或者進度為0,或者進度和文件長度不相符,就需要重新下載  
 
info.setCurrentSize(0);  
file.delete();  
}  
httpResult = HttpHelper.download(info.getUrl());  
// else {  
// // //文件存在且長度和進度相等,采用斷點下載  
// httpResult = HttpHelper.download(info.getUrl() + "&range=" +  
// info.getCurrentSize());  
// }  
if (httpResult == null  
|| (stream = httpResult.getInputStream()) == null) {  
info.setDownloadState(STATE_ERROR);// 沒有下載內(nèi)容返回,修改為錯誤狀態(tài)  
notifyDownloadStateChanged(info);  
} else {  
try {  
skipBytesFromStream(stream, info.getCurrentSize());  
} catch (Exception e1) {  
e1.printStackTrace();  
}  
 
FileOutputStream fos = null;  
try {  
fos = new FileOutputStream(file, true);  
int count = -1;  
byte[] buffer = new byte[1024];  
while (((count = stream.read(buffer)) != -1)  
&& info.getDownloadState() == STATE_DOWNLOADING) {  
// 每次讀取到數(shù)據(jù)后,都需要判斷是否為下載狀態(tài),如果不是,下載需要終止,如果是,則刷新進度  
fos.write(buffer, 0, count);  
fos.flush();  
info.setCurrentSize(info.getCurrentSize() + count);  
notifyDownloadProgressed(info);// 刷新進度  
}  
} catch (Exception e) {  
info.setDownloadState(STATE_ERROR);  
notifyDownloadStateChanged(info);  
info.setCurrentSize(0);  
file.delete();  
} finally {  
IOUtils.close(fos);  
if (httpResult != null) {  
httpResult.close();  
}  
}  
 
// 判斷進度是否和app總長度相等  
if (info.getCurrentSize() == info.getAppSize()) {  
info.setDownloadState(STATE_DOWNLOADED);  
notifyDownloadStateChanged(info);  
} else if (info.getDownloadState() == STATE_PAUSED) {// 判斷狀態(tài)  
notifyDownloadStateChanged(info);  
} else {  
info.setDownloadState(STATE_ERROR);  
notifyDownloadStateChanged(info);  
info.setCurrentSize(0);// 錯誤狀態(tài)需要刪除文件  
file.delete();  
}  
}  
mTaskMap.remove(info.getId());  
}  
}  
 
public interface DownloadObserver {  
 
public abstract void onDownloadStateChanged(DownloadInfo info);  
 
public abstract void onDownloadProgressed(DownloadInfo info);  
}  
 
/* 重寫了Inpustream 中的skip(long n) 方法,將數(shù)據(jù)流中起始的n 個字節(jié)跳過 */  
private long skipBytesFromStream(InputStream inputStream, long n) {  
long remaining = n;  
// SKIP_BUFFER_SIZE is used to determine the size of skipBuffer  
int SKIP_BUFFER_SIZE = 10000;  
// skipBuffer is initialized in skip(long), if needed.  
byte[] skipBuffer = null;  
int nr = 0;  
if (skipBuffer == null) {  
skipBuffer = new byte[SKIP_BUFFER_SIZE];  
}  
byte[] localSkipBuffer = skipBuffer;  
if (n <= 0) {  
return 0;  
}  
while (remaining > 0) {  
try {  
long skip = inputStream.skip(10000);  
nr = inputStream.read(localSkipBuffer, 0,  
(int) Math.min(SKIP_BUFFER_SIZE, remaining));  
} catch (IOException e) {  
e.printStackTrace();  
}  
if (nr < 0) {  
break;  
}  
remaining -= nr;  
}  
return n - remaining;  
}  
}

有兩點 需要說明,關(guān)于 點擊暫停后,再繼續(xù)下載 有兩種方式可以實現(xiàn)
第一種 點擊暫停的時候 記錄下載了 多少,然后 再點擊 繼續(xù)下載 時,告訴服務(wù)器, 讓服務(wù)器接著 上次的數(shù)據(jù) 往本地傳遞,
代碼 就是 我們 DownloadTask 下載時候,判斷一下

// //文件存在且長度和進度相等,采用斷點下載  
httpResult = HttpHelper.download(info.getUrl() +"&range="+ info.getCurrentSize());

通過 range 來區(qū)分 當(dāng)前的下載size.
服務(wù)器 處理的代碼 也很簡單 就是一句話
String range = req.getParameter(“range”); 拿到 range 判斷 range 存在不存在。 如果不存在

FileInputStream stream = new FileInputStream(file);  
int count = -1;  
byte[] buffer = new byte[1024];  
while ((count = stream.read(buffer)) != -1) {  
SystemClock.sleep(20);  
out.write(buffer, 0, count);  
out.flush();  
}  
stream.close();  
out.close();

如果存在 那么跳過range 個字節(jié)

RandomAccessFile raf = new RandomAccessFile(file, "r");  
raf.seek(Long.valueOf(range));    
int count = -1;  
byte[] buffer = new byte[1024];  
while ((count = raf.read(buffer)) != -1) {  
SystemClock.sleep(10);  
out.write(buffer, 0, count);  
out.flush();  
}  
raf.close();  
out.close();

? 另一種方式是本地處理,這個demo 中就是本地處理的, 但是有一個問題, 因為 Java api的原因 ,inputStream.skip() 方法 并不能準(zhǔn)確的 跳過多少個字節(jié),
而是 小于你想要跳過的字節(jié),所以 你要去遍歷 一直到 滿足你要跳過的字節(jié) 在繼續(xù)寫, 因為 這樣的方法有一個缺點,就是在下載很大的文件,比如文件大小20M ,當(dāng)已經(jīng)下載了15M 此時你去暫停,在繼續(xù)下載,那么要跳過前面的15M 將會話費很多時間。
? 所以這個僅限于學(xué)習(xí)。實際中 如果要下載大的文件,不能用這種方法。
代碼下載 :
MulDownLoad.zip

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容