实例介绍
【实例简介】
【实例截图】
【实例截图】
【核心代码】
package com.huashengmi.ui.android.ui.download.common;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.ContentValues;
import android.content.Context;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
import com.huashengmi.ui.android.ui.download.db.DownloadColumn;
import com.huashengmi.ui.android.ui.download.db.DownloadManager;
import com.huashengmi.ui.android.ui.download.http.AndroidHttpClient;
import com.huashengmi.ui.android.utils.Globals;
import org.apache.http.Header;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.params.ClientPNames;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.HttpParams;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.util.Map;
/**
* Created by huangsm on 2014/7/28 0028.
* Email:huangsanm@foxmail.com
*/
public class DownloadThread extends Thread {
private final static String TAG = "DownloadThread";
private final static int NOTIFICATION_STATUS_FLAG_UNERROR = -2;
private final static int NOTIFICATION_STATUS_FLAG_ERROR = -1;
private final static int NOTIFICATION_STATUS_FLAG_NORMAL = 1;
private final static int NOTIFICATION_STATUS_FLAG_PAUSE = 2;
private final static int NOTIFICATION_STATUS_FLAG_DELETE = 3;
private final static int NOTIFICATION_STATUS_FLAG_DONE = 4;
private final static int BUFFER_SIZE = 1024 * 8;
private final static int NOTIFY_INTERVAL = 1000;
private final static String TEMP_SUFFIX = ".smdownload";
private Context mContext;
//private int mDownloadID;
//private Notification mNotification;
private NotificationCompat.Builder mBuilder;
private File mTempFile;
//private DownloadItem mDownloadItem;
private DownloadManager mDownloadManager;
private NotificationManager mNotificationManager;
private int mTempSize;
//标题
private String mTitle;
//下载ID
private int mDownloadID;
//下载地址
private String mUri;
//后缀名
private String mSuffix;
//4.0
public DownloadThread(Context context, int downloadID, String title, String uri, String suffix, NotificationCompat.Builder builder) {
mContext = context;
mDownloadID = downloadID;
mUri = uri;
mSuffix = suffix;
mTitle = title;
Globals.log(mDownloadID "," mUri "," mSuffix "," mTitle);
mBuilder = builder;
mNotificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
mDownloadManager = new DownloadManager(mContext.getContentResolver());
mTempFile = new File(DownloadUtils.getFileDir(), mTitle TEMP_SUFFIX);
/*mDownloadItem = mDownloadManager.queryTask(downloadID);
Globals.log("DownloadThread:" mDownloadItem);*/
// if (mDownloadItem != null) {
//download finish update path value
// }
//notify
notifyNotification(NOTIFICATION_STATUS_FLAG_NORMAL, "", 100, 0);
}
/*public DownloadThread(Context context, DownloadItem item, NotificationCompat.Builder builder) {
mContext = context;
mBuilder = builder;
mNotificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
mDownloadManager = new DownloadManager(mContext.getContentResolver());
mDownloadItem = item;
Globals.log("DownloadThread:" mDownloadItem);
if (mDownloadItem != null) {
//download finish update path value
mTempFile = new File(mDownloadItem.getPath(), mDownloadItem.getName() TEMP_SUFFIX);
}
//notify
notifyNotification(NOTIFICATION_STATUS_FLAG_NORMAL, "", 100, 0);
}*/
/*//兼容低版本
public DownloadThread(Context context, int downloadID, Notification notification){
mContext = context;
mDownloadID = downloadID;
mNotification = notification;
}*/
@Override
public void run() {
AndroidHttpClient aClient = null;
//HttpClient aClient = null;
BufferedInputStream bufferedInputStream = null;
try {
aClient = AndroidHttpClient.newInstance("Linux; Android");
HttpGet request = new HttpGet(mUri);
HttpResponse response = aClient.execute(request);
long totalSize = response.getEntity().getContentLength();
//check storage
if (totalSize > 0) {
//更新数据库
ContentValues values = new ContentValues();
values.put(DownloadColumn.TOTAL_BYTE, totalSize);
int result = mDownloadManager.updateTask(mDownloadID, values);
if (result > 0) {
Globals.log("update database:" result);
}
}
if (mTempFile.exists()) {
mTempSize = (int) mTempFile.length();
request.addHeader("RANGE", "bytes=" mTempFile.length() "-");
aClient.close();
aClient = AndroidHttpClient.newInstance("Linux; Android");
response = aClient.execute(request);
}
long storage = DownloadUtils.getAvailableStorage();
if (totalSize - mTempSize > storage) {
notifyNotification(NOTIFICATION_STATUS_FLAG_ERROR, "您的手机内存不足", 0, 0);
return;
}
RandomAccessFile outStream = new RandomAccessFile(mTempFile, "rwd");
outStream.seek(mTempSize);
InputStream is = response.getEntity().getContent();
byte[] buffer = new byte[BUFFER_SIZE];
bufferedInputStream = new BufferedInputStream(is, BUFFER_SIZE);
int b = 0;
long updateStart = System.currentTimeMillis();
long updateDelta = 0;
int progress = mTempSize;
while (true) {
//check download status
if (checkDownloadCancel()) {
notifyNotification(NOTIFICATION_STATUS_FLAG_DELETE, "下载已被取消", 0, 0);
aClient.close();
break;
}
if (checkDownloadPause()) {
notifyNotification(NOTIFICATION_STATUS_FLAG_DELETE, "暂停下载", 0, 0);
aClient.close();
break;
}
b = bufferedInputStream.read(buffer, 0, BUFFER_SIZE);
if (b == -1) {
break;
}
outStream.write(buffer, 0, b);
progress = b;
//1s 更新一次通知栏
if (updateDelta > NOTIFY_INTERVAL) {
Globals.log(progress);
notifyNotification(NOTIFICATION_STATUS_FLAG_NORMAL, "", (int) totalSize, progress);
updateStart = System.currentTimeMillis();
//更新下载进度
updateProgress(progress);
}
//时间
updateDelta = System.currentTimeMillis() - updateStart;
}
is.close();
//下载完成
if (progress == totalSize) {
notifyNotification(NOTIFICATION_STATUS_FLAG_DONE, "下载完成,点此安装", progress, (int) totalSize);
}
} catch (Exception e) {
e.printStackTrace();
notifyNotification(NOTIFICATION_STATUS_FLAG_ERROR, "下载出错:" e.getMessage(), 0, 0);
} finally {
try {
aClient.close();
if (bufferedInputStream != null) {
bufferedInputStream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 检查下载是否被删除
*
* @return
*/
private boolean checkDownloadCancel() {
return mDownloadManager.checkTaskStatus(mDownloadID, DownloadStatus.STATUS_DELETEED);
}
/**
* 检查下载是否被暂停
*
* @return
*/
private boolean checkDownloadPause() {
return mDownloadManager.checkTaskStatus(mDownloadID, DownloadStatus.STATUS_PAUSED);
}
private void updateProgress(long progress) {
ContentValues values = new ContentValues();
values.put(DownloadColumn.CURRENT_BYTE, progress);
int result = mDownloadManager.updateTask(mDownloadID, values);
if (result > 0) {
Globals.log("updateProgress" result);
}
}
private void notifyNotification(int action, String msg, int totalByte, int currentByte) {
switch (action) {
case NOTIFICATION_STATUS_FLAG_UNERROR:
case NOTIFICATION_STATUS_FLAG_ERROR:
mBuilder.setContentTitle(mTitle);
mBuilder.setContentText(msg);
mBuilder.setProgress(0, 0, false);
break;
case NOTIFICATION_STATUS_FLAG_NORMAL:
mBuilder.setContentTitle(mTitle);
mBuilder.setProgress(totalByte, currentByte, false);
mBuilder.setContentText("");
break;
case NOTIFICATION_STATUS_FLAG_PAUSE:
mBuilder.setContentTitle(mTitle);
mBuilder.setContentText(msg);
mBuilder.setProgress(0, 0, false);
break;
case NOTIFICATION_STATUS_FLAG_DELETE:
mBuilder.setContentTitle(mTitle);
mBuilder.setContentText(msg);
mBuilder.setProgress(0, 0, false);
mBuilder.setAutoCancel(true);
break;
case NOTIFICATION_STATUS_FLAG_DONE:
downloadFinish(msg, currentByte, totalByte);
break;
}
//mBuilder.setWhen(System.currentTimeMillis());
mNotificationManager.notify(mDownloadID, mBuilder.build());
}
private void downloadFinish(String msg, int currentByte, int totalByte) {
//update db
ContentValues values = new ContentValues();
values.put(DownloadColumn.TOTAL_BYTE, totalByte);
values.put(DownloadColumn.CURRENT_BYTE, currentByte);
values.put(DownloadColumn.STATUS, DownloadStatus.STATUS_SUCCESSFUL);
int row = mDownloadManager.updateTask(mDownloadID, values);
if (row > 0) {
//rename
File file = new File(DownloadUtils.getFileDir(), mTitle mSuffix);
if (mTempFile.renameTo(file)) {
//delete tempfile
mTempFile.delete();
PendingIntent pi = PendingIntent.getActivity(mContext, 0, DownloadUtils.installApk(mContext, file), PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(pi);
mBuilder.setContentTitle(mTitle).setContentText(msg);
mBuilder.setProgress(0, 0, false);//remove progressbar
}
}
}
}
好例子网口号:伸出你的我的手 — 分享!
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明


网友评论
我要评论