多多色-多人伦交性欧美在线观看-多人伦精品一区二区三区视频-多色视频-免费黄色视屏网站-免费黄色在线

國內最全IT社區平臺 聯系我們 | 收藏本站
阿里云優惠2
您當前位置:首頁 > 互聯網 > F-droid 源碼片段(二)下載模塊整理

F-droid 源碼片段(二)下載模塊整理

來源:程序員人生   發布時間:2014-10-06 08:00:00 閱讀次數:2609次

 這篇文章把F-droid的下載功能經過修改單獨拿出來,而且做了一個demo。

希望能對自己后續起到借鑒作用。各位童鞋也可以去進行下載。

 

其實主要的思想有2個

 

1、使用接口進行回調

2、線程直接調用回調,由于無法知道主線程是否進行UI操作,所以把線程的回調進行了包裝,使用Handler來發消息。保證不會崩潰。

 

 

項目下載地址:

http://download.csdn.net/download/leehu1987/7979253

 


尚未完成的功能:

1、斷點下載(需要數據庫)

2、如果下載完成了,下次下載應該是不需要下載了。

 

缺陷:

一個下載線程只能注冊一個監聽,比較好的辦法是可以使用觀察者模式通知各個頁面。后續進行優化下。

 

 

一、定義一個接口,用于頁面下載狀態的監聽;

<pre class="java" name="code">import java.io.Serializable; public interface DownloadListener { public static class Data implements Serializable { private static final long serialVersionUID = 8954447444334039739L; private long currentSize; private long totalSize; public Data() { } public Data(int currentSize, int totalSize) { this.currentSize = currentSize; this.totalSize = totalSize; } public long getCurrentSize() { return currentSize; } public void setCurrentSize(long currentSize) { this.currentSize = currentSize; } public long getTotalSize() { return totalSize; } public void setTotalSize(long totalSize) { this.totalSize = totalSize; } @Override public String toString() { return "Data [currentSize=" + currentSize + ", totalSize=" + totalSize + "]"; } } /** * * @param data * : transfer downloaded data */ public void onProgress(Data data); /** * * @param e * : exception */ public void onError(Exception e); public void onCompleted(); }

 

二、定義了一個Downloader父類,為了適應不同的下載,比如使用Http進行下載;使用代理進行下載等。

所以這個類是一個接口類,定義了一些基本的操作方法。

 

package com.example.downloader;

