android实现下载图片在(Notification)通知栏上显示进度。
来源:互联网 发布:瓦房店天和网络 编辑:程序博客网 时间:2024/05/11 01:57
activity_main.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <Button android:id="@+id/myDownload" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="downLoadPic" android:text="开始下载" /></LinearLayout>myview.xml
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" ><ImageView android:id="@+id/myimage" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/noti_tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="50dp" android:text="15#" /> <ProgressBar android:id="@+id/noti_pd" style="?android:attr/progressBarStyleHorizontal" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignLeft="@id/noti_tv" android:layout_below="@id/noti_tv" /></RelativeLayout>MainActivity.java
package com.day11homeworknotify;import java.io.ByteArrayOutputStream;import java.io.IOException;import java.io.InputStream;import java.net.HttpURLConnection;import java.net.MalformedURLException;import java.net.URL;import android.app.Activity;import android.app.Notification;import android.app.NotificationManager;import android.app.PendingIntent;import android.content.Intent;import android.os.AsyncTask;import android.os.Bundle;import android.support.v4.app.NotificationCompat;import android.view.Menu;import android.view.MenuItem;import android.view.View;import android.widget.RemoteViews;public class MainActivity extends Activity {private Notification notification;private NotificationManager notificationManager;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);}public void downLoadPic(View view){NotificationCompat.Builder builder = new NotificationCompat.Builder(this).setContentTitle("开始下载图片").setSmallIcon(R.drawable.ic_launcher).setContentTitle("download");Intent intent = new Intent();PendingIntent contentIntent = PendingIntent.getActivity(this, R.string.app_name, intent, PendingIntent.FLAG_UPDATE_CURRENT);builder.setContentIntent(contentIntent );notification = builder.build();//开启异步任务downLoadTask dLoadTask=new downLoadTask();String urlString="http://i.guancha.cn/news/2016/01/06/20160106171107443.jpg";dLoadTask.execute(urlString);//设置一个view RemoteViews view1 = new RemoteViews(getPackageName(), R.layout.myview); notification.contentView = view1; notification.flags= Notification.FLAG_AUTO_CANCEL; notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);// notificationManager.notify(0, notification); notification.contentIntent = contentIntent;}class downLoadTask extends AsyncTask<String, Void, Void> { private int currentSize; private int currentProgess;@Override protected Void doInBackground(String... params) { String urlString =params[0]; URL url;try {url = new URL(urlString);HttpURLConnection httpURLConnection=(HttpURLConnection) url.openConnection();httpURLConnection.setRequestProperty("Accept-Encoding", "identity");int totalSize=httpURLConnection.getContentLength();//总的大小InputStream is= httpURLConnection.getInputStream();byte [] buf=new byte[100]; int len=0; ByteArrayOutputStream baos = new ByteArrayOutputStream(); while(true){ len=is.read(buf, 0, buf.length); if(len<0){ break; } currentSize = currentSize+len; currentProgess =(int) ((100.00*currentSize/totalSize));//这里如果是Current/totalSize的结果是0.注意这里的值。 // 更改文字 notification.contentView.setTextViewText(R.id.noti_tv, currentProgess + "%"); // 更改进度条 notification.contentView.setProgressBar(R.id.noti_pd, 100, currentProgess, false);
<span style="white-space:pre"></span> //设置图片 notification.contentView.setImageViewResource(R.id.myimage,R.drawable.ic_launcher); if(currentProgess==100){ // 更改文字d notification.contentView.setTextViewText(R.id.noti_tv, "下载完成"); } // 发送消息 notificationManager.notify(0, notification); } }catch (MalformedURLException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();} return null; }} }
1 0
- android实现下载图片在(Notification)通知栏上显示进度。
- Android FTPUtils工具在通知栏notification显示进度
- Notification实现下载进度显示!
- Notification实现下载进度显示!
- Notification实现下载进度显示!
- Android实现Service下载文件,Notification显示下载进度
- android 通知栏显示下载进度
- Android下载,在通知栏更新进度
- Android中使用Notification实现应用更新显示下载进度
- Android中使用Notification实现进度通知栏
- 通知栏显示下载进度
- Android——下载apk文件,并在通知栏显示下载进度
- Android 实现应用下载并在通知栏展示进度通知 打开APK文件
- android之apk自动更新采用Notification通知提示并显示下载进度
- android之apk自动更新采用Notification通知提示并显示下载进度
- 下载apk,并将进度显示在通知栏
- Android中使用Notification实现进度通知栏(Notification示例三)
- android第三更(下载附件,通知栏显示进度)
- 检测一个网址是否进行了301重定向
- jvm
- UVa 1440 - Inspection
- 使用“过滤器”的优点
- Mysql存储引擎基础
- android实现下载图片在(Notification)通知栏上显示进度。
- Serializability vs Linearizability
- scala学习(1)
- 一般人总是在期望将来的某件事情发生,比如一顿饭,一次会面
- Nuget使用入门
- 小猪的Android入门之路 Day 3 - part 3
- Python开发的IDLE调试
- UEFI + GPT模式安装Windows7
- 小猪的Android入门之路 Day 4 - part 1