要创建一个自定义的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

自定义的Notification的更多相关文章

  1. Android简易实战教程--第三十八话《自定义通知NotifiCation》

    上一篇小案例,完成了一个普通的通知,点击通知启动了一个活动.但是那里的通知没有加入些"靓点",这一篇就给它加入自定义的布局,完成自定义的通知. 应用:比如QQ音乐为例,当点击音乐播 ...

  2. 第十六章:自定义push notification sound

    前面一节已经讲过如何在ionic中集成jpush,这样我们的hybrid app在部署到ios或者android上面的时候,就可以接收通知了.如果不满足系统自带的声音,可以通过一些方式来播放自定义的通 ...

  3. Android无布局文件下自定义通知栏notification的 icon

    在开发项目一个与通知栏有关的功能时,由于自己的项目是基于插件形式的所以无法引入系统可用的布局文件,这样无法自定义布局,造成无法自定义通知栏的icon. 在网上也有一种不用布局文件更换icon的方法,但 ...

  4. 通知栏发送消息Notification(可以使用自定义的布局)

    一个简单的应用场景:假如用户打开Activity以后,按Home键,此时Activity 进入-> onPause() -> onStop() 不可见.代码在此时机发送一个Notifica ...

  5. android自定义Notification通知栏实例

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

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

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

  7. Android notification的使用

    notification出现在通知栏中的提示,特别是在4.0以后改进了不少,这里讲得都是基于4.0及4.1以后的. 分类: 1.普通Notification 2.大布局Notification 图1 ...

  8. Notification NotificationManager RemoteViews PendingIntent

    Notification和NotificationManager :可以用来实现可视化的消息通知等,比如:下载进度显示,消息显示,广播的内容等 RemoteViews :用于创建自定义的Notific ...

  9. iOS - Notification 通知

    1.Notification 通知中心实际上是在程序内部提供了消息广播的一种机制,它允许我们在低程度耦合的情况下,满足控制器与一个任意的对象进行通信的目的.每一个 iOS 程序(即每一个进程)都有一个 ...

随机推荐

  1. Mysql对象

    2.简介 2.1 存储过程 2.1.1什么是存储过程 存储过程就是一种类似函数的脚本,可以把多个sql语句组合起来,然后使用 call 存储过程名 来调用,从而执行这些SQL语句. 特点:一次编译,下 ...

  2. 01基础数据类型——list相关操作

    #列表的创建#列表是由[]来表示的,将元素放在[]中,如lst=["aa","bb",["cc","dd"," ...

  3. python3.x Day6 多线程

    线程???进程????区别???何时使用??? 进程:是程序以一个整体的形式暴露给操作系统管理,里边包含了对各种资源的调用,内存的使用,对各种资源的管理的集合,这就叫进程 线程:是操作系统最小的调度单 ...

  4. 安装bitcore

    官网----------------------------------------------https://bitcore.io先安装好 node.js v4,  npmsudo npm inst ...

  5. Python之字符串计算(计算器)

    Python之字符串计算(计算器) import re expression = '-1-2*((60+2*(-3-40.0+42425/5)*(9-2*5/3+357/553/3*99/4*2998 ...

  6. django 使用框架下auth.models自带的User进行扩展增加字段

    需要改动三个地方: 1.models.py   创建模型User,并继承原模型类AbstraUser(在此处我增加了一个新的字段手机号) from django.db import models # ...

  7. Python解释器的种类以及特点

    CPython 由C语言开发的  使用最广的解释器 IPython 基于cpython之上的一个交互式计时器 交互方式增强 功能和cpython一样 PyPy 目标是执行效率 采用JIT技术 对pyt ...

  8. Win8系统下MT4不能添加指标无法找到技术指标

    首次使用MT4平台时系统会将MT4所有文件都加载到了Windows用户目录下,导致在MT4安装位置添加了技术指标后,MT4平台也无法找到技术指标,关于这个问题的解决方法如下 MT4是一款享誉盛名的市场 ...

  9. windows开启远程

    windows开启远程桌面超级简单,跟linux相比太简单了. 补充:有瑕疵,应该是远程中的远程桌面属性打钩,但是W8.1没有这个选项,W7可以,其次创建一个管理员账户,身份是管理员,不是标准用户,要 ...

  10. iLBC 压缩比

    *---------------------------------------------------* *                                             ...