在Notification中显示下载进度

效果:


代码直接在上一篇AsyncTask示例中做修改,布局跟上次一样,这里不贴了。
MainActivity:
  1. package com.example.asynctaskdemo4;  
  2. import java.io.ByteArrayOutputStream;  
  3. import java.io.InputStream;  
  4. import org.apache.http.HttpEntity;  
  5. import org.apache.http.HttpResponse;  
  6. import org.apache.http.client.HttpClient;  
  7. import org.apache.http.client.methods.HttpGet;  
  8. import org.apache.http.impl.client.DefaultHttpClient;  
  9. import android.app.Activity;  
  10. import android.app.Notification;  
  11. import android.app.NotificationManager;  
  12. import android.app.PendingIntent;  
  13. import android.content.Intent;  
  14. import android.graphics.Bitmap;  
  15. import android.graphics.BitmapFactory;  
  16. import android.graphics.BitmapFactory.Options;  
  17. import android.os.AsyncTask;  
  18. import android.os.Bundle;  
  19. import android.support.v4.app.NotificationCompat;  
  20. import android.view.View;  
  21. import android.view.View.OnClickListener;  
  22. import android.widget.Button;  
  23. import android.widget.ImageView;  
  24. import android.widget.ProgressBar;  
  25. import android.widget.TextView;  
  26. import android.widget.Toast;  
  27. public class MainActivity extends Activity implements OnClickListener  
  28. {  
  29.     private static final String PATH = "http://h.hiphotos.baidu.com/image/w%3D2048/sign=d78504fa9d3df8dca63d8891f929738b/9f510fb30f2442a77e79f7a8d343ad4bd1130243.jpg";  
  30.     private Button but_down = null;  
  31.     private ImageView iv_show = null;  
  32.     private TextView tv_progress = null;  
  33.     private ProgressBar pb = null;  
  34.     private NotificationManager manager = null;  
  35.     private NotificationCompat.Builder builder;  
  36.     @Override  
  37.     protected void onCreate(Bundle savedInstanceState)  
  38.     {  
  39.         super.onCreate(savedInstanceState);  
  40.         setContentView(R.layout.activity_main);  
  41.         but_down = (Button) findViewById(R.id.but_down);  
  42.         iv_show = (ImageView) findViewById(R.id.iv_show);  
  43.         tv_progress = (TextView) findViewById(R.id.tv_progress);  
  44.         pb = (ProgressBar) findViewById(R.id.pb);  
  45.           
  46.         manager = (NotificationManager) MainActivity.this.getSystemService(NOTIFICATION_SERVICE);  
  47.         but_down.setOnClickListener(this);  
  48.     }  
  49.     @Override  
  50.     public void onClick(View v)  
  51.     {  
  52.         if (R.id.but_down == v.getId())  
  53.         {  
  54.             new DownloadImageTask().execute(PATH);  
  55.         }  
  56.     }  
  57.     private class DownloadImageTask extends AsyncTask<String, Integer, Bitmap>  
  58.     {  
  59.         @Override  
  60.         protected void onPostExecute(Bitmap result)  
  61.         {  
  62.             if (result != null)  
  63.             {  
  64.                 tv_progress.setVisibility(View.GONE);  
  65.                 pb.setVisibility(View.GONE);  
  66.                   
  67.                 iv_show.setImageBitmap(result);  
  68.                 builder.setProgress(0,0,true);  
  69.                 builder.setContentText("下载完成");  
  70.                 Notification no = builder.build();  
  71.                 no.flags = Notification.FLAG_AUTO_CANCEL;  
  72.                 no.defaults = Notification.DEFAULT_SOUND;  
  73.                 manager.notify(1, no);  
  74.             } else  
  75.             {  
  76.                 builder.setProgress(00true);  
  77.                 builder.setContentText("下载失败..");  
  78.                 manager.notify(1,builder.build());  
  79.                 tv_progress.setVisibility(View.GONE);  
  80.                 pb.setVisibility(View.GONE);  
  81.                 Toast.makeText(MainActivity.this,"下载失败",0).show();  
  82.             }  
  83.         }  
  84.         @Override  
  85.         protected Bitmap doInBackground(String... params)  
  86.         {  
  87.             String path = params[0];  
  88.             HttpClient client = new DefaultHttpClient();  
  89.             HttpGet get = new HttpGet(path);  
  90.             try  
  91.             {  
  92.                 HttpResponse resp = client.execute(get);  
  93.                 if(resp.getStatusLine().getStatusCode() == 200)  
  94.                 {  
  95.                     HttpEntity entity = resp.getEntity();  
  96.                     if(entity == null)  
  97.                     {  
  98.                         return null;  
  99.                     }  
  100.                     long total_length = entity.getContentLength();//获取文件总长  
  101.                     InputStream is = entity.getContent();  
  102.                     ByteArrayOutputStream bous = new ByteArrayOutputStream();  
  103.                     int len = 0;  
  104.                     byte[] buf = new byte[1024];  
  105.                     int current_len = 0;  
  106.                     int progress = 0;//当前下载进度  
  107.                     while((len = is.read(buf))!= -1)  
  108.                     {  
  109.                         current_len+=len;  
  110.                         bous.write(buf, 0, len);  
  111.                         progress = (int) ((current_len/(float)total_length)*100);  
  112.                         this.publishProgress(progress);  
  113.                     }  
  114.                     is.close();  
  115.                     byte[] data = bous.toByteArray();  
  116.                     Options opts = new Options();  
  117.                     opts.inSampleSize = 2;//简单起见直接指定缩放比例  
  118.                     Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length, opts);  
  119.                     return bitmap;  
  120.                 }  
  121.             } catch (Exception e)  
  122.             {  
  123.                 e.printStackTrace();  
  124.             }  
  125.             return null;  
  126.         }  
  127.         @Override  
  128.         protected void onProgressUpdate(Integer... values)  
  129.         {  
  130.             tv_progress.setText("下载进度:"+values[0]);  
  131.             builder.setContentText("下载进度:"+values[0]+"%");  
  132.             builder.setProgress(100, values[0], false);  
  133.             Notification no = builder.build();  
  134.             no.flags = Notification.FLAG_NO_CLEAR;  
  135.             manager.notify(1,no);  
  136.             no = null;  
  137.         }  
  138.         @Override  
  139.         protected void onPreExecute()  
  140.         {  
  141.             tv_progress.setVisibility(View.VISIBLE);  
  142.             pb.setVisibility(View.VISIBLE);  
  143.               
  144.             builder = new NotificationCompat.Builder(MainActivity.this);  
  145.             builder.setSmallIcon(R.drawable.down);  
  146.             builder.setLargeIcon(BitmapFactory.decodeResource(getResources(),R.drawable.down));  
  147.             builder.setContentTitle("美女图片");  
  148.             PendingIntent pi = PendingIntent.getActivity(MainActivity.this0new Intent(MainActivity.this,MainActivity.class),PendingIntent.FLAG_UPDATE_CURRENT);  
  149.             builder.setContentIntent(pi);  
  150.             builder.setProgress(1000false);  
  151.             manager.notify(1,builder.build());  
  152.         }  
  153.     }  
  154. }  

这里使用的是v4包的 NotificationCompat.Builder类来产生Notification实例,如果你直接new一个Notification,那应该自定义一个Notification样式,即在通知中加一个ProgressBar。
像这样:
  1. Notification no = new Notification(icon, tickerText, when);  
  2. no.contentView.setProgressBar(viewId, max, progress, indeterminate); 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值