-
Notifications
You must be signed in to change notification settings - Fork 157
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #984 from liangchuxian/master
- Loading branch information
Showing
10 changed files
with
762 additions
and
0 deletions.
There are no files selected for viewing
31 changes: 31 additions & 0 deletions
31
app/src/main/java/edu/hzuapps/androidworks/homeworks/net1314080903223/DBHelper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package single_thread_download.skyward.com.db; | ||
|
||
import android.content.Context; | ||
import android.database.sqlite.SQLiteDatabase; | ||
import android.database.sqlite.SQLiteOpenHelper; | ||
|
||
/** | ||
* Created by skyward on 2016/6/15. | ||
*/ | ||
public class DBHelper extends SQLiteOpenHelper { | ||
public static final int VERSION = 1; | ||
public static final String SQL_CREATE = "create table thread_info(_id integer primary key," + | ||
"thread_id integer,url text,start integer,end integer,finished integer)"; | ||
public static final String SQL_DROP = "drop table if exists thread_info"; | ||
public static final String DB_NAME = "download.db"; | ||
|
||
public DBHelper(Context context) { | ||
super(context, DB_NAME, null, VERSION); | ||
} | ||
|
||
@Override | ||
public void onCreate(SQLiteDatabase db) { | ||
db.execSQL(SQL_CREATE); | ||
} | ||
|
||
@Override | ||
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { | ||
db.execSQL(SQL_DROP); | ||
db.execSQL(SQL_CREATE); | ||
} | ||
} |
118 changes: 118 additions & 0 deletions
118
app/src/main/java/edu/hzuapps/androidworks/homeworks/net1314080903223/DownloadService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
package single_thread_download.skyward.com.service; | ||
|
||
import android.app.Service; | ||
import android.content.Intent; | ||
import android.os.Bundle; | ||
import android.os.Environment; | ||
import android.os.Handler; | ||
import android.os.IBinder; | ||
import android.os.Message; | ||
import android.text.LoginFilter; | ||
import android.util.Log; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.io.RandomAccessFile; | ||
import java.net.HttpURLConnection; | ||
import java.net.MalformedURLException; | ||
import java.net.URL; | ||
|
||
import single_thread_download.skyward.com.bean.FileInfo; | ||
|
||
public class DownloadService extends Service { | ||
DownloadTask downloadTask = null; | ||
Handler mHandler = new Handler() { | ||
@Override | ||
public void handleMessage(Message msg) { | ||
switch (MSG_INIT) { | ||
case MSG_INIT: | ||
FileInfo fileInfo = (FileInfo) msg.obj; | ||
Log.i(TAG, "handleMessage: " + fileInfo); | ||
downloadTask = new DownloadTask(DownloadService.this, fileInfo); | ||
downloadTask.download(); | ||
break; | ||
} | ||
} | ||
}; | ||
private static final String TAG = "DownloadService"; | ||
public static final String ACTION_START = "ACTION_START"; | ||
public static final String ACTION_STOP = "ACTION_STOP"; | ||
public static final String ACTION_UPDATE = "ACTION_UPDATE"; | ||
public static final int MSG_INIT = 0; | ||
public static final String DOWNLOAD_PATH = Environment.getExternalStorageDirectory().getAbsolutePath() + "/download/"; | ||
|
||
@Override | ||
public int onStartCommand(Intent intent, int flags, int startId) { | ||
String action = intent.getAction(); | ||
//获得Activity传来的参数 | ||
FileInfo fileInfo = (FileInfo) intent.getSerializableExtra("fileInfo"); | ||
if(action.equals(ACTION_START)) { | ||
Log.i(TAG, "Start: " + fileInfo.toString()); | ||
new InitThread(fileInfo).start(); | ||
} else if(action.equals(ACTION_STOP)) { | ||
Log.i(TAG, "Stop: " +fileInfo.toString()); | ||
downloadTask.isPause = true; | ||
} | ||
return super.onStartCommand(intent, flags, startId); | ||
} | ||
|
||
public DownloadService() { | ||
} | ||
|
||
@Override | ||
public IBinder onBind(Intent intent) { | ||
return null; | ||
} | ||
|
||
class InitThread extends Thread { | ||
private FileInfo mFileInfo; | ||
|
||
public InitThread(FileInfo mFileInfo) { | ||
this.mFileInfo = mFileInfo; | ||
} | ||
|
||
@Override | ||
public void run() { | ||
HttpURLConnection conn = null; | ||
RandomAccessFile raf = null; | ||
try { | ||
URL url = new URL(mFileInfo.getUrl()); | ||
conn = (HttpURLConnection) url.openConnection(); | ||
conn.setConnectTimeout(3000); | ||
conn.setRequestMethod("GET"); | ||
int length = -1; | ||
if(conn.getResponseCode() == 200) { | ||
//获取文件长度 | ||
length = conn.getContentLength(); | ||
} | ||
if(length <= 0) { | ||
return; | ||
} | ||
File dir = new File(DOWNLOAD_PATH); | ||
if(!dir.exists()) { | ||
dir.mkdir(); | ||
} | ||
//在本地创建文件 | ||
File file = new File(dir, mFileInfo.getFileName()); | ||
raf = new RandomAccessFile(file, "rwd"); | ||
//设置文件长度 | ||
raf.setLength(length); | ||
mFileInfo.setLength(length); | ||
mHandler.obtainMessage(MSG_INIT, mFileInfo).sendToTarget(); | ||
} catch (MalformedURLException e) { | ||
e.printStackTrace(); | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} finally { | ||
try { | ||
if(raf != null) { | ||
raf.close(); | ||
} | ||
conn.disconnect(); | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
} | ||
} | ||
} |
126 changes: 126 additions & 0 deletions
126
app/src/main/java/edu/hzuapps/androidworks/homeworks/net1314080903223/DownloadTask.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
package single_thread_download.skyward.com.service; | ||
|
||
import android.content.Context; | ||
import android.content.Intent; | ||
import android.widget.ProgressBar; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.RandomAccessFile; | ||
import java.net.HttpURLConnection; | ||
import java.net.MalformedURLException; | ||
import java.net.URL; | ||
import java.util.List; | ||
|
||
import single_thread_download.skyward.com.bean.FileInfo; | ||
import single_thread_download.skyward.com.bean.ThreadInfo; | ||
import single_thread_download.skyward.com.db.ThreadDao; | ||
import single_thread_download.skyward.com.db.ThreadDaoImpl; | ||
|
||
/** | ||
* 下载任务类 | ||
* Created by skyward on 2016/6/16. | ||
*/ | ||
public class DownloadTask { | ||
private Context mContext = null; | ||
private FileInfo mFileInfo = null; | ||
private ThreadDao mDao = null; | ||
private int mFinished = 0; | ||
public boolean isPause = false; | ||
|
||
public void download() { | ||
//读取数据库的线程信息 | ||
List<ThreadInfo> threadInfos = mDao.getThreads(mFileInfo.getUrl()); | ||
ThreadInfo threadInfo = null; | ||
if(threadInfos.size() == 0) { | ||
//初始化线程信息对象 | ||
threadInfo = new ThreadInfo(0, mFileInfo.getUrl(), 0, mFileInfo.getLength(), 0); | ||
} else { | ||
threadInfo = threadInfos.get(0); | ||
} | ||
//创建子线程进行下载 | ||
new DownloadThread(threadInfo).start(); | ||
} | ||
|
||
public DownloadTask(Context mContext, FileInfo mFileInfo) { | ||
this.mContext = mContext; | ||
this.mFileInfo = mFileInfo; | ||
mDao = new ThreadDaoImpl(mContext); | ||
} | ||
|
||
/** | ||
* 下载线程 | ||
*/ | ||
class DownloadThread extends Thread { | ||
private ThreadInfo mThreadInfo = null; | ||
|
||
public DownloadThread(ThreadInfo mThreadInfo){ | ||
this.mThreadInfo = mThreadInfo; | ||
} | ||
|
||
@Override | ||
public void run() { | ||
//向数据库插入线程信息 | ||
if(!mDao.isExists(mThreadInfo.getUrl(), mThreadInfo.getId())) { | ||
mDao.insertThread(mThreadInfo); | ||
} | ||
HttpURLConnection conn = null; | ||
RandomAccessFile raf = null; | ||
InputStream in = null; | ||
try { | ||
URL url = new URL(mThreadInfo.getUrl()); | ||
conn = (HttpURLConnection) url.openConnection(); | ||
conn.setConnectTimeout(3000); | ||
conn.setRequestMethod("GET"); | ||
//下载设置 | ||
int start = mThreadInfo.getStart() + mThreadInfo.getFinished(); | ||
conn.setRequestProperty("Range", "bytes=" + start + "-" + mThreadInfo.getEnd()); | ||
//写入设置 | ||
File f = new File(DownloadService.DOWNLOAD_PATH, mFileInfo.getFileName()); | ||
raf = new RandomAccessFile(f, "rwd"); | ||
raf.seek(start); | ||
Intent intent = new Intent(DownloadService.ACTION_UPDATE); | ||
mFinished += mThreadInfo.getFinished(); | ||
//开始下载 | ||
if(conn.getResponseCode() == 206) { | ||
//读取数据 | ||
in = conn.getInputStream(); | ||
byte[] buffer = new byte[1024 * 4]; | ||
int len = -1; | ||
long time = System.currentTimeMillis(); | ||
while((len = in.read(buffer)) != -1) { | ||
//写入文件 | ||
raf.write(buffer, 0, len); | ||
//把下载进度广播给Activity | ||
mFinished += len; | ||
if(System.currentTimeMillis() - time > 500) { | ||
time = System.currentTimeMillis(); | ||
intent.putExtra("finished", mFinished * 100 / mFileInfo.getLength()); | ||
mContext.sendBroadcast(intent); | ||
} | ||
//在下载暂停时,保存下载进度 | ||
if(isPause) { | ||
mDao.updateThread(mThreadInfo.getUrl(), mThreadInfo.getId(), mFinished); | ||
return; | ||
} | ||
} | ||
//删除线程信息 | ||
mDao.deleteThread(mThreadInfo.getUrl(), mThreadInfo.getId()); | ||
} | ||
} catch (MalformedURLException e) { | ||
e.printStackTrace(); | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} finally { | ||
try { | ||
conn.disconnect(); | ||
raf.close(); | ||
in.close(); | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
} | ||
} | ||
} |
81 changes: 81 additions & 0 deletions
81
app/src/main/java/edu/hzuapps/androidworks/homeworks/net1314080903223/FileInfo.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package single_thread_download.skyward.com.bean; | ||
|
||
import java.io.Serializable; | ||
|
||
/** | ||
* Created by skyward on 2016/5/30. | ||
*/ | ||
public class FileInfo implements Serializable{ | ||
//编号 | ||
private int id; | ||
//文件名 | ||
private String fileName; | ||
//文件大小 | ||
private int length; | ||
//下载链接 | ||
private String url; | ||
//已下载文件大小 | ||
private int finished; | ||
|
||
public FileInfo(int id, String url, String fileName, int length, int finished) { | ||
this.id = id; | ||
this.fileName = fileName; | ||
this.length = length; | ||
this.url = url; | ||
this.finished = finished; | ||
} | ||
|
||
public FileInfo() { | ||
} | ||
|
||
public int getId() { | ||
return id; | ||
} | ||
|
||
public void setId(int id) { | ||
this.id = id; | ||
} | ||
|
||
public String getFileName() { | ||
return fileName; | ||
} | ||
|
||
public void setFileName(String fileName) { | ||
this.fileName = fileName; | ||
} | ||
|
||
public int getLength() { | ||
return length; | ||
} | ||
|
||
public void setLength(int length) { | ||
this.length = length; | ||
} | ||
|
||
public String getUrl() { | ||
return url; | ||
} | ||
|
||
public void setUrl(String url) { | ||
this.url = url; | ||
} | ||
|
||
public int getFinished() { | ||
return finished; | ||
} | ||
|
||
public void setFinished(int finished) { | ||
this.finished = finished; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "FileInfo{" + | ||
"id=" + id + | ||
", fileName='" + fileName + '\'' + | ||
", length=" + length + | ||
", url='" + url + '\'' + | ||
", finished=" + finished + | ||
'}'; | ||
} | ||
} |
Oops, something went wrong.