要创建一个自定义的Notification,可以使用RemoteViews。要定义自己的扩展消息,首先要初始化一个RemoteViews对象,然后将它传递给Notification contentView字段,再把PendingIntent传递给contentIntent字段。以下示例代码是完整步骤:

//1、创建一个自定义的消息布局 view.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="fill_parent" android:layout_height="fill_parent">

<ImageView android:id="@+id/image" android:layout_width="wrap_content"

android:layout_height="fill_parent" android:layout_marginRight="10dp" />

<TextView android:id="@+id/text" android:layout_width="wrap_content"

android:layout_height="fill_parent" android:textColor="#000" />

</LinearLayout>

//2、在程序代码中使用RemoteViews的方法来定义image和text。然后把RemoteViews对象传到contentView字段

RemoteViews contentView = new RemoteViews(getPackageName(),R.layout.view);

contentView.setImageViewResource(R.id.image,R.drawable.icon);

contentView.setTextViewText(R.id.text,”Hello,this message is in a custom expanded view”);

notification.contentView = contentView;

//3、为Notification的contentIntent字段定义一个Intent(注意,使用自定义View不需要setLatestEventInfo()方法)

Intent notificationIntent = new Intent(this,Main.class);

PendingIntent contentIntent = PendingIntent.getActivity(this,0,notificationIntent,0);

notification.contentIntent = contentIntent;

//4、发送通知

mNotificationManager.notify(2,notification);

//以下是全部示例代码

//创建一个NotificationManager的引用

String ns = Context.NOTIFICATION_SERVICE;

NotificationManager mNotificationManager = (NotificationManager)getSystemService(ns);

//定义Notification的各种属性

int icon = R.drawable.icon; //通知图标

CharSequence tickerText = "Hello"; //状态栏显示的通知文本提示

long when = System.currentTimeMillis(); //通知产生的时间,会在通知信息里显示

//用上面的属性初始化Nofification

Notification notification = new Notification(icon,tickerText,when);

RemoteViews contentView = new RemoteViews(getPackageName(),R.layout.view);

contentView.setImageViewResource(R.id.image, R.drawable.iconempty);

contentView.setTextViewText(R.id.text, "Hello,this is JC");

notification.contentView = contentView;

Intent notificationIntent = new Intent(this,Main.class);

PendingIntent contentIntent = PendingIntent.getActivity(this,0,notificationIntent,0);

notification.contentIntent = contentIntent;

//把Notification传递给NotificationManager

mNotificationManager.notify(0,notification);

来自:http://blog.csdn.net/iamfafa/article/details/6298011

最新文章

  1. 学android:直接用jdk来helloworld
  2. AngularJS性能优化
  3. WkHtmlToPdf 生成 PDF
  4. 在Sharepoint 2010中启用Session功能的说明文档
  5. Castle IOC容器内幕故事(上)
  6. linux 常用命令 集锦
  7. 封装DB类
  8. Jqery之select操作
  9. 2014/09/30 Learning Note
  10. 【转】这些隐藏在苹果iPhone当中的内置代码你是否知道?
  11. 基于visual Studio2013解决C语言竞赛题之0506选择排序
  12. 【大话QT之十七】Jenkins介绍及安装使用文档(与Git集成)
  13. JUnit----单元测试
  14. Amazon Aurora解读(SIGMOD 2017)
  15. GUI(JTabel表格)
  16. JavaScript(第十天)【Function类型】
  17. EasyUI中easyui-combobox的onchange事件。
  18. c#核心基础 - 浅谈 c# 中的特性 Attribute)
  19. JavaWeb-SQL-Servlet-JSP学做购物系统——日志一
  20. backbond整体架构

热门文章

  1. [Python3网络爬虫开发实战] 6.3-Ajax结果提取
  2. JavaScript中数据类型的转换规则
  3. Python之游戏开发-飞机大战
  4. Python中的数据类型之字符串
  5. NOI模拟赛(3.8)Problem B
  6. 【05】emmet系列之各种缩写
  7. 【NOIP2017】宝藏(状压DP)
  8. Linux下汇编语言学习笔记14 ---
  9. codeforces Educational Codeforces Round 39 (Rated for Div. 2) D
  10. springboot application.properties