自定义的Notification
来源:互联网 发布:淘宝代理招聘 编辑:程序博客网 时间:2024/05/21 17:01
要创建一个自定义的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);
- 自定义的Notification
- 使用自定义的 Notification
- Android 自定义的notification
- 自定义的Notification布局
- android自定义的notification
- 自定义的Notification布局
- 默认的Notification和自定义的Notification
- Notification的使用(自定义Notification布局)
- 实现自定义布局的Notification
- 自定义Notification,Notificaton的使用,
- Notification&&自定义Notification
- 顶部通知 Notification的使用,及自定义Notification
- 自定义notification
- 自定义Notification
- 自定义Notification
- 自定义Notification~~
- 自定义Notification
- 自定义Notification
- Customize tag lib 1 (freemarker)
- 导向恶意网站的搜索引擎搜索结果
- 关于shell unix下,直接执行shell与sh 执行 或加 . 脚本 的区别及含义
- 会感染电脑的恶意Flash文件
- 使用PHP进行压缩/解压缩文件操作技巧汇总
- 自定义的Notification
- dot Net 线程相关问题
- obj,lib,dll,exe的关系
- Customize tag lib 2
- struts2中点击textfiled出现日历的代码
- ZOJ3305 Get Sauce(最大流)
- MFC中CMainFrame类CDoc类CView类CApp类之间关系
- MFC中CMainFrame类CDoc类CView类CApp类之间关系
- COOKIES原理以及解析