編輯:關於Android編程
今天帶來一個多線程下載的 例子。先看一下效果,點擊 下載 開始下載,同時顯示下載進度,下載完成,變成程 安裝,點擊安裝 提示 安裝應用。
界面效果
這裡寫圖片描述
線程池 ThreadPoolExecutor
在下面介紹實現下載原理的時候,我想嘗試倒著來說,這樣是否好理解一點?
我們都知道,下載助手,比如360, 百度的 手機助手,下載APP 的時候 ,都可以同時下載多個,所以,下載肯定是多線程的,所以我們就需要一個線程工具類 來管理我們的線程,這個工具類的核心,就是 線程池。
線程池ThreadPoolExecutor ,先簡單學習下這個線程池的使用
/**
* Parameters:
corePoolSize
the number of threads to keep in the pool, even if they are idle, unless allowCoreThreadTimeOut is set
maximumPoolSize
the maximum number of threads to allow in the pool
keepAliveTime
when the number of threads is greater than the core, this is the maximum time that excess idle threads will wait for new tasks before terminating.
unit
the time unit for the keepAliveTime argument
workQueue
the queue to use for holding tasks before they are executed. This queue will hold only the Runnable tasks submitted by the execute method.
handler
the handler to use when execution is blocked because the thread bounds and queue capacities are reached
Throws:
IllegalArgumentException - if one of the following holds:
corePoolSize < 0
keepAliveTime < 0
maximumPoolSize <= 0
maximumPoolSize < corePoolSize
NullPointerException - if workQueue or handler is null
*/
ThreadPoolExecutor threadpool=new ThreadPoolExecutor(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, handler)
上面是 ThreadPoolExecutor的參數介紹,
第一個參數 corePoolSize : 空閒時 存在的線程數目、
第二個參數 maximumPoolSize :允許同時存在的最大線程數、
第三個參數 keepAliveTime: 這個參數是 允許空閒線程存活的時間、
第四個參數 unit : 是 時間的單位 、
第五個參數 workQueue :這個是一個容器,它裡面存放的是、 threadpool.execute(new Runnable()) 執行的線程.new Runnable()、
第六個參數 handler:當執行被阻塞時,該處理程序將被阻塞,因為線程的邊界和隊列容量達到了 。
工具類 ThreadManager
介紹完了 線程池參數,那我們就先創建一個線程管理的工具類 ThreadManager
public class ThreadManager {
public static final String DEFAULT_SINGLE_POOL_NAME = “DEFAULT_SINGLE_POOL_NAME”;
private static ThreadPoolProxy mLongPool = null;
private static Object mLongLock = new Object();
private static ThreadPoolProxy mShortPool = null;
private static Object mShortLock = new Object();
private static ThreadPoolProxy mDownloadPool = null;
private static Object mDownloadLock = new Object();
private static Map mMap = new HashMap();
private static Object mSingleLock = new Object();
/** 獲取下載線程 */
public static ThreadPoolProxy getDownloadPool() {
synchronized (mDownloadLock) {
if (mDownloadPool == null) {
mDownloadPool = new ThreadPoolProxy(3, 3, 5L);
}
return mDownloadPool;
}
}
/** 獲取一個用於執行長耗時任務的線程池,避免和短耗時任務處在同一個隊列而阻塞了重要的短耗時任務,通常用來聯網操作 */
public static ThreadPoolProxy getLongPool() {
synchronized (mLongLock) {
if (mLongPool == null) {
mLongPool = new ThreadPoolProxy(5, 5, 5L);
}
return mLongPool;
}
}
/** 獲取一個用於執行短耗時任務的線程池,避免因為和耗時長的任務處在同一個隊列而長時間得不到執行,通常用來執行本地的IO/SQL */
public static ThreadPoolProxy getShortPool() {
synchronized (mShortLock) {
if (mShortPool == null) {
mShortPool = new ThreadPoolProxy(2, 2, 5L);
}
return mShortPool;
}
}
/** 獲取一個單線程池,所有任務將會被按照加入的順序執行,免除了同步開銷的問題 */
public static ThreadPoolProxy getSinglePool() {
return getSinglePool(DEFAULT_SINGLE_POOL_NAME);
}
/** 獲取一個單線程池,所有任務將會被按照加入的順序執行,免除了同步開銷的問題 */
public static ThreadPoolProxy getSinglePool(String name) {
synchronized (mSingleLock) {
ThreadPoolProxy singlePool = mMap.get(name);
if (singlePool == null) {
singlePool = new ThreadPoolProxy(1, 1, 5L);
mMap.put(name, singlePool);
}
return singlePool;
}
}
public static class ThreadPoolProxy {
private ThreadPoolExecutor mPool;
private int mCorePoolSize;
private int mMaximumPoolSize;
private long mKeepAliveTime;
private ThreadPoolProxy(int corePoolSize, int maximumPoolSize, long keepAliveTime) {
mCorePoolSize = corePoolSize;
mMaximumPoolSize = maximumPoolSize;
mKeepAliveTime = keepAliveTime;
}
/** 執行任務,當線程池處於關閉,將會重新創建新的線程池 */
public synchronized void execute(Runnable run) {
if (run == null) {
return;
}
if (mPool == null || mPool.isShutdown()) {
mPool = new ThreadPoolExecutor(mCorePoolSize, mMaximumPoolSize, mKeepAliveTime, TimeUnit.MILLISECONDS, new LinkedBlockingQueue(), Executors.defaultThreadFactory(), new AbortPolicy());
}
mPool.execute(run);
}
/** 取消線程池中某個還未執行的任務 */
public synchronized void cancel(Runnable run) {
if (mPool != null && (!mPool.isShutdown() || mPool.isTerminating())) {
mPool.getQueue().remove(run);
}
}
/** 取消線程池中某個還未執行的任務 */
public synchronized boolean contains(Runnable run) {
if (mPool != null && (!mPool.isShutdown() || mPool.isTerminating())) {
return mPool.getQueue().contains(run);
} else {
return false;
}
}
/** 立刻關閉線程池,並且正在執行的任務也將會被中斷 */
public void stop() {
if (mPool != null && (!mPool.isShutdown() || mPool.isTerminating())) {
mPool.shutdownNow();
}
}
/** 平緩關閉單任務線程池,但是會確保所有已經加入的任務都將會被執行完畢才關閉 */
public synchronized void shutdown() {
if (mPool != null && (!mPool.isShutdown() || mPool.isTerminating())) {
mPool.shutdownNow();
}
}
}
}
這個線程池工具類 主要就是 生成一個線程池, 以及 取消線程池中的任務,查詢線程池中是否包含某一任務。
下載任務 DownloadTask
我們的現在線程 DownloadTask 就 通過 ThreadManager .getDownloadPool().execute() 方法 交給線程池去管理。
有了線程池管理我們的線程, 那我們下一步 就是 DownloadTask 這個類去下載了。
/* 下載任務 /
public class DownloadTask implements Runnable {
private DownloadInfo info;
public DownloadTask(DownloadInfo info) {
this.info = info;
}
@Override
public void run() {
info.setDownloadState(STATE_DOWNLOADING);// 先改變下載狀態
notifyDownloadStateChanged(info);
File file = new File(info.getPath());// 獲取下載文件
HttpResult httpResult = null;
InputStream stream = null;
if (info.getCurrentSize() == 0 || !file.exists()
|| file.length() != info.getCurrentSize()) {
// 如果文件不存在,或者進度為0,或者進度和文件長度不相符,就需要重新下載
info.setCurrentSize(0);
file.delete();
}
httpResult = HttpHelper.download(info.getUrl());
// else {
// // //文件存在且長度和進度相等,采用斷點下載
// httpResult = HttpHelper.download(info.getUrl() + &range= +
// info.getCurrentSize());
// }
if (httpResult == null
|| (stream = httpResult.getInputStream()) == null) {
info.setDownloadState(STATE_ERROR);// 沒有下載內容返回,修改為錯誤狀態
notifyDownloadStateChanged(info);
} else {
try {
skipBytesFromStream(stream, info.getCurrentSize());
} catch (Exception e1) {
e1.printStackTrace();
}
FileOutputStream fos = null;
try {
fos = new FileOutputStream(file, true);
int count = -1;
byte[] buffer = new byte[1024];
while (((count = stream.read(buffer)) != -1)
&& info.getDownloadState() == STATE_DOWNLOADING) {
// 每次讀取到數據後,都需要判斷是否為下載狀態,如果不是,下載需要終止,如果是,則刷新進度
fos.write(buffer, 0, count);
fos.flush();
info.setCurrentSize(info.getCurrentSize() + count);
notifyDownloadProgressed(info);// 刷新進度
}
} catch (Exception e) {
info.setDownloadState(STATE_ERROR);
notifyDownloadStateChanged(info);
info.setCurrentSize(0);
file.delete();
} finally {
IOUtils.close(fos);
if (httpResult != null) {
httpResult.close();
}
}
// 判斷進度是否和app總長度相等
if (info.getCurrentSize() == info.getAppSize()) {
info.setDownloadState(STATE_DOWNLOADED);
notifyDownloadStateChanged(info);
} else if (info.getDownloadState() == STATE_PAUSED) {// 判斷狀態
notifyDownloadStateChanged(info);
} else {
info.setDownloadState(STATE_ERROR);
notifyDownloadStateChanged(info);
info.setCurrentSize(0);// 錯誤狀態需要刪除文件
file.delete();
}
}
mTaskMap.remove(info.getId());
}
}
下載的原理 很簡單,就是 通過 目標的URL 拿到流,然後寫到本地。
因為下載在 run() 裡面執行,這個DownloadTask 類 我們就看run() 方法的實現,所以 關鍵代碼 就是下面一點點
fos = new FileOutputStream(file, true);
int count = -1;
byte[] buffer = new byte[1024];
while (((count = stream.read(buffer)) != -1)
&& info.getDownloadState() == STATE_DOWNLOADING) {
// 每次讀取到數據後,都需要判斷是否為下載狀態,如果不是,下載需要終止,如果是,則刷新進度
fos.write(buffer, 0, count);
fos.flush();
info.setCurrentSize(info.getCurrentSize() + count);
notifyDownloadProgressed(info);// 刷新進度
}
這個在我們剛接觸Java 的時候 肯定都寫過了。 這就是往本地寫數據的代碼。所以run()方法中的 前面 就是拿到 stream 輸入流, 以及 把file 創建出來。
刷新進度,狀態
關於控制 button中text 顯示 暫停 ,下載,還是進度,就靠 notifyDownloadProgressed(info);和 notifyDownloadStateChanged(info)兩個方法, 這兩個方法 實際上調用的是兩個接口,只要我們在我們需要改變界面的類裡 實現這兩個接口,就可以接收到 包含最新信息的info對象。而我們在哪個類裡改變button 上面 顯示的文字呢? 當然是在 我們的adapter 裡面了,大家都知道 是在 adapter 的getView() 方法裡面 加載的每一條數據的布局。
那就一起看下是不是這樣子呢?
public class RecommendAdapter extends BaseAdapter implements
DownloadManager.DownloadObserver {
ArrayList list;
private List mDisplayedHolders;
private FinalBitmap finalBitmap;
private Context context;
public RecommendAdapter(ArrayList list, FinalBitmap finalBitmap,
Context context) {
this.list = list;
this.context = context;
this.finalBitmap = finalBitmap;
mDisplayedHolders = new ArrayList();
}
public void startObserver() {
DownloadManager.getInstance().registerObserver(this);
}
public void stopObserver() {
DownloadManager.getInstance().unRegisterObserver(this);
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return list.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final AppInfo appInfo = list.get(position);
final ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder(context);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.setData(appInfo);
mDisplayedHolders.add(holder);
return holder.getRootView();
}
@Override
public void onDownloadStateChanged(DownloadInfo info) {
refreshHolder(info);
}
@Override
public void onDownloadProgressed(DownloadInfo info) {
refreshHolder(info);
}
public List getDisplayedHolders() {
synchronized (mDisplayedHolders) {
return new ArrayList(mDisplayedHolders);
}
}
public void clearAllItem() {
if (list != null){
list.clear();
}
if (mDisplayedHolders != null) {
mDisplayedHolders.clear();
}
}
public void addItems(ArrayList infos) {
list.addAll(infos);
}
private void refreshHolder(final DownloadInfo info) {
List displayedHolders = getDisplayedHolders();
for (int i = 0; i < displayedHolders.size(); i++) {
final ViewHolder holder = displayedHolders.get(i);
AppInfo appInfo = holder.getData();
if (appInfo.getId() == info.getId()) {
AppUtil.post(new Runnable() {
@Override
public void run() {
holder.refreshState(info.getDownloadState(),
info.getProgress());
}
});
}
}
}
public class ViewHolder {
public TextView textView01;
public TextView textView02;
public TextView textView03;
public TextView textView04;
public ImageView imageView_icon;
public Button button;
public LinearLayout linearLayout;
public AppInfo mData;
private DownloadManager mDownloadManager;
private int mState;
private float mProgress;
protected View mRootView;
private Context context;
private boolean hasAttached;
public ViewHolder(Context context) {
mRootView = initView();
mRootView.setTag(this);
this.context = context;
}
public View getRootView() {
return mRootView;
}
public View initView() {
View view = AppUtil.inflate(R.layout.item_recommend_award);
imageView_icon = (ImageView) view
.findViewById(R.id.imageview_task_app_cion);
textView01 = (TextView) view
.findViewById(R.id.textview_task_app_name);
textView02 = (TextView) view
.findViewById(R.id.textview_task_app_size);
textView03 = (TextView) view
.findViewById(R.id.textview_task_app_desc);
textView04 = (TextView) view
.findViewById(R.id.textview_task_app_love);
button = (Button) view.findViewById(R.id.button_task_download);
linearLayout = (LinearLayout) view
.findViewById(R.id.linearlayout_task);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
System.out.println(mState:173 +mState);
if (mState == DownloadManager.STATE_NONE
|| mState == DownloadManager.STATE_PAUSED
|| mState == DownloadManager.STATE_ERROR) {
mDownloadManager.download(mData);
} else if (mState == DownloadManager.STATE_WAITING
|| mState == DownloadManager.STATE_DOWNLOADING) {
mDownloadManager.pause(mData);
} else if (mState == DownloadManager.STATE_DOWNLOADED) {
// tell2Server();
mDownloadManager.install(mData);
}
}
});
return view;
}
public void setData(AppInfo data) {
if (mDownloadManager == null) {
mDownloadManager = DownloadManager.getInstance();
}
String filepath= FileUtil.getDownloadDir(AppUtil.getContext()) + File.separator + data.getName() + .apk;
boolean existsFile = FileUtil.isExistsFile(filepath);
if(existsFile){
int fileSize = FileUtil.getFileSize(filepath);
if(data.getSize()==fileSize){
DownloadInfo downloadInfo = DownloadInfo.clone(data);
downloadInfo.setCurrentSize(data.getSize());
downloadInfo.setHasFinished(true);
mDownloadManager.setDownloadInfo(data.getId(),downloadInfo );
}
// else if(fileSize>0){
// DownloadInfo downloadInfo = DownloadInfo.clone(data);
// downloadInfo.setCurrentSize(data.getSize());
// downloadInfo.setHasFinished(false);
// mDownloadManager.setDownloadInfo(data.getId(),downloadInfo );
// }
}
DownloadInfo downloadInfo = mDownloadManager.getDownloadInfo(data
.getId());
if (downloadInfo != null) {
mState = downloadInfo.getDownloadState();
mProgress = downloadInfo.getProgress();
} else {
mState = DownloadManager.STATE_NONE;
mProgress = 0;
}
this.mData = data;
refreshView();
}
public AppInfo getData() {
return mData;
}
public void refreshView() {
linearLayout.removeAllViews();
AppInfo info = getData();
textView01.setText(info.getName());
textView02.setText(FileUtil.FormetFileSize(info.getSize()));
textView03.setText(info.getDes());
textView04.setText(info.getDownloadNum() + 下載量);
finalBitmap.display(imageView_icon, info.getIconUrl());
if (info.getType().equals(0)) {
// mState = DownloadManager.STATE_READ;
textView02.setVisibility(View.GONE);
}else{
String path=FileUtil.getDownloadDir(AppUtil.getContext()) + File.separator + info.getName() + “.apk”;
hasAttached = FileUtil.isValidAttach(path, false);
DownloadInfo downloadInfo = mDownloadManager.getDownloadInfo(info
.getId());
if (downloadInfo != null && hasAttached) {
if(downloadInfo.isHasFinished()){
mState = DownloadManager.STATE_DOWNLOADED;
}else{
mState = DownloadManager.STATE_PAUSED;
}
} else {
mState = DownloadManager.STATE_NONE;
if(downloadInfo !=null){
downloadInfo.setDownloadState(mState);
}
}
}
refreshState(mState, mProgress);
}
public void refreshState(int state, float progress) {
mState = state;
mProgress = progress;
switch (mState) {
case DownloadManager.STATE_NONE:
button.setText(R.string.app_state_download);
break;
case DownloadManager.STATE_PAUSED:
button.setText(R.string.app_state_paused);
break;
case DownloadManager.STATE_ERROR:
button.setText(R.string.app_state_error);
break;
case DownloadManager.STATE_WAITING:
button.setText(R.string.app_state_waiting);
break;
case DownloadManager.STATE_DOWNLOADING:
button.setText((int) (mProgress * 100) + %);
break;
case DownloadManager.STATE_DOWNLOADED:
button.setText(R.string.app_state_downloaded);
break;
// case DownloadManager.STATE_READ:
// button.setText(R.string.app_state_read);
// break;
default:
break;
}
}
}
}
何時 注冊 監聽observer
裡面代碼有點多,那就看startObserver()方法做了什麼。
public void startObserver() {
DownloadManager.getInstance().registerObserver(this);
}
這裡 是 注冊了observer, Observer 是什麼東西?在DownloadManager 中我們定義了
public interface DownloadObserver {
public void onDownloadStateChanged(DownloadInfo info);
public void onDownloadProgressed(DownloadInfo info);
}
一個接口,裡面有兩個抽象方法 一個是 進度,另一個是下載狀態。
那回過頭來,屢一下, 我們在 下載的關鍵代碼裡面調用了 DownloadObserver onDownloadProgressed() DownloadObserver.onDownloadStateChanged()兩個抽象方法,而我們在 adapter
@Override
public void onDownloadStateChanged(DownloadInfo info) {
refreshHolder(info);
}
@Override
public void onDownloadProgressed(DownloadInfo info) {
refreshHolder(info);
}
中實現了 這兩個方法 就可以輕松的控制 去 刷新 和改變 下載狀態了。
細心的朋友 或許 發現問題了,對,我們還沒有注冊Observer,就在 DownloadManager 中去調用了。
這裡 在看下DownloadManager 中 調用的方法
/* 當下載狀態發送改變的時候回調 /
public void notifyDownloadStateChanged(DownloadInfo info) {
synchronized (mObservers) {
for (DownloadObserver observer : mObservers) {
observer.onDownloadStateChanged(info);
}
}
}
/* 當下載進度發送改變的時候回調 /
public void notifyDownloadProgressed(DownloadInfo info) {
synchronized (mObservers) {
for (DownloadObserver observer : mObservers) {
observer.onDownloadProgressed(info);
}
}
}
是的,這裡我們遍歷一個observer 容器,然後去刷新 ,所以我們還需要 把 Observer 對象 添加到 集合 mObservers 中,
所以肯定有這樣一個方法 講 observer 添加到集合中 。
/* 注冊觀察者 /
public void registerObserver(DownloadObserver observer) {
synchronized (mObservers) {
if (!mObservers.contains(observer)) {
mObservers.add(observer);
}
}
}
/* 反注冊觀察者 /
public void unRegisterObserver(DownloadObserver observer) {
synchronized (mObservers) {
if (mObservers.contains(observer)) {
mObservers.remove(observer);
}
}
}
所以最後一步,因為 adapter 方法中有 startObserver, 所以 我們在 主界面 MainActivity 的類中調用 adapter.startObser() 將 實現了 接口的adapter 對象 添加到 Observer 容器中 就可以了。
OK。大功告成!
=============================================
DownloadManager 代碼
這裡 貼一下DownloadManager 代碼
public class DownloadManager {
public static final int STATE_NONE = 0;
/* 等待中 /
public static final int STATE_WAITING = 1;
/* 下載中 /
public static final int STATE_DOWNLOADING = 2;
/* 暫停 /
public static final int STATE_PAUSED = 3;
/* 下載完畢 /
public static final int STATE_DOWNLOADED = 4;
/* 下載失敗 /
public static final int STATE_ERROR = 5;
// public static final int STATE_READ = 6;
private static DownloadManager instance;
private DownloadManager() {
}
/** 用於記錄下載信息,如果是正式項目,需要持久化保存 */
private Map mDownloadMap = new ConcurrentHashMap();
/** 用於記錄觀察者,當信息發送了改變,需要通知他們 */
private List mObservers = new ArrayList();
/** 用於記錄所有下載的任務,方便在取消下載時,通過id能找到該任務進行刪除 */
private Map mTaskMap = new ConcurrentHashMap();
public static synchronized DownloadManager getInstance() {
if (instance == null) {
instance = new DownloadManager();
}
return instance;
}
/** 注冊觀察者 */
public void registerObserver(DownloadObserver observer) {
synchronized (mObservers) {
if (!mObservers.contains(observer)) {
mObservers.add(observer);
}
}
}
/** 反注冊觀察者 */
public void unRegisterObserver(DownloadObserver observer) {
synchronized (mObservers) {
if (mObservers.contains(observer)) {
mObservers.remove(observer);
}
}
}
/** 當下載狀態發送改變的時候回調 */
public void notifyDownloadStateChanged(DownloadInfo info) {
synchronized (mObservers) {
for (DownloadObserver observer : mObservers) {
observer.onDownloadStateChanged(info);
}
}
}
/** 當下載進度發送改變的時候回調 */
public void notifyDownloadProgressed(DownloadInfo info) {
synchronized (mObservers) {
for (DownloadObserver observer : mObservers) {
observer.onDownloadProgressed(info);
}
}
}
/** 下載,需要傳入一個appInfo對象 */
public synchronized void download(AppInfo appInfo) {
// 先判斷是否有這個app的下載信息
DownloadInfo info = mDownloadMap.get(appInfo.getId());
if (info == null) {// 如果沒有,則根據appInfo創建一個新的下載信息
info = DownloadInfo.clone(appInfo);
mDownloadMap.put(appInfo.getId(), info);
}
// 判斷狀態是否為STATE_NONE、STATE_PAUSED、STATE_ERROR。只有這3種狀態才能進行下載,其他狀態不予處理
if (info.getDownloadState() == STATE_NONE
|| info.getDownloadState() == STATE_PAUSED
|| info.getDownloadState() == STATE_ERROR) {
// 下載之前,把狀態設置為STATE_WAITING,因為此時並沒有產開始下載,只是把任務放入了線程池中,當任務真正開始執行時,才會改為STATE_DOWNLOADING
info.setDownloadState(STATE_WAITING);
notifyDownloadStateChanged(info);// 每次狀態發生改變,都需要回調該方法通知所有觀察者
DownloadTask task = new DownloadTask(info);// 創建一個下載任務,放入線程池
mTaskMap.put(info.getId(), task);
ThreadManager.getDownloadPool().execute(task);
}
}
/** 暫停下載 */
public synchronized void pause(AppInfo appInfo) {
stopDownload(appInfo);
DownloadInfo info = mDownloadMap.get(appInfo.getId());// 找出下載信息
if (info != null) {// 修改下載狀態
info.setDownloadState(STATE_PAUSED);
notifyDownloadStateChanged(info);
}
}
/** 取消下載,邏輯和暫停類似,只是需要刪除已下載的文件 */
public synchronized void cancel(AppInfo appInfo) {
stopDownload(appInfo);
DownloadInfo info = mDownloadMap.get(appInfo.getId());// 找出下載信息
if (info != null) {// 修改下載狀態並刪除文件
info.setDownloadState(STATE_NONE);
notifyDownloadStateChanged(info);
info.setCurrentSize(0);
File file = new File(info.getPath());
file.delete();
}
}
/** 安裝應用 */
public synchronized void install(AppInfo appInfo) {
stopDownload(appInfo);
DownloadInfo info = mDownloadMap.get(appInfo.getId());// 找出下載信息
if (info != null) {// 發送安裝的意圖
Intent installIntent = new Intent(Intent.ACTION_VIEW);
installIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
installIntent.setDataAndType(Uri.parse(file:// + info.getPath()),
application/vnd.android.package-archive);
AppUtil.getContext().startActivity(installIntent);
}
notifyDownloadStateChanged(info);
}
/** 啟動應用,啟動應用是最後一個 */
public synchronized void open(AppInfo appInfo) {
try {
Context context = AppUtil.getContext();
// 獲取啟動Intent
Intent intent = context.getPackageManager()
.getLaunchIntentForPackage(appInfo.getPackageName());
context.startActivity(intent);
} catch (Exception e) {
}
}
/** 如果該下載任務還處於線程池中,且沒有執行,先從線程池中移除 */
private void stopDownload(AppInfo appInfo) {
DownloadTask task = mTaskMap.remove(appInfo.getId());// 先從集合中找出下載任務
if (task != null) {
ThreadManager.getDownloadPool().cancel(task);// 然後從線程池中移除
}
}
/** 獲取下載信息 */
public synchronized DownloadInfo getDownloadInfo(long id) {
return mDownloadMap.get(id);
}
public synchronized void setDownloadInfo(long id, DownloadInfo info) {
mDownloadMap.put(id, info);
}
/** 下載任務 */
public class DownloadTask implements Runnable {
private DownloadInfo info;
public DownloadTask(DownloadInfo info) {
this.info = info;
}
@Override
public void run() {
info.setDownloadState(STATE_DOWNLOADING);// 先改變下載狀態
notifyDownloadStateChanged(info);
File file = new File(info.getPath());// 獲取下載文件
HttpResult httpResult = null;
InputStream stream = null;
if (info.getCurrentSize() == 0 || !file.exists()
|| file.length() != info.getCurrentSize()) {
// 如果文件不存在,或者進度為0,或者進度和文件長度不相符,就需要重新下載
info.setCurrentSize(0);
file.delete();
}
httpResult = HttpHelper.download(info.getUrl());
// else {
// // //文件存在且長度和進度相等,采用斷點下載
// httpResult = HttpHelper.download(info.getUrl() + &range= +
// info.getCurrentSize());
// }
if (httpResult == null
|| (stream = httpResult.getInputStream()) == null) {
info.setDownloadState(STATE_ERROR);// 沒有下載內容返回,修改為錯誤狀態
notifyDownloadStateChanged(info);
} else {
try {
skipBytesFromStream(stream, info.getCurrentSize());
} catch (Exception e1) {
e1.printStackTrace();
}
FileOutputStream fos = null;
try {
fos = new FileOutputStream(file, true);
int count = -1;
byte[] buffer = new byte[1024];
while (((count = stream.read(buffer)) != -1)
&& info.getDownloadState() == STATE_DOWNLOADING) {
// 每次讀取到數據後,都需要判斷是否為下載狀態,如果不是,下載需要終止,如果是,則刷新進度
fos.write(buffer, 0, count);
fos.flush();
info.setCurrentSize(info.getCurrentSize() + count);
notifyDownloadProgressed(info);// 刷新進度
}
} catch (Exception e) {
info.setDownloadState(STATE_ERROR);
notifyDownloadStateChanged(info);
info.setCurrentSize(0);
file.delete();
} finally {
IOUtils.close(fos);
if (httpResult != null) {
httpResult.close();
}
}
// 判斷進度是否和app總長度相等
if (info.getCurrentSize() == info.getAppSize()) {
info.setDownloadState(STATE_DOWNLOADED);
notifyDownloadStateChanged(info);
} else if (info.getDownloadState() == STATE_PAUSED) {// 判斷狀態
notifyDownloadStateChanged(info);
} else {
info.setDownloadState(STATE_ERROR);
notifyDownloadStateChanged(info);
info.setCurrentSize(0);// 錯誤狀態需要刪除文件
file.delete();
}
}
mTaskMap.remove(info.getId());
}
}
public interface DownloadObserver {
public abstract void onDownloadStateChanged(DownloadInfo info);
public abstract void onDownloadProgressed(DownloadInfo info);
}
/* 重寫了Inpustream 中的skip(long n) 方法,將數據流中起始的n 個字節跳過 */
private long skipBytesFromStream(InputStream inputStream, long n) {
long remaining = n;
// SKIP_BUFFER_SIZE is used to determine the size of skipBuffer
int SKIP_BUFFER_SIZE = 10000;
// skipBuffer is initialized in skip(long), if needed.
byte[] skipBuffer = null;
int nr = 0;
if (skipBuffer == null) {
skipBuffer = new byte[SKIP_BUFFER_SIZE];
}
byte[] localSkipBuffer = skipBuffer;
if (n <= 0) {
return 0;
}
while (remaining > 0) {
try {
long skip = inputStream.skip(10000);
nr = inputStream.read(localSkipBuffer, 0,
(int) Math.min(SKIP_BUFFER_SIZE, remaining));
} catch (IOException e) {
e.printStackTrace();
}
if (nr < 0) {
break;
}
remaining -= nr;
}
return n - remaining;
}
}
有兩點 需要說明,關於 點擊暫停後,再繼續下載 有兩種方式可以實現
第一種 點擊暫停的時候 記錄下載了 多少,然後 再點擊 繼續下載 時,告訴服務器, 讓服務器接著 上次的數據 往本地傳遞,
代碼 就是 我們 DownloadTask 下載時候,判斷一下
// //文件存在且長度和進度相等,采用斷點下載
httpResult = HttpHelper.download(info.getUrl() + “&range=” + info.getCurrentSize());
通過 range 來區分 當前的下載size.
服務器 處理的代碼 也很簡單 就是一句話
String range = req.getParameter(“range”); 拿到 range 判斷 range 存在不存在。
如果不存在
FileInputStream stream = new FileInputStream(file);
int count = -1;
byte[] buffer = new byte[1024];
while ((count = stream.read(buffer)) != -1) {
SystemClock.sleep(20);
out.write(buffer, 0, count);
out.flush();
}
stream.close();
out.close();
如果存在 那麼跳過range 個字節
RandomAccessFile raf = new RandomAccessFile(file, “r”);
raf.seek(Long.valueOf(range));
int count = -1;
byte[] buffer = new byte[1024];
while ((count = raf.read(buffer)) != -1) {
SystemClock.sleep(10);
out.write(buffer, 0, count);
out.flush();
}
raf.close();
out.close();
另一種方式是本地處理,這個demo 中就是本地處理的, 但是有一個問題, 因為 Java api的原因 ,inputStream.skip() 方法 並不能准確的 跳過多少個字節,而是 小於你想要跳過的字節,所以 你要去遍歷 一直到 滿足你要跳過的字節 在繼續寫, 因為 這樣的方法有一個缺點,就是在下載很大的文件,比如文件大小20M ,當已經下載了15M 此時你去暫停,在繼續下載,那麼要跳過前面的15M 將會話費很多時間。
所以這個僅限於學習。實際中 如果要下載大的文件,不能用這種方法。
一、簡介 Android應用程序中一般都有多個Activity,在Activity中,通過調用StartActivity方法,並在該方法的參數中傳遞Intent對象,就可
一、ListView的簡單用法首先創建一個項目:ListViewDemoandroid:layout_width="match_parent"andr
前言:可能Android的事件分發對於剛學Android的童鞋來說接觸得不多,這樣不奇怪。因為剛學的時候,一般人很難注意到或是會選擇主動去了解。那麼究竟什麼是Androi
背景一般情況下,為了讓用戶更方便的打開應用,程序會在桌面上生成一些快捷方式。本來呢,如果是原生的桌面,其實是十分簡單,直接調用系統相關的API就行了。但是眾多的系統廠商以