Android 下载文件通知栏显示进度条功能的实例代码
1、使用AsyncTask异步任务实现,调用publishProgress()方法刷新进度来实现(已优化)
publicclassMyAsyncTaskextendsAsyncTask{ privateContextcontext; privateNotificationManagernotificationManager; privateNotificationCompat.Builderbuilder; publicMyAsyncTask(Contextcontext){ this.context=context; notificationManager=(NotificationManager)context.getSystemService(Activity.NOTIFICATION_SERVICE); builder=newNotificationCompat.Builder(context); } @Override protectedvoidonPreExecute(){ super.onPreExecute(); builder.setSmallIcon(R.mipmap.ic_launcher) .setContentInfo("下载中...") .setContentTitle("正在下载"); } @Override protectedIntegerdoInBackground(String...params){ Log.e(TAG,"doInBackground:"+params[0]); InputStreamis=null; OutputStreamos=null; HttpURLConnectionconnection=null; inttotal_length=0; try{ URLurl1=newURL(params[0]); connection=(HttpURLConnection)url1.openConnection(); connection.setRequestMethod("GET"); connection.setReadTimeout(50000); connection.connect(); if(connection.getResponseCode()==200){ is=connection.getInputStream(); os=newFileOutputStream("/sdcard/zongzhi.apk"); byte[]buf=newbyte[1024]; intlen; intpro1=0; intpro2=0; //获取文件流大小,用于更新进度 longfile_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(MalformedURLExceptione){ e.printStackTrace(); }catch(IOExceptione){ e.printStackTrace(); }finally{ try{ if(is!=null){ is.close(); } if(os!=null){ os.close(); } }catch(IOExceptione){ e.printStackTrace(); } if(connection!=null){ connection.disconnect(); } } returntotal_length; } @Override protectedvoidonCancelled(Integerinteger){ super.onCancelled(integer); } @Override protectedvoidonCancelled(){ super.onCancelled(); } @Override protectedvoidonProgressUpdate(Integer...values){ super.onProgressUpdate(values); builder.setProgress(100,values[0],false); notificationManager.notify(0x3,builder.build()); //下载进度提示 builder.setContentText("下载"+values[0]+"%"); if(values[0]==100){//下载完成后点击安装 Intentit=newIntent(Intent.ACTION_VIEW); it.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); it.setDataAndType(Uri.parse("file:///sdcard/zongzhi.apk"),"application/vnd.android.package-archive"); PendingIntentpendingIntent=PendingIntent.getActivity(context,0,it,PendingIntent.FLAG_UPDATE_CURRENT); builder.setContentTitle("下载完成") .setContentText("点击安装") .setContentInfo("下载完成") .setContentIntent(pendingIntent); notificationManager.notify(0x3,builder.build()); } } @Override protectedvoidonPostExecute(Integerinteger){ super.onPostExecute(integer); if(integer==100){ Toast.makeText(context,"下载完成",Toast.LENGTH_SHORT).show(); } } }
2、使用系统服务来实现(不是特别推荐此方法)
//取得系统的下载服务 DownloadManagerdownloadManager=(DownloadManager)getSystemService(Context.DOWNLOAD_SERVICE); //创建下载请求对象 DownloadManager.Requestrequest=newDownloadManager.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下载文件通知栏显示进度条功能的实例代码,希望对大家有所帮助,如果大家有任何疑问欢迎给我留言,小编会及时回复大家的!
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。