Notification使用以及PendingIntent.getActivity() (转)
public void sendNotification(Context ctx,String message)
{
//get the notification manager
String ns = Context.NOTIFICATION_SERVERS;
NotificationManager nm = (Notification) ctx.getSystemService(ns); //create notification object includes icon ,text ,the time to send
int icon = R.drawable.robot;
Charquence tickerText = "hello";
long when = System.currentTimeMills();//return the system current time in milliseconds since 1970 Notification notification = new Notification(icon,tickerText,when); //set contextView by using setLastestEventInfo
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("http://www.google.com"));
PendingIntent pi = PendingIntent.getActivity(ctx,0,intent,0);//explain it below
notification.setLastestEventInfo(ctx,"title","text",pi); //send notification
nm.notify(1,notification);//通知加入状态栏,标记为id=1
}//above all is how to use Notification
个人水平有限,接触到的暂时是这么多,因此当我系统了解完了之后会对此做一个补充
Notification 的使用需要以上四个步骤。
简单的说PendingIntent.getActivity()就是即将发生的intent.
PendingIntent.getActivity(ctx,o,intent,o)官方文档是这样解释的:
public static PendingIntent getActivity (Context context, int requestCode, Intent intent, int flags)
Retrieve a PendingIntent that will start a new activity, like calling Context.startActivity(Intent)
. Note that the activity will be started outside of the context of an existing activity, so you must use the Intent.FLAG_ACTIVITY_NEW_TASK
launch flag in the Intent.
Parameters
context | The Context in which this PendingIntent should start the activity. |
---|---|
requestCode | Private request code for the sender (currently not used). 现在不使用了 |
intent | Intent of the activity to be launched. |
flags | May be FLAG_ONE_SHOT , FLAG_NO_CREATE , FLAG_CANCEL_CURRENT , FLAG_UPDATE_CURRENT , or any of the flags as supported byIntent.fillIn() to control which unspecified parts of the intent that can be supplied when the actual send happens. |
Returns
- Returns an existing or new PendingIntent matching the given parameters. May return null only if
FLAG_NO_CREATE
has been supplied.
一下摘自一个人的博客,只是比官方文档好理解点:
下面来谈谈notification,这个notification一般用在电话,短信,邮件,闹钟铃声,在手机的状态栏上就会出现一个小图标,提示用户处理这个快讯,这时手从上方滑动状态栏就可以展开并处理这个快讯。发现这个功能特别好用,所以我就根据我的理解来谈谈。摘自帮助文档 : notification类表示一个持久的通知,将提交给用户使用NotificationManager。已添加的Notification.Builder,使其更容易构建通知。notification是一种让你的应用程序在没有开启情况下或在后台运行警示用户。它是看不见的程序组件(Broadcast Receiver,Service和不活跃的Activity)警示用户有需要注意的事件发生的最好途径。
先来区分以下状态栏和状态条的区别:
1、状态条就是手机屏幕最上方的一个条形状的区域;
在状态条有好多信息量:比如usb连接图标,手机信号图标,电池电量图标,时间图标等等;
2、状态栏就是手从状态条滑下来的可以伸缩的view;
在状态栏中一般有两类(使用FLAG_标记):
(1)正在进行的程序;
(2)是通知事件;
大概来描述创建一个Notification传送的信息有:
1、一个状态条图标;
2、在拉伸的状态栏窗口中显示带有大标题,小标题,图标的信息,并且有处理该点击事件:比如调用该程序的入口类;
3、闪光,LED,或者震动;
快速创建一个Notification的步骤简单可以分为以下四步:
第一步:通过getSystemService()方法得到NotificationManager对象;
第二步:对Notification的一些属性进行设置比如:内容,图标,标题,相应notification的动作进行处理等等;
第三步:通过NotificationManager对象的notify()方法来执行一个notification的快讯;
第四步:通过NotificationManager对象的cancel()方法来取消一个notificatioin的快讯;
下面对Notification类中的一些常量,字段,方法简单介绍一下:
常量:
DEFAULT_ALL 使用所有默认值,比如声音,震动,闪屏等等
DEFAULT_LIGHTS 使用默认闪光提示
DEFAULT_SOUNDS 使用默认提示声音
DEFAULT_VIBRATE 使用默认手机震动
【说明】:加入手机震动,一定要在manifest.xml中加入权限:
<uses-permission android:name="android.permission.VIBRATE" />
以上的效果常量可以叠加,即通过
mNotifaction.defaults =DEFAULT_SOUND | DEFAULT_VIBRATE ;
或mNotifaction.defaults |=DEFAULT_SOUND (最好在真机上测试,震动效果模拟器上没有)
//设置flag位
FLAG_AUTO_CANCEL 该通知能被状态栏的清除按钮给清除掉
FLAG_NO_CLEAR 该通知能被状态栏的清除按钮给清除掉
FLAG_ONGOING_EVENT 通知放置在正在运行
FLAG_INSISTENT 是否一直进行,比如音乐一直播放,知道用户响应
常用字段:
contentIntent 设置PendingIntent对象,点击时发送该Intent
defaults 添加默认效果
flags 设置flag位,例如FLAG_NO_CLEAR等
icon 设置图标
sound 设置声音
tickerText 显示在状态栏中的文字
when 发送此通知的时间戳
Notification.build构造Notification方法介绍:
void setLatestEventInfo(Context context , CharSequencecontentTitle,CharSequence contentText,PendingIntent contentIntent)
功能: 显示在拉伸状态栏中的Notification属性,点击后将发送PendingIntent对象
参数: context 上下文环境
contentTitle 状态栏中的大标题
contentText 状态栏中的小标题
contentIntent 点击后将发送PendingIntent对象
说明:要是在Notification中加入图标,在状态栏和状态条中显示图标一定要用这个方法,否则报错!
最后说一下NotificationManager类的常用方法:
通过获取系统服务来获取该对象:
NotificationManager mNotificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE) ;
NotificationManager常用方法介绍:
public void cancelAll() 移除所有通知 (只是针对当前Context下的Notification)
public void cancel(int id) 移除标记为id的通知 (只是针对当前Context下的所有Notification)
public voidnotify(String tag ,int id, Notification notification) 将通知加入状态栏, 标签为tag,标记为id
public void notify(int id, Notification notification) 将通知加入状态栏,,标记为id
http://www.cnblogs.com/babynight/archive/2012/08/22/Notification_NotificationManage_PendingIntent.html
Notification使用以及PendingIntent.getActivity() (转)的更多相关文章
- Notification NotificationManager RemoteViews PendingIntent
Notification和NotificationManager :可以用来实现可视化的消息通知等,比如:下载进度显示,消息显示,广播的内容等 RemoteViews :用于创建自定义的Notific ...
- Android消息通知(notification)和PendingIntent传值
通知栏的自定义布局:转:http://blog.csdn.net/vipzjyno1/article/details/25248021 拓展 实现自定义的通知栏效果: 这里要用到RemoteViews ...
- 0703-APP-Notification-statue-bar
1.展示显示textTicker和仅仅有icon的两种情况:当參数showTicker为true时显示否则不显示 // In this sample, we'll use the same text ...
- notification.setLatestEventInfo(context, title, message, pendingIntent); undefined
notification.setLatestEventInfo(context, title, message, pendingIntent); 在target为23时删除了该方法,我们应该使用 ...
- PendingIntent、Notification常用方法
PendingIntent PendingIntent它的直译是:待处理意图,这样翻译,大家就猜出它的作用是什么了,用于处理一些定义但是不立即使用的意图,最常见的就是用户点击通知,然后跳转指定的页面: ...
- [问题解决]同时显示多个Notification时PendingIntent的Intent被覆盖?
情况是这样的,使用NotificationManager触发多个Notification: private Notification genreNotification(Context context ...
- Notification(二)——PendingIntent的flag导致数据同样的问题
MainActivity例如以下: package cc.cu; import android.os.Bundle; import android.view.View; import android. ...
- AppWidget应用(二)---PendingIntent 之 getActivity
通过AppWidget应用(一)的介绍,我们已经知道如何创建一个在主界面上显示一个appWidget窗口,但这并不是我们的目的,我们需要做到程序与用户之间进行交互:下面来介绍下如何通过appWidge ...
- Android Notification 详解(一)——基本操作
Android Notification 详解(一)--基本操作 版权声明:本文为博主原创文章,未经博主允许不得转载. 微博:厉圣杰 源码:AndroidDemo/Notification 文中如有纰 ...
随机推荐
- 项目架构mvc+webapi
mvc+webapi 项目架构 首先项目是mvc5+webapi2.0+orm-dapper+ef codefirst. 1.项目框架层次结构: 这个mvc项目根据不同的业务和功能进行不同的区域划分, ...
- Google Map API V2密钥申请
之前用的都是v1,用的是MapView,好吧,仅仅能认命了.废话不再多说,開始android 的Google Maps Android API v2吧 之前參考了http://www.cnblogs. ...
- 用数据说话,外贸产品选择(中篇)-google趋势分析法
在上篇文章<用数据说话,贸B2C产品选择(上篇)-热门搜索法>中我们能搜索出来几种产品了,那我们就拿上次搜索出来的热门产品来做一个趋势分析.我们经过几个站点挑出了几种热卖产品Wedding ...
- 设计模式(一)工厂模式Factory(创建类型)
设计模式一 工厂模式Factory 在面向对象编程中, 最通常的方法是一个new操作符产生一个对象实例,new操作符就是用来构造对象实例的.可是在一些情况下, new操作符直接生成对象会带来一些问题. ...
- PocketSphinx语音识别系统语言模型的训练和声学模型的改进
PocketSphinx语音识别系统语言模型的训练和声学模型的改进 zouxy09@qq.com http://blog.csdn.net/zouxy09 关于语音识别的基础知识和sphinx的知识, ...
- Quartz CronTrigger应用
CronTrigger配置格式: 格式: [第二] [支] [小时] [日本] [月] [周] [年] 序号 说明 是否必填 同意填写的值 同意的通配符 1 秒 是 0-59 , ...
- Android多线程的研究(8)——Java5于Futrue获取线程返回结果
我们先来看看ExecutorService操作的方法: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvZGF3YW5nYW5iYW4=/font/5a6L5 ...
- 十一:Java之GUI图形Awt和Swing
一. AWT和 Swing AWT 和 Swing 是 Java 设计 GUI用户界面的基础.与 AWT 的重量级组件不同,Swing 中大部分是轻量级组件.正是这个原因,Swing 差点儿无所不能, ...
- MongoDB时间处理问题
MongoDB保存到数据库的时候,默认为UTC时间,在数据库保存时,会和当前时间有个间隔,差距为8小时. 在读取的时候,需要再次转换回来,比较麻烦. 其实,Mongo本身就已经提供了相应的处理方法,即 ...
- 为应用程序池 'DefaultAppPool' 提供服务的进程关闭时间超过了限制
服务器经常产生“应用程序池 'DefaultAppPool' 提供服务的进程关闭时间超过了限制.进程 ID 是 '2068'.”的错误,导致iis处于假死状态,经了解是IIS应用程序池的设置问题.解决 ...