package com.example.downloader; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.MalformedURLException; import java.net.URL; import android.util.Log; public abstract class Downloader { private static final String TAG = "Downloader"; protected URL sourceUrl; private OutputStream outputStream; private DownloadListener downloadListener = null; private File outputFile; private final int BUFFER_SIZE = 1240000;//1M // /////////////////////////// public abstract InputStream getInputStream() throws IOException; public abstract int totalDownloadSize(); public abstract void download() throws IOException, InterruptedException; // /////////////////////////// Downloader(File destFile) throws FileNotFoundException, MalformedURLException { outputFile = destFile; outputStream = new FileOutputStream(outputFile); } public void setProgressListener(DownloadListener downloadListener) { this.downloadListener = downloadListener; } public File getFile() { return outputFile; } protected void downloadFromStream() throws IOException, InterruptedException { Log.d(TAG, "Downloading from stream"); InputStream input = null; try { input = getInputStream(); throwExceptionIfInterrupted(); copyInputToOutputStream(getInputStream()); } finally { try { if (outputStream != null) { outputStream.close(); } if (input != null) { input.close(); } } catch (IOException ioe) { // ignore } } // Even if we have completely downloaded the file, we should probably // respect // the wishes of the user who wanted to cancel us. throwExceptionIfInterrupted(); } /** * stop thread * * @throws InterruptedException */ private void throwExceptionIfInterrupted() throws InterruptedException { if (Thread.interrupted()) { Log.d(TAG, "Received interrupt, cancelling download"); throw new InterruptedException(); } } protected void copyInputToOutputStream(InputStream input) throws IOException, InterruptedException { byte[] buffer = new byte[BUFFER_SIZE]; int bytesRead = 0; int totalBytes = totalDownloadSize(); throwExceptionIfInterrupted(); sendProgress(bytesRead, totalBytes); while (true) { int count = input.read(buffer); throwExceptionIfInterrupted(); bytesRead += count; sendProgress(bytesRead, totalBytes); if (count == -1) { Log.d(TAG, "Finished downloading from stream"); break; } outputStream.write(buffer, 0, count); } outputStream.flush(); } protected void sendProgress(int bytesRead, int totalBytes) { if (downloadListener != null) { DownloadListener.Data data = new DownloadListener.Data(bytesRead, totalBytes); downloadListener.onProgress(data); } } }

 

三、寫了一個HttpDownloader。繼承自Downloader

package com.example.downloader; import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; public class HttpDownloader extends Downloader { protected HttpURLConnection connection; HttpDownloader(String source, File destFile) throws FileNotFoundException, MalformedURLException { super(destFile); sourceUrl = new URL(source); } @Override public void download() throws IOException, InterruptedException { setupConnection(); downloadFromStream(); } protected void setupConnection() throws IOException { if (connection != null) { return; } connection = (HttpURLConnection) sourceUrl.openConnection(); } @Override public InputStream getInputStream() throws IOException { setupConnection(); return connection.getInputStream(); } @Override public int totalDownloadSize() { return connection.getContentLength(); } }



 四、需要一個控制方法

 

package com.example.downloader; import java.io.IOException; import com.example.downloader.DownloadListener.Data; import android.os.Handler; import android.os.Message; import android.util.Log; public class DownloadManager extends Handler { private DownloadThread downloadThread; private Downloader downloader; private DownloadListener listener; private static final int MSG_PROGRESS = 1; private static final int MSG_DOWNLOAD_COMPLETE = 2; private static final int MSG_DOWNLOAD_CANCELLED = 3; private static final int MSG_ERROR = 4; public DownloadManager(Downloader downloader, DownloadListener listener) { this.downloader = downloader; this.listener = listener; } @Override public void handleMessage(Message msg) { super.handleMessage(msg); switch (msg.what) { case MSG_PROGRESS: { DownloadListener.Data data = (Data) msg.obj; if (listener != null) { listener.onProgress(data); } break; } case MSG_DOWNLOAD_COMPLETE: { if (listener != null) { listener.onCompleted(); } break; } case MSG_DOWNLOAD_CANCELLED: { break; } case MSG_ERROR: { Exception e = (Exception) msg.obj; if (listener != null) { listener.onError(e); } break; } default: return; } } public void download() { downloadThread = new DownloadThread(); downloadThread.start(); } private class DownloadThread extends Thread implements<strong><span style="color:#ff6666;"> DownloadListener </span></strong>{ private static final String TAG = "DownloadThread"; public void run() { try { downloader.setProgressListener(this); downloader.download(); sendMessage(MSG_DOWNLOAD_COMPLETE); } catch (InterruptedException e) { sendMessage(MSG_DOWNLOAD_CANCELLED); } catch (IOException e) { Log.e(TAG, e.getMessage() + ": " + Log.getStackTraceString(e)); Message message = new Message(); message.what = MSG_ERROR; message.obj = e; sendMessage(message); } } private void sendMessage(int messageType) { Message message = new Message(); message.what = messageType; DownloadManager.this.sendMessage(message); } private void sendMessage(Message msg){ DownloadManager.this.sendMessage(msg); } @Override public void onProgress(Data data) { // TODO Message message = new Message(); message.what = MSG_PROGRESS; message.obj =data; DownloadManager.this.sendMessage(message); } @Override public void onError(Exception e) { // 在這里沒有任何用,異常被捕獲了 // do nothing } @Override public void onCompleted() { // do nothing } } }


五、頁面如何使用

<pre class="html" name="code">package com.example.downloader; import java.io.File; import java.io.FileNotFoundException; import java.net.MalformedURLException; import android.os.Bundle; import android.os.Environment; import android.app.Activity; import android.view.Menu; public class MainActivity extends Activity implements DownloadListener { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); String dir = Environment.getExternalStorageDirectory() .getAbsolutePath() + File.separator + "test.apk"; File f = new File(dir); try { Downloader downloader = new HttpDownloader( "http://192.168.131.63/fengxing2.2.0.6.apk", f); DownloadManager m = new DownloadManager(downloader, this); m.download(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (MalformedURLException e) { e.printStackTrace(); } } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } @Override public void onProgress(Data data) { System.out.println("======"+data); } @Override public void onError(Exception e) { System.out.println("======"); e.printStackTrace(); } @Override public void onCompleted() { System.out.println("======下載完成"); } }


 

 

生活不易,碼農辛苦
如果您覺得本網站對您的學習有所幫助,可以手機掃描二維碼進行捐贈
程序員人生
------分隔線----------------------------
分享到:
------分隔線----------------------------
關閉
程序員人生
主站蜘蛛池模板: 国产免费私拍一区二区三区 | 亚洲国产成人资源在线桃色 | 亚洲欧美日韩中文综合v日本 | 三级性生活视频 | 国产精品毛片在线更新 | 免费看黄在线网站 | 欧美精品v国产精品v日韩精品 | 亚洲专区一区 | 国产中文字幕视频 | 大伊人久久 | 欧美一级在线观看视频 | 在线观看中文字幕第一页 | 在线欧美一级毛片免费观看 | 香蕉视频在线网址 | 啄木乌欧美一区二区三区 | 第一福利在线观看 | 亚洲日韩欧美一区二区在线 | 欧美专区一区 | 欧美一区二区精品系列在线观看 | 欧美一级视屏 | 国产日韩欧美精品一区二区三区 | 久久精品a一国产成人免费网站 | 在线欧美69v免费观看视频 | 亚洲精品视频网 | 亚洲国产成a人v在线 | 欧美a色爱欧美综合v | 爽爽免费视频 | 在线免费看| 五月亭亭激情五月 | 亚洲国产欧美精品一区二区三区 | 亚洲动漫第一页 | free性欧美高清vide0s | 亚洲 欧美 日韩 综合 | 国产激情在线观看完整流畅 | 免费在线观看视频a | 久久久久久久综合日本亚洲 | 日本护士xxxx视频 | 秋霞午夜限制土鳖免费观看 | 国产一级一片免费播放视频 | 亚洲一区毛片 | 国产免费一区二区三区免费视频 |