
项目有个需求,需要在发送Notification的时候动态给定url的图片。大概思路如下:自己定义一个Notification的布局文件,这样能够很方便设置View的属性。
首先加载网络图片,使用BitmapFactory.decodeStream解析出Bitmap,然后,设置到自定义布局文件中的ImageView上。
自定义通知栏Notification布局如下:
- <?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" >
- <ImageView
- android:id="@+id/image"
- android:layout_width="45dip"
- android:layout_height="45dip"
- android:layout_alignParentLeft="true"
- android:layout_marginBottom="8.0dip"
- android:layout_marginLeft="8.0dip"
- android:layout_marginRight="10dp"
- android:layout_marginTop="8.0dip" />
- <TextView
- android:id="@+id/title"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginTop="8.0dip"
- android:layout_toRightOf="@id/image"
- android:textSize="16.0dip" />
- <TextView
- android:id="@+id/text"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_below="@id/title"
- android:layout_marginTop="3.0dip"
- android:layout_toRightOf="@id/image"
- android:textSize="16.0dip" />
- <TextView
- android:id="@+id/time"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignParentRight="true"
- android:layout_centerVertical="true"
- android:layout_marginRight="8.0dip"
- android:textSize="16.0dip" />
- </RelativeLayout>
android代码:
- 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.Context;
- import android.content.Intent;
- import android.graphics.Bitmap;
- import android.graphics.BitmapFactory;
- import android.os.AsyncTask;
- import android.os.Bundle;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.RemoteViews;
- public class MainActivity extends Activity {
- private String url = "http://www.takungpao.com/world/content/image/attachement/jpg/site2/20120605/d4bed9b92d221137df0511.jpg";
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- findViewById(R.id.btn).setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- set(url);
- }
- });
- }
- public void set(String urlStr) {
- new AsyncTask<String, Void, Bitmap>() {
- @Override
- protected Bitmap doInBackground(String... params) {
- try {
- URL url = new URL(params[0]);
- HttpURLConnection conn = (HttpURLConnection) url.openConnection();
- conn.setConnectTimeout(6000);//设置超时
- conn.setDoInput(true);
- conn.setUseCaches(false);//不缓存
- conn.connect();
- int code = conn.getResponseCode();
- Bitmap bitmap = null;
- if(code==200) {
- InputStream is = conn.getInputStream();//获得图片的数据流
- bitmap = BitmapFactory.decodeStream(is);
- }
- return bitmap;
- } catch (MalformedURLException e) {
- e.printStackTrace();
- return null;
- } catch (IOException e) {
- e.printStackTrace();
- return null;
- }
- }
- @Override
- protected void onPostExecute(Bitmap result) {
- super.onPostExecute(result);
- if (result != null) {
- showNotification(result);
- }
- }
- }.execute(urlStr);
- }
- private void showNotification(Bitmap bitmap){
- NotificationManager manager = (NotificationManager) MainActivity.this
- .getSystemService(Context.NOTIFICATION_SERVICE);
- Notification noti = new Notification();
- noti.flags = Notification.FLAG_AUTO_CANCEL;
- noti.icon = R.drawable.ic_launcher;
- // 1、创建一个自定义的消息布局 notification.xml
- // 2、在程序代码中使用RemoteViews的方法来定义image和text。然后把RemoteViews对象传到contentView字段
- RemoteViews rv = new RemoteViews(this.getPackageName(),
- R.layout.cus_noti);
- rv.setImageViewResource(R.id.image,
- R.drawable.ic_launcher);
- rv.setImageViewBitmap(R.id.image, bitmap);
- rv.setTextViewText(R.id.text,
- "Hello,this message is in a custom expanded view");
- noti.contentView = rv;
- // 3、为Notification的contentIntent字段定义一个Intent(注意,使用自定义View不需要setLatestEventInfo()方法)
- // 这儿点击后简答启动Settings模块
- PendingIntent contentIntent = PendingIntent.getActivity
- (MainActivity.this, 0,new
- Intent("android.settings.SETTINGS"), 0);
- noti.contentIntent = contentIntent;
- manager.notify(1, noti);
- }
- }