项目有个需求,需要在发送Notification的时候动态给定url的图片。大概思路如下:自己定义一个Notification的布局文件,这样能够很方便设置View的属性。

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

自定义通知栏Notification布局如下:

  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. <ImageView
  6. android:id="@+id/image"
  7. android:layout_width="45dip"
  8. android:layout_height="45dip"
  9. android:layout_alignParentLeft="true"
  10. android:layout_marginBottom="8.0dip"
  11. android:layout_marginLeft="8.0dip"
  12. android:layout_marginRight="10dp"
  13. android:layout_marginTop="8.0dip" />
  14. <TextView
  15. android:id="@+id/title"
  16. android:layout_width="wrap_content"
  17. android:layout_height="wrap_content"
  18. android:layout_marginTop="8.0dip"
  19. android:layout_toRightOf="@id/image"
  20. android:textSize="16.0dip" />
  21. <TextView
  22. android:id="@+id/text"
  23. android:layout_width="wrap_content"
  24. android:layout_height="wrap_content"
  25. android:layout_below="@id/title"
  26. android:layout_marginTop="3.0dip"
  27. android:layout_toRightOf="@id/image"
  28. android:textSize="16.0dip" />
  29. <TextView
  30. android:id="@+id/time"
  31. android:layout_width="wrap_content"
  32. android:layout_height="wrap_content"
  33. android:layout_alignParentRight="true"
  34. android:layout_centerVertical="true"
  35. android:layout_marginRight="8.0dip"
  36. android:textSize="16.0dip" />
  37. </RelativeLayout>

android代码:

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

android自定义Notification通知栏实例的更多相关文章

  1. Android自定义Notification并没有那么简单

    背景 最近需要实现一个自定义Notification的功能.网上找了找代码,解决方案就是通过RemoteViews来实现.但是在实现过程中遇到不少问题,网上也没有很好的文章描述这些问题,所以在这里做个 ...

  2. android 自定义通知栏

    package com.example.mvp; import cn.ljuns.temperature.view.TemperatureView;import presenter.ILoginPre ...

  3. Android -- 系统和自定义Notification

    Notification是一种让你的应用程序在不使用Activity的情况下警示用户,Notification是看不见的程序组件警示用户有需要注意的事件发生的最好途径. 作为UI部分,Notifica ...

  4. Android学习(二十)Notification通知栏

    一.通知栏的内容 1.图标 2.标题 3.内容 4.时间 5.点击后的相应 二.如何实现通知栏 1.获取NotificationManager. 2.显示通知栏:notify(id,notificat ...

  5. Android Notification通知栏使用

    package com.example.mynotifycation; import android.app.Activity; import android.app.Notification; im ...

  6. Android开发——Notification通知的使用及NotificationCopat.Builder常用设置API

    想要看全部设置的请看这一篇 [转]NotificationCopat.Builder全部设置 常用设置: 设置属性 说明 setAutoCancel(boolean autocancel) 设置点击信 ...

  7. android的消息通知栏

    在android的应用层中,涉及到很多应用框架,例如:Service框架,Activity管理机制,Broadcast机制,对话框框架,标题栏框架,状态栏框架,通知机制,ActionBar框架等等. ...

  8. Android开发——Notification通知的各种Style详解

    本来是想与之前讲解使用Notification通知使用一起写的,查看了资料,觉得有必要将这Style部分单独拿出来讲解 前篇:Android开发——Notification通知的使用及Notifica ...

  9. android拾遗——Android之Notification和NotificationManager

    1.使用系统自带的Notification //创建一个NotificationManager的引用 String ns = Context.NOTIFICATION_SERVICE; Notific ...

随机推荐

  1. tensorflow.python.framework.errors_impl.OutOfRangeError: FIFOQueue

    tensorflow.python.framework.errors_impl.OutOfRangeError: FIFOQueue 原创文章,请勿转载哦~!! 觉得有用的话,欢迎一起讨论相互学习~F ...

  2. File类中的list和listFiles方法

    File类中的list和listFiles方法 list()方法是返回某个目录下的所有文件和目录的文件名,返回的是String数组 listFiles()方法是返回某个目录下所有文件和目录的绝对路径, ...

  3. 基于JAVA实现的排序算法总结

    常用的排序方法有:冒泡排序.快速排序.选择排序.插入排序.归并排序,除此之外,还有基数排序.鸡尾酒排序.桶排序.鸽巢排序.希尔排序等,这里着重介绍下前半段列举的几种常见方法的实现. 1. 冒泡排序法: ...

  4. PHP可以通过类名调用非静态方法

    今日有兄弟遇上一个问题,就是可以通过class名称直接调用该类中的函数,我测试了一下,确实可以,概念中是只有静态方法才可以这样调用的,现在 被刷新了,于是我在方法中加入一行$this相关的操作,再运行 ...

  5. 洛谷 [P2766] 最长不下降子序列问题

    啊啊啊,再把MAXN和MAXM搞反我就退役 层次图求不相交路径数 第一问简单DP 第二问想办法把每一个不上升子序列转化成DAG上的一条路径,就转换成了求不相交路径数 因为每一个数只能用一次,所以要拆点 ...

  6. 关于WebApi 跨域问题的解决的方式

    最近在做WebApi 进行开发的时候 一直会遇到跨域方面的问题那么如何进行跨域问题其实非常的简单. 1.一直在使用WebApi的时候总是遇到跨域的问题 那么 什么是跨域?跨域,指的是浏览器不能执行其他 ...

  7. ERROR 1044 (42000): Access denied for user ''@'localhost' to database 'mysql' mysql> use mysql

    show databases;select user,password,host from user;我们想通过 查看存在"mysql"数据库中的user表来查看我们的msql数据 ...

  8. MDT 2013 从入门到精通之概念扫盲

    从今日开始为大家带来微软MDT 2013批量部署操作系统从入门到精通系列教程,旨在为大家以后的工作.学习提供一个便利的参考教程,以便大家更好.更深入的了解微软MDT,从而减轻企业工程师.IT从业人员及 ...

  9. 从一个实例学习----FLASK-WTF

    本案例通过实现一个注册页面的编写,来带你了解FLASK-WTF的运用. 主要功能为表单基础的功能--手机号码必须为11位数,且通过数据库查找不能有已经注册的了,密码要求输入两遍且必须一样,且所有内容不 ...

  10. 第三方页面嵌入到web项目的方案 之 使用iframe嵌入

    有些项目中可能会遇到这样的需求, 需要在一个项目中嵌入其他的项目的页面或者功能.并且需要这两个页面之间能够进行交互. 本文主要介绍如何实现这种第三方应用的嵌入, 主要有以下几个方向: 1.iframe ...