filedownloader使用
来源:互联网 发布:中兴通讯数据分析 编辑:程序博客网 时间:2024/05/16 06:14
Android 通知栏显示下载进度:
AS在build中:最新
https://github.com/lingochamp/FileDownloader
compile ‘com.liulishuo.filedownloader:library:1.6.8’
public XXApplication extends Application{
...@Overridepublic 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;
}
- filedownloader使用
- 使用FileDownLoader下载文件
- 使用FileDownLoader下载文件
- lingochamp/FileDownloader
- android FileDownloader
- FileDownloader 库推荐
- Android 文件下载引擎 FileDownloader
- Android高能下载库FileDownloader
- FileDownloader-Android 文件下载引擎,稳定、高效、简单易用
- Android 文件下载引擎,稳定、高效、简单易用:FileDownloader
- 解决FileDownloader库中无全部开始下载任务接口的问题
- Android 文件下载引擎,稳定、高效、简单易用:FileDownloader(结合okhttp)
- Android 文件下载引擎,稳定、高效、简单易用:FileDownloader(结合okhttp)
- 使用
- 使用
- 使用
- 使用
- 使用
- android APP如何上线以及所做的哪些准备
- 小蓝单车解散迷局:高管分歧、退押金走后门、HR变卖家具
- Java中,&&与&,||与|的区别
- Servlet 中 session 的创建、销毁及监听 原创 2016年03月23日 18:09:33 标签:session /session超时 /在线人数统计 4916 1、session 和 c
- android的MVP架构
- filedownloader使用
- Chrome插件baiduexporter+aria2百度网盘下载
- 手机移动端网页,按屏幕大小让图片自适应屏幕。
- 在CentOS 7上安装Caffe
- Lucene(1)---入门示例
- MATLAB R2014a与VS2013混合编程(vs调用dll文件)
- 选择,插入,冒泡,二分法,归并,希尔排序算法合集演示及标注
- 创建自己的命令
- react native前端环境安装