2.0版本开发
This commit is contained in:
2
.idea/misc.xml
generated
2
.idea/misc.xml
generated
@ -53,7 +53,7 @@
|
|||||||
<ConfirmationsSetting value="0" id="Add" />
|
<ConfirmationsSetting value="0" id="Add" />
|
||||||
<ConfirmationsSetting value="0" id="Remove" />
|
<ConfirmationsSetting value="0" id="Remove" />
|
||||||
</component>
|
</component>
|
||||||
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_7" default="true" assert-keyword="true" jdk-15="true" project-jdk-name="1.8" project-jdk-type="JavaSDK">
|
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" default="true" assert-keyword="true" jdk-15="true" project-jdk-name="1.8" project-jdk-type="JavaSDK">
|
||||||
<output url="file://$PROJECT_DIR$/build/classes" />
|
<output url="file://$PROJECT_DIR$/build/classes" />
|
||||||
</component>
|
</component>
|
||||||
<component name="ProjectType">
|
<component name="ProjectType">
|
||||||
|
@ -4,20 +4,16 @@ import android.os.Bundle;
|
|||||||
import android.os.Environment;
|
import android.os.Environment;
|
||||||
import android.os.Handler;
|
import android.os.Handler;
|
||||||
import android.os.Message;
|
import android.os.Message;
|
||||||
import android.support.design.widget.FloatingActionButton;
|
|
||||||
import android.support.design.widget.Snackbar;
|
|
||||||
import android.support.v7.app.AppCompatActivity;
|
import android.support.v7.app.AppCompatActivity;
|
||||||
import android.support.v7.widget.Toolbar;
|
import android.support.v7.widget.Toolbar;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
import android.view.Menu;
|
|
||||||
import android.view.MenuItem;
|
|
||||||
import android.widget.Button;
|
import android.widget.Button;
|
||||||
import android.widget.ProgressBar;
|
import android.widget.ProgressBar;
|
||||||
import android.widget.TextView;
|
import android.widget.TextView;
|
||||||
import android.widget.Toast;
|
import android.widget.Toast;
|
||||||
|
|
||||||
import com.arialyy.downloadutil.DownLoadUtil;
|
import com.arialyy.downloadutil.util.DownLoadUtil;
|
||||||
import com.arialyy.downloadutil.Util;
|
import com.arialyy.downloadutil.util.Util;
|
||||||
|
|
||||||
import java.net.HttpURLConnection;
|
import java.net.HttpURLConnection;
|
||||||
|
|
||||||
|
@ -0,0 +1,61 @@
|
|||||||
|
package com.arialyy.downloadutil;
|
||||||
|
|
||||||
|
import android.content.Context;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created by lyy on 2016/8/11.
|
||||||
|
* 下载管理器,通过命令的方式控制下载
|
||||||
|
*/
|
||||||
|
public class DownloadManager {
|
||||||
|
/**
|
||||||
|
* 下载开始前事件
|
||||||
|
*/
|
||||||
|
public static final String ACTION_PRE = "ACTION_PRE";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 开始下载事件
|
||||||
|
*/
|
||||||
|
public static final String ACTION_START = "ACTION_START";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 恢复下载事件
|
||||||
|
*/
|
||||||
|
public static final String ACTION_RESUME = "ACTION_RESUME";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 正在下载事件
|
||||||
|
*/
|
||||||
|
public static final String ACTION_RUNNING = "ACTION_RUNNING";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 停止下载事件
|
||||||
|
*/
|
||||||
|
public static final String ACTION_STOP = "ACTION_STOP";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 取消下载事件
|
||||||
|
*/
|
||||||
|
public static final String ACTION_CANCEL = "ACTION_CANCEL";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 下载完成事件
|
||||||
|
*/
|
||||||
|
public static final String ACTION_COMPLETE = "ACTION_COMPLETE";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 下载失败事件
|
||||||
|
*/
|
||||||
|
public static final String ACTION_FAIL = "ACTION_FAIL";
|
||||||
|
|
||||||
|
private Context mContext;
|
||||||
|
|
||||||
|
private DownloadManager(Context context) {
|
||||||
|
mContext = context;
|
||||||
|
// ACTION_RUNNING = context.getPackageName();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static DownloadManager getInstance(Context context) {
|
||||||
|
return new DownloadManager(context);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,175 @@
|
|||||||
|
package com.arialyy.downloadutil.core;
|
||||||
|
|
||||||
|
import android.content.Context;
|
||||||
|
import android.content.Intent;
|
||||||
|
import android.os.Handler;
|
||||||
|
import android.util.Log;
|
||||||
|
|
||||||
|
import com.arialyy.downloadutil.DownloadManager;
|
||||||
|
import com.arialyy.downloadutil.inf.IDownloadListener;
|
||||||
|
import com.arialyy.downloadutil.util.DownLoadUtil;
|
||||||
|
|
||||||
|
import java.net.HttpURLConnection;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created by lyy on 2016/8/11.
|
||||||
|
* 下载任务类
|
||||||
|
*/
|
||||||
|
public class Task {
|
||||||
|
public static final String TAG = "Task";
|
||||||
|
/**
|
||||||
|
* 下载路径
|
||||||
|
*/
|
||||||
|
String downloadUrl;
|
||||||
|
/**
|
||||||
|
* 保存路径
|
||||||
|
*/
|
||||||
|
String downloadPath;
|
||||||
|
/**
|
||||||
|
* 下载监听
|
||||||
|
*/
|
||||||
|
IDownloadListener listener;
|
||||||
|
Handler outHandler;
|
||||||
|
Context context;
|
||||||
|
DownLoadUtil util;
|
||||||
|
|
||||||
|
private Task() {
|
||||||
|
util = new DownLoadUtil();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 开始下载
|
||||||
|
*/
|
||||||
|
public void start() {
|
||||||
|
if (util.isDownloading()) {
|
||||||
|
Log.d(TAG, "任务正在下载");
|
||||||
|
} else {
|
||||||
|
if (listener == null) {
|
||||||
|
listener = new DownloadListener(context, outHandler);
|
||||||
|
}
|
||||||
|
util.download(context, downloadUrl, downloadPath, listener);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 停止下载
|
||||||
|
*/
|
||||||
|
public void stop() {
|
||||||
|
if (util.isDownloading()) {
|
||||||
|
util.stopDownload();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 取消下载
|
||||||
|
*/
|
||||||
|
public void cancel() {
|
||||||
|
util.cancelDownload();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 下载监听类
|
||||||
|
*/
|
||||||
|
static class DownloadListener extends DownLoadUtil.DownloadListener {
|
||||||
|
Handler outHandler;
|
||||||
|
Context context;
|
||||||
|
Intent sendIntent;
|
||||||
|
long INTERVAL = 1024 * 10; //10k大小的间隔
|
||||||
|
long lastLen = 0; //上一次发送长度
|
||||||
|
|
||||||
|
public DownloadListener(Context context, Handler outHandler) {
|
||||||
|
this.context = context;
|
||||||
|
this.outHandler = outHandler;
|
||||||
|
sendIntent = new Intent();
|
||||||
|
sendIntent.addCategory(context.getPackageName());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onPreDownload(HttpURLConnection connection) {
|
||||||
|
super.onPreDownload(connection);
|
||||||
|
long len = connection.getContentLength();
|
||||||
|
Intent preIntent = new Intent();
|
||||||
|
preIntent.addCategory(context.getPackageName());
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onResume(long resumeLocation) {
|
||||||
|
super.onResume(resumeLocation);
|
||||||
|
sendIntent.putExtra(DownloadManager.RESUME_LOCATION, resumeLocation);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onStart(long startLocation) {
|
||||||
|
super.onStart(startLocation);
|
||||||
|
sendIntent.putExtra(DownloadManager.START_LOCATION, startLocation);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onProgress(long currentLocation) {
|
||||||
|
super.onProgress(currentLocation);
|
||||||
|
if (currentLocation - lastLen > INTERVAL) { //不要太过于频繁发送广播
|
||||||
|
sendIntent.putExtra("progress", currentLocation);
|
||||||
|
sendBroadcast(sendIntent);
|
||||||
|
lastLen = currentLocation;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onStop(long stopLocation) {
|
||||||
|
super.onStop(stopLocation);
|
||||||
|
sendIntent.putExtra(DownloadManager.STOP_LOCATION, stopLocation);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onCancel() {
|
||||||
|
super.onCancel();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onComplete() {
|
||||||
|
super.onComplete();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onFail() {
|
||||||
|
super.onFail();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class Builder {
|
||||||
|
String downloadUrl;
|
||||||
|
String downloadPath;
|
||||||
|
IDownloadListener listener;
|
||||||
|
Handler outHandler;
|
||||||
|
Context context;
|
||||||
|
|
||||||
|
public Builder(Context context, String downloadUrl, String downloadPath) {
|
||||||
|
this.context = context;
|
||||||
|
this.downloadUrl = downloadUrl;
|
||||||
|
this.downloadPath = downloadPath;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Builder setDownloadListener(IDownloadListener listener) {
|
||||||
|
this.listener = listener;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Builder setOutHandler(Handler outHandler) {
|
||||||
|
this.outHandler = outHandler;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Task builder() {
|
||||||
|
Task task = new Task();
|
||||||
|
task.context = context;
|
||||||
|
task.downloadUrl = downloadUrl;
|
||||||
|
task.downloadPath = downloadPath;
|
||||||
|
task.listener = listener;
|
||||||
|
task.outHandler = outHandler;
|
||||||
|
return task;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -1,4 +1,4 @@
|
|||||||
package com.arialyy.downloadutil;
|
package com.arialyy.downloadutil.util;
|
||||||
|
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.support.annotation.NonNull;
|
import android.support.annotation.NonNull;
|
||||||
@ -22,7 +22,7 @@ import java.util.Properties;
|
|||||||
public class DownLoadUtil {
|
public class DownLoadUtil {
|
||||||
private static final String TAG = "DownLoadUtil";
|
private static final String TAG = "DownLoadUtil";
|
||||||
//下载监听
|
//下载监听
|
||||||
private DownloadListener mListener;
|
private IDownloadListener mListener;
|
||||||
/**
|
/**
|
||||||
* 线程数
|
* 线程数
|
||||||
*/
|
*/
|
||||||
@ -41,7 +41,7 @@ public class DownLoadUtil {
|
|||||||
private int mStopNum = 0;
|
private int mStopNum = 0;
|
||||||
public DownLoadUtil() {
|
public DownLoadUtil() {
|
||||||
}
|
}
|
||||||
public DownloadListener getListener(){
|
public IDownloadListener getListener(){
|
||||||
return mListener;
|
return mListener;
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
@ -76,7 +76,7 @@ public class DownLoadUtil {
|
|||||||
* @param downloadListener 下载进度监听 {@link DownloadListener}
|
* @param downloadListener 下载进度监听 {@link DownloadListener}
|
||||||
*/
|
*/
|
||||||
public void download(final Context context, @NonNull final String downloadUrl, @NonNull final String filePath,
|
public void download(final Context context, @NonNull final String downloadUrl, @NonNull final String filePath,
|
||||||
@NonNull final DownloadListener downloadListener) {
|
@NonNull final IDownloadListener downloadListener) {
|
||||||
isDownloading = true;
|
isDownloading = true;
|
||||||
mCurrentLocation = 0;
|
mCurrentLocation = 0;
|
||||||
isStop = false;
|
isStop = false;
|
@ -1,4 +1,4 @@
|
|||||||
package com.arialyy.downloadutil;
|
package com.arialyy.downloadutil.util;
|
||||||
|
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
|
|
Reference in New Issue
Block a user