日本搞逼视频_黄色一级片免费在线观看_色99久久_性明星video另类hd_欧美77_综合在线视频

國內(nèi)最全I(xiàn)T社區(qū)平臺(tái) 聯(lián)系我們 | 收藏本站
阿里云優(yōu)惠2
您當(dāng)前位置:首頁 > 互聯(lián)網(wǎng) > F-droid 源碼片段(二)下載模塊整理

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

來源:程序員人生   發(fā)布時(shí)間:2014-10-06 08:00:00 閱讀次數(shù):2614次

 這篇文章把F-droid的下載功能經(jīng)過修改單獨(dú)拿出來,而且做了一個(gè)demo。

希望能對(duì)自己后續(xù)起到借鑒作用。各位童鞋也可以去進(jìn)行下載。

 

其實(shí)主要的思想有2個(gè)

 

1、使用接口進(jìn)行回調(diào)

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

 

 

項(xiàng)目下載地址:

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

 


尚未完成的功能:

1、斷點(diǎn)下載(需要數(shù)據(jù)庫)

2、如果下載完成了,下次下載應(yīng)該是不需要下載了。

 

缺陷:

一個(gè)下載線程只能注冊(cè)一個(gè)監(jiān)聽,比較好的辦法是可以使用觀察者模式通知各個(gè)頁面。后續(xù)進(jìn)行優(yōu)化下。

 

 

一、定義一個(gè)接口,用于頁面下載狀態(tài)的監(jiān)聽;

<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(); }

 

二、定義了一個(gè)Downloader父類,為了適應(yīng)不同的下載,比如使用Http進(jìn)行下載;使用代理進(jìn)行下載等。

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

 

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); } } }

 

三、寫了一個(gè)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(); } }



 四、需要一個(gè)控制方法

 

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("======下載完成"); } }


 

 

生活不易,碼農(nóng)辛苦
如果您覺得本網(wǎng)站對(duì)您的學(xué)習(xí)有所幫助,可以手機(jī)掃描二維碼進(jìn)行捐贈(zèng)
程序員人生
------分隔線----------------------------
分享到:
------分隔線----------------------------
關(guān)閉
程序員人生
主站蜘蛛池模板: 成人a在线 | 爱情岛论坛首页网址 | 日韩久久激情 | 日韩黄视频 | 日韩视频在线免费观看 | 欧美国产在线视频 | 亚洲三级在线播放 | 亚洲一区二区在线观看视频 | 国产嫩草影院 | 亚洲视频 欧美视频 | 在线观看av网站永久 | 蜜桃视频一区二区三区在线观看 | 婷婷婷婷色 | 黄色av一区 | 黄网址在线免费观看 | 日本国产一区 | 日韩 国产 在线 | 国产精品久久久久久久 | 久久精品福利视频 | 成人福利网| 在线观看不卡av | 中文字幕一区二区三区在线视频 | 天堂av在线免费观看 | 国产精品久久久久久妇 | 日韩欧美国产高清 | 亚洲精品美女久久久久网站 | ree性亚洲88av | 中文字幕+乱码+中文字 | 成人免费大片在线观看 | 欧美午夜电影在线观看 | 野花成人免费视频 | 91精产品一区一区三区 | 国产成人精品免费视频大全最热 | 91精品国产高清一区二区三区 | 91一区| 欧美国产高清 | 欧美在线观看一区二区 | 黄色免费视频在线观看 | 国产欧美一区二区精品久导航 | 麻豆av在线播放 | 亚洲精品在线观 |