统一暂停,继续下载处理
This commit is contained in:
@ -2,6 +2,9 @@ package com.gh.download;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.os.Handler;
|
||||
import android.os.Message;
|
||||
import android.support.v4.util.ArrayMap;
|
||||
import android.widget.Toast;
|
||||
|
||||
import com.gh.common.constant.Constants;
|
||||
@ -16,16 +19,43 @@ public class DownloadManager {
|
||||
private static DownloadManager mInstance;
|
||||
private Context context;
|
||||
|
||||
private Handler handler;
|
||||
private ArrayMap<String, Long> lastTimeMap;
|
||||
|
||||
private DownloadManager(Context context) {
|
||||
this.context = context;
|
||||
lastTimeMap = new ArrayMap<String, Long>();
|
||||
handler = new Handler(context.getMainLooper()){
|
||||
@Override
|
||||
public void handleMessage(Message msg) {
|
||||
if (msg.what == Constants.CONTINUE_DOWNLOAD_TASK) {
|
||||
String url = (String) msg.obj;
|
||||
if (System.currentTimeMillis() - lastTimeMap.get(url) >= 1000) {
|
||||
resume(url);
|
||||
}
|
||||
} else if (msg.what == Constants.PAUSE_DOWNLOAD_TASK) {
|
||||
String url = (String) msg.obj;
|
||||
if (System.currentTimeMillis() - lastTimeMap.get(url) >= 1000) {
|
||||
pause(url);
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
context.startService(new Intent(context, DownloadService.class));
|
||||
}
|
||||
|
||||
public void put(String url, long time) {
|
||||
lastTimeMap.put(url, time);
|
||||
}
|
||||
|
||||
public void sendMessageDelayed(Message msg, long delayMillis) {
|
||||
handler.sendMessageDelayed(msg, delayMillis);
|
||||
}
|
||||
|
||||
public static DownloadManager getInstance(Context context) {
|
||||
if (mInstance == null) {
|
||||
mInstance = new DownloadManager(context.getApplicationContext());
|
||||
}
|
||||
|
||||
return mInstance;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user