Android notification动态设置网络图片icon

来源:互联网 发布:考研中南财经大学知乎 编辑:程序博客网 时间:2024/06/11 14:01

公司项目有个需求,需要在发送Notification的时候动态加载服务器的图片,在网上找了半天,没有完整的代码。研究了半天总算实现了。大概思路如下

自定义Notification的布局文件,这样能够很方便设置View的属性。

首先加载网络图片,使用BitmapFactory.decodeStream解析出Bitmap,然后设置到自定义布局文件中的ImageView上。


自定义Notification布局如下:

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent" >  
  5.   
  6.     <ImageView  
  7.         android:id="@+id/image"  
  8.         android:layout_width="45dip"  
  9.         android:layout_height="45dip"  
  10.         android:layout_alignParentLeft="true"  
  11.         android:layout_marginBottom="8.0dip"  
  12.         android:layout_marginLeft="8.0dip"  
  13.         android:layout_marginRight="10dp"  
  14.         android:layout_marginTop="8.0dip" />  
  15.   
  16.     <TextView  
  17.         android:id="@+id/title"  
  18.         android:layout_width="wrap_content"  
  19.         android:layout_height="wrap_content"  
  20.         android:layout_marginTop="8.0dip"  
  21.         android:layout_toRightOf="@id/image"  
  22.         android:textSize="16.0dip" />  
  23.   
  24.     <TextView  
  25.         android:id="@+id/text"  
  26.         android:layout_width="wrap_content"  
  27.         android:layout_height="wrap_content"  
  28.         android:layout_below="@id/title"  
  29.         android:layout_marginTop="3.0dip"  
  30.         android:layout_toRightOf="@id/image"  
  31.         android:textSize="16.0dip" />  
  32.   
  33.     <TextView  
  34.         android:id="@+id/time"  
  35.         android:layout_width="wrap_content"  
  36.         android:layout_height="wrap_content"  
  37.         android:layout_alignParentRight="true"  
  38.         android:layout_centerVertical="true"  
  39.         android:layout_marginRight="8.0dip"  
  40.         android:textSize="16.0dip" />  
  41.   
  42. </RelativeLayout>  



Java实现代码:

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. import java.io.IOException;  
  2. import java.io.InputStream;  
  3. import java.net.HttpURLConnection;  
  4. import java.net.MalformedURLException;  
  5. import java.net.URL;  
  6.   
  7. import android.app.Activity;  
  8. import android.app.Notification;  
  9. import android.app.NotificationManager;  
  10. import android.app.PendingIntent;  
  11. import android.content.Context;  
  12. import android.content.Intent;  
  13. import android.graphics.Bitmap;  
  14. import android.graphics.BitmapFactory;  
  15. import android.os.AsyncTask;  
  16. import android.os.Bundle;  
  17. import android.view.View;  
  18. import android.view.View.OnClickListener;  
  19. import android.widget.RemoteViews;  
  20.   
  21. public class MainActivity extends Activity {  
  22.     private String url = "http://www.takungpao.com/world/content/image/attachement/jpg/site2/20120605/d4bed9b92d221137df0511.jpg";  
  23.     @Override  
  24.     protected void onCreate(Bundle savedInstanceState) {  
  25.         super.onCreate(savedInstanceState);  
  26.         setContentView(R.layout.activity_main);  
  27.         findViewById(R.id.btn).setOnClickListener(new OnClickListener() {  
  28.             @Override  
  29.             public void onClick(View v) {  
  30.                 set(url);  
  31.             }  
  32.         });  
  33.     }  
  34.     public void set(String urlStr) {  
  35.         new AsyncTask<String, Void, Bitmap>() {  
  36.             @Override  
  37.             protected Bitmap doInBackground(String... params) {  
  38.                 try {  
  39.                     URL url = new URL(params[0]);  
  40.                     HttpURLConnection conn = (HttpURLConnection) url.openConnection();    
  41.                     conn.setConnectTimeout(6000);//设置超时    
  42.                     conn.setDoInput(true);    
  43.                     conn.setUseCaches(false);//不缓存    
  44.                     conn.connect();    
  45.                     int code = conn.getResponseCode();  
  46.                     Bitmap bitmap = null;  
  47.                     if(code==200) {  
  48.                         InputStream is = conn.getInputStream();//获得图片的数据流    
  49.                         bitmap = BitmapFactory.decodeStream(is);  
  50.                     }  
  51.                     return bitmap;  
  52.                 } catch (MalformedURLException e) {  
  53.                     e.printStackTrace();  
  54.                     return null;  
  55.                 } catch (IOException e) {  
  56.                     e.printStackTrace();  
  57.                     return null;  
  58.                 }  
  59.             }  
  60.   
  61.             @Override  
  62.             protected void onPostExecute(Bitmap result) {  
  63.                 super.onPostExecute(result);  
  64.                 if (result != null) {  
  65.                     showNotification(result);  
  66.                 }  
  67.             }  
  68.         }.execute(urlStr);  
  69.     }  
  70.     private void showNotification(Bitmap bitmap){  
  71.         NotificationManager manager = (NotificationManager) MainActivity.this  
  72.                 .getSystemService(Context.NOTIFICATION_SERVICE);  
  73.         Notification noti = new Notification();  
  74.         noti.flags = Notification.FLAG_AUTO_CANCEL;  
  75.         noti.icon = R.drawable.ic_launcher;  
  76.         // 1、创建一个自定义的消息布局 notification.xml  
  77.         // 2、在程序代码中使用RemoteViews的方法来定义image和text。然后把RemoteViews对象传到contentView字段  
  78.         RemoteViews remoteView = new RemoteViews(this.getPackageName(),  
  79.                 R.layout.cus_noti);  
  80.         remoteView.setImageViewResource(R.id.image,  
  81.                 R.drawable.ic_launcher);  
  82.         remoteView.setImageViewBitmap(R.id.image, bitmap);  
  83.         remoteView.setTextViewText(R.id.text,  
  84.                 "Hello,this message is in a custom expanded view");  
  85.         noti.contentView = remoteView;  
  86.         // 3、为Notification的contentIntent字段定义一个Intent(注意,使用自定义View不需要setLatestEventInfo()方法)  
  87.   
  88.         // 这儿点击后简答启动Settings模块  
  89.          PendingIntent contentIntent = PendingIntent.getActivity  
  90.          (MainActivity.this0,new  
  91.          Intent("android.settings.SETTINGS"), 0);  
  92.          noti.contentIntent = contentIntent;  
  93.         manager.notify(1, noti);  
  94.     }  
  95. }
0 0
原创粉丝点击