和尚尼姑偷腥图片图,德军血洗乌克兰,彼岸岛2
android开发中实现下载文件通知栏显示进度条。
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(); } } }
//取得系统的下载服务 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 Manifest中meta-data扩展元素数据的配置与获取方式
Android实现获取meta-data和build.gradle的值
Android获取清单文件中的meta-data,解决碰到数值为null的问题
解决webview 第二次调用loadUrl页面不刷新的问题
解决Android WebView拦截url,视频播放加载失败的问题
Android webview加载https链接错误或无响应的解决
网友评论