Skip to content
This repository has been archived by the owner on Mar 6, 2019. It is now read-only.

Commit

Permalink
新增移除下载状态监听接口;避免内存泄漏
Browse files Browse the repository at this point in the history
  • Loading branch information
lijian committed Jul 26, 2017
1 parent fc8dd1f commit c4c1f2c
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 10 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ allprojects {
```
dependencies {
//如果项目中已有com.android.support.*包,则从Downloader中剔除;
compile('com.github.CodyyAndroid:Downloader:1.1.6') {
compile('com.github.CodyyAndroid:Downloader:1.1.7') {
exclude group: 'com.android.support'
}
//如果项目中无com.android.support.*包,则保留;
compile('com.github.CodyyAndroid:Downloader:1.1.6')
compile('com.github.CodyyAndroid:Downloader:1.1.7')
}
```
## [API](https://jitpack.io/com/github/CodyyAndroid/Downloader/1.1.6/javadoc/)
## [API](https://jitpack.io/com/github/CodyyAndroid/Downloader/1.1.7/javadoc/)


9 changes: 8 additions & 1 deletion app/src/test/java/com/codyy/downloader/ExampleUnitTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

import org.junit.Test;

import java.util.HashMap;
import java.util.Map;

import static org.junit.Assert.*;

/**
Expand All @@ -12,6 +15,10 @@
public class ExampleUnitTest {
@Test
public void addition_isCorrect() throws Exception {
assertEquals(4, 2 + 2);
Map<String,String> map=new HashMap<>();
map.put("1","111");
map.put("2","2222");
map.remove("1");
assertEquals(1,map.size());
}
}
6 changes: 3 additions & 3 deletions downloadlibrary/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ android {
defaultConfig {
minSdkVersion 14
targetSdkVersion 26
versionCode 49
versionName "1.1.6"
versionCode 50
versionName "1.1.7"

testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

Expand Down Expand Up @@ -56,7 +56,7 @@ javadoc {
author true
version true
links "http://docs.oracle.com/javase/7/docs/api"
title "下载库1.1.6"
title "下载库1.1.7"
}
}
// 制作文档(Javadoc)
Expand Down
25 changes: 23 additions & 2 deletions downloadlibrary/src/main/java/com/codyy/download/Downloader.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
* 文件下载器
* Created by lijian on 2017/6/7.
*
* @version 1.1.5
* @version 1.1.7
*/

public class Downloader {
Expand Down Expand Up @@ -132,6 +132,7 @@ public void onServiceDisconnected(ComponentName name) {

/**
* 同步已下载的记录
*
* @param entity 下载记录
* @return true:同步成功;false:服务未启动或同步失败
*/
Expand Down Expand Up @@ -274,7 +275,7 @@ public void deleteAll() {
/**
* 接收下载状态
*
* @param id 下载地址
* @param id 下载资源ID
* @param loadListener 状态监听
*/
public void receiveDownloadStatus(@NonNull String id, @NonNull DownLoadListener loadListener) {
Expand All @@ -286,6 +287,26 @@ public void receiveDownloadStatus(@NonNull String id, @NonNull DownLoadListener
}
}

/**
* 移除下载状态监听
*
* @param id 下载资源ID
*/
public void removeDownloadStatusListener(@NonNull String id) {
if (mDownloadService != null) {
mDownloadService.removeDownloadStatusListener(id);
}
}

/**
* 移除所有下载状态监听
*/
public void removeAllDownloadStatusListener() {
if (mDownloadService != null) {
mDownloadService.removeAllDownloadStatusListener();
}
}

/**
* 监听下载速率和正在下载的任务数{@link DownloadRateListener#onRate(String, int)}
* 监听下载成功后返回下载结果{@link DownloadRateListener#onComplete(DownloadEntity)}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ public void onDestroy() {
mDownloadDao.closeDB();
}
mHandler = null;
mDownLoadListeners.clear();
Cog.d(TAG, "DownloadService onDestroy");
}

Expand Down Expand Up @@ -190,13 +191,31 @@ private String getSavePath(@NonNull String downloadUrl, String path) {
/**
* 接收下载状态
*
* @param id 下载地址
* @param id 资源id
* @param loadListener 监听
*/
public void receiveDownloadStatus(@NonNull String id, @NonNull DownLoadListener loadListener) {
mDownLoadListeners.put(id, loadListener);
}

/**
* 移除下载监听
*
* @param id 资源id
*/
public void removeDownloadStatusListener(@NonNull String id) {
if (mDownLoadListeners.containsKey(id)) {
mDownLoadListeners.remove(id);
}
}

/**
* 移除所有下载监听
*/
public void removeAllDownloadStatusListener() {
mDownLoadListeners.clear();
}

/**
* 增加下载速率监听
*
Expand Down

0 comments on commit c4c1f2c

Please sign in to comment.