Android 通知栏显示下载进度:
AS在build中:最新
https://github.com/lingochamp/FileDownloader
compile ‘com.liulishuo.filedownloader:library:1.6.8’
public XXApplication extends Application{
...
@Override
public void onCreate() {
/**
* 仅仅是缓存Application的Context,不耗时
*/
FileDownloader.init(getApplicationContext);
}
...
}
// 通知栏进度条
private NotificationManager mNotificationManager = null;
private Notification mNotification;
Intent intent;
PendingIntent pIntent;
String url =”“;
//解决下载地址有中文乱码
try {
url = URLEncoder.encode(url, “utf-8”).replaceAll(“\+”,
“%20”);
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
url = url.replaceAll(“%3A”, “:”).replaceAll(“%2F”, “/”);
FileDownloader.getImpl().create(url)
.setPath(upgradeFilepath)
.setListener(new FileDownloadListener() {
@Override
protected void pending(BaseDownloadTask task,
int soFarBytes, int totalBytes) {
}
@Override
protected void connected(BaseDownloadTask task,
String etag, boolean isContinue, int soFarBytes,
int totalBytes) {
}
@Override
protected void progress(BaseDownloadTask task,
int soFarBytes, int totalBytes) {
Message message = handler.obtainMessage();
message.what = 0;
message.arg1 = (int) (((float) soFarBytes / totalBytes) * 100);
// Log.d("123", "下载进度+++++++++"
// + (int) (((float) count / length) * 100));
handler.sendMessage(message);
}
@Override
protected void blockComplete(BaseDownloadTask task) {
}
@Override
protected void retry(final BaseDownloadTask task,
final Throwable ex, final int retryingTimes,
final int soFarBytes) {
}
@Override
protected void completed(BaseDownloadTask task) {
Message message1 = handler.obtainMessage();
message1.what = 0;
message1.arg1 = 100;
handler.sendMessage(message1);
Log.d("123", "下载完成+++++++++");
mDownloadFlag = -1;
File file = new File(upgradeFilepath);
if (!file.exists()) {
return;
}
intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(file),
"application/vnd.android.package-archive");
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_NEW_TASK);
pIntent = PendingIntent.getActivity(SettingActivity.this,
0, intent, 0);
mNotification.flags = Notification.FLAG_AUTO_CANCEL;
mNotification.contentIntent = pIntent;
// mNotificationManager.cancelAll();
AppUtils.InstallPackage(SettingActivity.this,
upgradeFilepath);
}
@Override
protected void paused(BaseDownloadTask task,
int soFarBytes, int totalBytes) {
}
@Override
protected void error(BaseDownloadTask task, Throwable e) {
}
@Override
protected void warn(BaseDownloadTask task) {
}
}).start();
//导入安装包
public static boolean InstallPackage(Context context, String apkPath) {
try {
File file = new File(apkPath);
if (!file.exists()) {
return false;
}
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(file),
"application/vnd.android.package-archive");
context.startActivity(intent);
} catch (Exception ex) {
ex.printStackTrace();
// if(LibConstant.DEBUG)LogUtils.e("ExpansionManager:InstallPackage\n"+ex.getMessage());
return false;
}
return true;
}
private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case 0: // 更新通知栏(下载中)
mNotification.contentView.setTextViewText(
R.id.content_view_text1, msg.arg1 + “%”);
mNotification.contentView.setProgressBar(
R.id.content_view_progress, 100, msg.arg1, false);
mNotificationManager.notify(0, mNotification);
break;
case 1: // 下载完毕
break;
case 2: // 版本检查更新(向后台请求数据得到)
// RequestParams params = new RequestParams();
// params.addBodyParameter(“type”, 0 + “”);
// sendRequest(params, RequestType.UPDATE);
break;
}
}
};
private void notificationInit() {
// 通知栏内显示下载进度条
intent = new Intent();
intent.putExtra("type", 0);
intent.putExtra("installFlag", true);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_NEW_TASK);
// intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK| Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);//
pIntent = PendingIntent.getActivity(this, 0, intent, 0);
mNotificationManager = (NotificationManager) this
.getSystemService(NOTIFICATION_SERVICE);
mNotification = new Notification();
mNotification.icon = R.drawable.ic_launcher;
mNotification.tickerText = “正在下载”;
mNotification.flags = Notification.FLAG_AUTO_CANCEL;
mNotification.contentView = new RemoteViews(getPackageName(),
R.layout.”“);// 自定义通知栏中进度布局
mNotification.contentIntent = pIntent;
}