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
原创粉丝点击