Notification 通知栏定义和相关问题
来源:互联网 发布:淘宝质量好小众的店铺 编辑:程序博客网 时间:2024/06/05 20:28
<span style="font-family: Arial; line-height: 1.625; font-size: 14px;"></span><pre name="code" class="plain">
注意,关于通知(Notification)的显示类型有两种:
第一种:使用默认的形式(效果图如上显示)。具体使用是为Notification对象设置setLatestEventInfo()方法(该方法内部创建
了默认的RemoteViews对象,因此为默认显示),否则程序会报异常 ;
第二种: 使用自定义的View(RemoteViews对象)显示(功能更加自由,强大),具体方法为设置Notification对象的
contentView 属性和contentIntent属性 ,此时不需要设置setLatestEventInfo()方法。
NotificationManager是所有Notification的大管家,它的主要职责是加入/移除Notification。
NotificationManager类
通过获取系统服务来获取该对象:
NotificationManager mNotificationManager = (NotificationManager)getSystemServic(Context.NOTIFICATION_SERVICE) ;
常用方法:
public void cancelAll() 移除所有通知 (只是针对当前Context下的Notification)
public void cancel(int id) 移除标记为id的通知 (只是针对当前Context下的所有Notification)
public void notify(String tag ,int id, Notification notification) 将通知加入状态栏, 标签为tag,标记为id
public void notify(int id, Notification notification) 将通知加入状态栏,,标记为id
Notification类介绍:
常用字段
contentView 通知在状态栏的显示View(自定义,具体请看下文) ,常与contentIntent配合使用,点击该通知后,
即触发contentIntent
contentIntent 设置PendingIntent对象,点击该通知时发送该Intent
flags 设置flag位,例如FLAG_NO_CLEAR等
defaults 添加效果
tickerText 显示在状态栏中的文字
when 发送此通知的时间戳
icon 设置图标
//设置Flag位
FLAG_AUTO_CANCEL 该通知能被状态栏的清除按钮给清除掉 (我常用的点击后消失)
FLAG_NO_CLEAR 该通知不能被状态栏的清除按钮给清除掉
FLAG_ONGOING_EVENT 通知放置在正在运行
常用方法介绍:
void setLatestEventInfo(Context context , CharSequence contentTitle,CharSequence contentText,PendingIntent contentIntent)
系统通知栏点击跳转就使用此方法,如果自定义通知栏就可以使用Notification类的 字段 ( contentIntent 设置PendingIntent对象,点击该通知时发送该Intent)
功能: 显示在拉伸状态栏中的Notification属性,点击后将发送PendingIntent对象。
参数: context 上下文环境
contentTitle 状态栏中的大标题
contentText 状态栏中的小标题
contentIntent 点击后将发送PendingIntent对象
第一种:使用默认的形式
// 默认显示的的Notification
private void showDefaultNotification() { // 定义Notication的各种属性 CharSequence title = "i am new"; int icon = R.drawable.icon; long when = System.currentTimeMillis(); Notification noti = new Notification(icon, title, when + 10000); noti.flags = Notification.FLAG_INSISTENT; // 创建一个通知 Notification mNotification = new Notification(); // 设置属性值 mNotification.icon = R.drawable.icon; mNotification.tickerText = "NotificationTest"; mNotification.when = System.currentTimeMillis(); // 立即发生此通知 // 带参数的构造函数,属性值如上 // Notification mNotification = = new Notification(R.drawable.icon,"NotificationTest", System.currentTimeMillis())); // 添加声音效果 mNotification.defaults |= Notification.DEFAULT_SOUND; // 添加震动,后来得知需要添加震动权限 : Virbate Permission //mNotification.defaults |= Notification.DEFAULT_VIBRATE ; //添加状态标志 //FLAG_AUTO_CANCEL 该通知能被状态栏的清除按钮给清除掉 //FLAG_NO_CLEAR 该通知能被状态栏的清除按钮给清除掉 //FLAG_ONGOING_EVENT 通知放置在正在运行 //FLAG_INSISTENT 通知的音乐效果一直播放 mNotification.flags = Notification.FLAG_INSISTENT ; //将该通知显示为默认View PendingIntent contentIntent = PendingIntent.getActivity (MainActivity.this, 0,new Intent("android.settings.SETTINGS"), 0); mNotification.setLatestEventInfo(MainActivity.this, "通知类型:默认View", "一般般哟。。。。",contentIntent); // 设置setLatestEventInfo方法,如果不设置会App报错异常 NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); //注册此通知 // 如果该NOTIFICATION_ID的通知已存在,会显示最新通知的相关信息 ,比如tickerText 等 mNotificationManager.notify(2, mNotification); }
注意,关于通知(Notification)的显示类型有两种:
第一种:使用默认的形式(效果图如上显示)。具体使用是为Notification对象设置setLatestEventInfo()方法(该方法内部创建
了默认的RemoteViews对象,因此为默认显示),否则程序会报异常 ;
第二种: 使用自定义的View(RemoteViews对象)显示(功能更加自由,强大),具体方法为设置Notification对象的
contentView 属性和contentIntent属性 ,此时不需要设置setLatestEventInfo()方法。
NotificationManager是所有Notification的大管家,它的主要职责是加入/移除Notification。
NotificationManager类
通过获取系统服务来获取该对象:
NotificationManager mNotificationManager = (NotificationManager)getSystemServic(Context.NOTIFICATION_SERVICE) ;
常用方法:
public void cancelAll() 移除所有通知 (只是针对当前Context下的Notification)
public void cancel(int id) 移除标记为id的通知 (只是针对当前Context下的所有Notification)
public void notify(String tag ,int id, Notification notification) 将通知加入状态栏, 标签为tag,标记为id
public void notify(int id, Notification notification) 将通知加入状态栏,,标记为id
Notification类介绍:
常用字段
contentView 通知在状态栏的显示View(自定义,具体请看下文) ,常与contentIntent配合使用,点击该通知后,
即触发contentIntent
contentIntent 设置PendingIntent对象,点击该通知时发送该Intent
flags 设置flag位,例如FLAG_NO_CLEAR等
defaults 添加效果
tickerText 显示在状态栏中的文字
when 发送此通知的时间戳
icon 设置图标
//设置Flag位
FLAG_AUTO_CANCEL 该通知能被状态栏的清除按钮给清除掉 (我常用的点击后消失)
FLAG_NO_CLEAR 该通知不能被状态栏的清除按钮给清除掉
FLAG_ONGOING_EVENT 通知放置在正在运行
常用方法介绍:
void setLatestEventInfo(Context context , CharSequence contentTitle,CharSequence contentText,PendingIntent contentIntent)
系统通知栏点击跳转就使用此方法,如果自定义通知栏就可以使用Notification类的 字段 ( contentIntent 设置PendingIntent对象,点击该通知时发送该Intent)
功能: 显示在拉伸状态栏中的Notification属性,点击后将发送PendingIntent对象。
参数: context 上下文环境
contentTitle 状态栏中的大标题
contentText 状态栏中的小标题
contentIntent 点击后将发送PendingIntent对象
第一种:使用默认的形式
// 默认显示的的Notification
第二种: 使用自定义的View(RemoteViews对象)
//自定义显示的通知 ,创建RemoteView对象
private void showCustomizeNotification() { CharSequence title = "i am new"; int icon = R.drawable.icon; long when = System.currentTimeMillis();//创建通知 参数(通知的图标,标题,时间(获取当前的系统时间)) Notification noti = new Notification(icon, title, when + 10000); noti.flags = Notification.FLAG_INSISTENT; noti.flags = Notification.FLAG_AUTO_CANCEL;(我常用的) // 1、创建一个自定义的消息布局 view.xml // 2、在程序代码中使用RemoteViews的方法来定义image和text。然后把RemoteViews对象传到contentView字段 RemoteViews remoteView = new RemoteViews(this.getPackageName(),R.layout.notification); remoteView.setImageViewResource(R.id.image, R.drawable.icon); remoteView.setTextViewText(R.id.text , "通知类型为:自定义View"); noti.contentView = remoteView; // 3、为Notification的contentIntent字段定义一个Intent(注意,使用自定义View不需要setLatestEventInfo()方法) 给contentIntent设置PendingIntent对象,点击该通知时发送该Intent //这儿点击后简单启动Settings模块 PendingIntent contentIntent = PendingIntent.getActivity (MainActivity.this, 0,new Intent("android.settings.SETTINGS"), 0); noti.contentIntent = contentIntent; NotificationManager mnotiManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); mnotiManager.notify(0, noti); }
问题:当我通知栏都写好的时候 推送消息提示通知栏 然后点击跳转到页面,然后小米手机竟然不跳最后在跳转的对应Activity中注册添加一条属性:
- Notification 通知栏定义和相关问题
- 通知栏Notification和常驻通知栏
- 通知栏通知Notification
- Notification 通知栏通知
- Notification通知栏通知
- 通知栏的问题(Notification)
- notification通知栏时间标题刷新问题
- 认识Notification通知栏通知
- Notification(通知栏通知)
- Android 通知栏 Notification
- Notification(通知栏)
- Android---Notification通知栏
- Android 通知栏Notification
- Android 通知栏Notification
- Notification任务通知栏
- Notification 通知栏
- android 通知栏 notification
- Android Notification 通知栏
- Codeforces Round #289 (Div. 2, ACM ICPC Rules) E. Pretty Song
- 手把手陪你搭建Windows Server 2012 + SQL SERVER 2014群集(之群集节点服务器环境准备)
- android ffmpeg libx264编译记录
- cocos2dx 截图
- MQTT fusesource / mqtt-client 原Git项目翻译
- Notification 通知栏定义和相关问题
- win7 64位配置mysql-5.6.22-win32免安装版
- Codeforces 510C - Fox And Names (拓扑排序)
- libevent学习八
- 水面效果的2D实现(一)
- java链表实现
- Ubuntu下gcc的安装和使用简单
- 猜牌问题
- iOS开发- 蓝牙后台接收数据(BLE4.0)