android下载通知栏,Android开发中实现下载文件通知栏显示进度条
android开发中实现下载文件通知栏显示进度条。
1、使用asynctask异步任务实现,调用publishprogress()方法刷新进度来实现(已优化)
public class myasynctask extends asynctask {
private context context;
private notificationmanager notificationmanager;
private notificationcompat.builder builder;
public myasynctask(context context){
this.context = context;
notificationmanager = (notificationmanager) context.getsystemservice(activity.notification_service);
builder = new notificationcompat.builder(context);
}
@override
protected void onpreexecute() {
super.onpreexecute();
builder.setsmallicon(r.mipmap.ic_launcher)
.setcontentinfo("下载中...")
.setcontenttitle("正在下载");
}
@override
protected integer doinbackground(string... params) {
log.e(tag, "doinbackground: "+params[0] );
inputstream is = null;
outputstream os = null;
httpurlconnection connection = null;
int total_length = 0;
try {
url url1 = new url(params[0]);
connection = (httpurlconnection) url1.openconnection();
connection.setrequestmethod("get");
connection.setreadtimeout(50000);
connection.connect();
if(connection.getresponsecode() == 200){
is = connection.getinputstream();
os = new fileoutputstream("/sdcard/zongzhi.apk");
byte [] buf = new byte[1024];
int len;
int pro1=0;
int pro2=0;
// 获取文件流大小,用于更新进度
long file_length = connection.getcontentlength();
while((len = is.read(buf))!=-1){
total_length += len;
if(file_length>0) {
pro1 = (int) ((total_length / (float) file_length) * 100);//传递进度(注意顺序)
}
if(pro1!=pro2) {
// 调用update函数,更新进度
publishprogress(pro2=pro1);
}
os.write(buf, 0, len);
}
}
} catch (malformedurlexception e) {
e.printstacktrace();
} catch (ioexception e) {
e.printstacktrace();
}finally {
try {
if (is != null) {
is.close();
}
if (os != null) {
os.close();
}
} catch (ioexception e) {
e.printstacktrace();
}
if (connection != null) {
connection.disconnect();
}
}
return total_length;
}
@override
protected void oncancelled(integer integer) {
super.oncancelled(integer);
}
@override
protected void oncancelled() {
super.oncancelled();
}
@override
protected void onprogressupdate(integer... values) {
super.onprogressupdate(values);
builder.setprogress(100,values[0],false);
notificationmanager.notify(0x3,builder.build());
//下载进度提示
builder.setcontenttext("下载"+values[0]+"%");
if(values[0]==100) { //下载完成后点击安装
intent it = new intent(intent.action_view);
it.addflags(intent.flag_activity_new_task);
it.setdataandtype(uri.parse("file:///sdcard/zongzhi.apk"), "application/vnd.android.package-archive");
pendingintent pendingintent = pendingintent.getactivity(context, 0, it, pendingintent.flag_update_current);
builder.setcontenttitle("下载完成")
.setcontenttext("点击安装")
.setcontentinfo("下载完成")
.setcontentintent(pendingintent);
notificationmanager.notify(0x3, builder.build());
}
}
@override
protected void onpostexecute(integer integer) {
super.onpostexecute(integer);
if(integer == 100) {
toast.maketext(context, "下载完成", toast.length_short).show();
}
}
}
2、使用服务来实现(不是特别推荐此方法)
//取得系统的下载服务
downloadmanager downloadmanager= (downloadmanager) getsystemservice(context.download_service);
//创建下载请求对象
downloadmanager.request request=new downloadmanager.request(uri.parse(downurl));
request.setdestinationinexternalpublicdir("目录","文件名");
request.setnotificationvisibility(downloadmanager.request.network_mobile|downloadmanager.request.network_wifi);
request.setnotificationvisibility(downloadmanager.request.visibility_visible_notify_completed);
downloadmanager.enqueue(request);
android下载通知栏,Android开发中实现下载文件通知栏显示进度条相关推荐
- php excel 进度,在php中生成Excel文件时显示进度条
我有一个 HTML表单,当您通过单击按钮提交表单时,应用程序使用 PHPExcel生成Excel文件.一切正常,但是当excel文件很大时,等待时间很长.我想添加进度条或显示完整百分比值.我的问题是我 ...
- Android 下载文件并显示进度条
2019独角兽企业重金招聘Python工程师标准>>> OK,上一篇文章讲了上传文件到服务端,并显示进度条 那么这边文章主要讲下载文件并显示进度条. 由于简单,所以只上传代码.还是需 ...
- java 下载文件 进度条_java – 从服务器下载文件时显示进度条
如果我很了解你,你想显示一个进度条,直到你的服务器准备好发送一个文件,而不是显示蜜蜂下载的文件的进度. 如果是这样,你正在处理艰难的练习.一个可靠的进度条需要知道(相当准确)你在做什么以及需要多长时间 ...
- 使用libcurl开源库和Duilib做的下载文件并显示进度条的小工具
转载:http://blog.csdn.net/mfcing/article/details/43603525 转载:http://blog.csdn.net/infoworld/article/de ...
- C# Winform下载文件并显示进度条
private void btnDown_Click(object sender, EventArgs e) { DownloadFile("http://localhost:1928/We ...
- Delphi下载指定网址(URL)的文件,带进度条显示
主要使用的是Delphi自带的TIdhttp控件. 一.界面设置 在窗体上放置两个TEdit控件,一个用于输入要下载的文件URL,一个用于输入要保存到本地的文件路径:放置两个TLabel控件,一个显示 ...
- VC下载文件 + 显示进度条
在codeproject里找了许久,发现这样一个VC下载文件并显示进度条的源码,于是添加了些中文注释: 1.下载线程函数: UINT DownloadFile(LPVOID pParam) { CWn ...
- Android记录22-关于开发中账号绑定的逻辑处理
Android记录22-关于开发中账号绑定的逻辑处理 转载请注明:IT_xiao小巫 博客地址:http://blog.csdn.net/wwj_748 前言 本篇博客要跟大家分享的是开发中我 ...
- android仿微信图片上传进度,Android开发之模仿微信打开网页的进度条效果(高仿)...
一,为什么说是真正的高仿? 阐述这个问题前,先说下之前网上的,各位可以复制这段字,去百度一下 "仿微信打开网页的进度条效果",你会看到有很多类似的文章,不过他们有个共同点,就是实 ...
最新文章
- 项目实践:SpringBoot三招组合拳,手把手教你打出优雅的后端接口
- windows sftp工具_将SSH服务器映射成Windows网络驱动器
- Ubuntu 16.04 和 Ubuntu 18.04 启用 点击Launcher图标,窗口实现最小化 功能
- Luogu P1654 OSU! | 期望
- [flutter专题]详解AppBar小部件
- python head 函数_python爬虫中header是什么?怎么用?
- UOJ #150 【NOIP2015】 运输计划
- python模拟登录webspare_全面解读python web 程序的9种部署方式
- 3.2 Lucene实战:一个简单的小程序
- Android 在Activity界面下滑动ViewPager实现两个Fragment之间的切换?
- 【数据结构】----将一个链表拆分为两个链表
- java案例2-6:登录注册
- android 打印机 万能驱动,打印机驱动,万能打印机驱动下载,驱动程序_万能驱动下载...
- 衡水互联网服务器网站,衡水联通dns的服务器地址
- 自动化车辆的开发、测试和验证场景
- 用Python-opencv快速实现人脸识别功能(从零开始教你)(复制粘贴即可用)
- H5页面保存base64图片到本地
- 数学:确定性的丧失---第八章 不合逻辑的发展:天堂之门
- repair mysql_REPAIR TABLE语法--MySql数据库
- java古典兔子问题(java50道经典编程题)
热门文章
- bartender打印错误
- input发送a.jax_Java EE 7和JAX-RS 2.0
- 【领域泛化论文阅读】Birds of A Feather Flock Together:Category-Divergence Guidance for DomainAdaptiveSegmentat
- php讲字符串转成数组中,PHP将字符串转数组
- java-获取电池电量
- tensorflow——模型的保存和恢复tf.trian.saver()
- TCP_NODELAY/SO_LINGER/SO_NOSIGPIPE/MSG_NOSIGNAL设置
- inaflash什么意思中文_FLASH 到底是什么意思?
- Linux单机到Windows的OGG安装部署步骤
- 解决使用百度地图API时偏移问题并自适应中心点和比例