Notification使用以及PendingIntent.getActivity() (转)

摘要:
我发现这个函数特别有用,所以我将根据我的理解来讨论它。已添加Notification.Builder,以使生成通知更容易。通知是一种让应用程序在未启动或未在后台运行时警告用户的方法。这是不可见程序组件提醒用户需要注意的事件的最佳方式。



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 PendingIntentgetActivity (Context context, int requestCode, Intent intent, int flags)

Since: API Level 1

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
contextThe Context in which this PendingIntent should start the activity.
requestCodePrivate request code for the sender (currently not used).  现在不使用了
intentIntent of the activity to be launched.
flagsMay 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() (转)》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇Vue中使用v-for制作动态手风琴效果数据结构-树下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

[问题解决]同时显示多个Notification时PendingIntent的Intent被覆盖?

情况是这样的,使用NotificationManager触发多个Notification: Java代码 privateNotificationgenreNotification(Contextcontext,inticon,StringtickerText,Stringtitle,Stringcontent,Intentintent){ Not...

高仿微信新消息提示音功能

近期公司在做一个项目。有一个切换消息提示音的功能,能够切换本应用收到消息的提示音,而不影响系统提示音。我就依照微信的那个样式进行了编程,终于得到想要的效果。 转载请注明出处。谢谢:http://blog.csdn.net/harryweasley/article/details/46408037 怕有些人不知道怎么进入微信的新消息提示音功能,我这里说...

iOS状态栏详解(隐藏)

状态栏的隐藏 状态栏的隐藏主要有两种方法:方法一:通过代码控制 @interface UIApplication(UIApplicationDeprecated) // Setting statusBarHidden does nothing if your application is using the default UIViewControl...

Java服务端极光推送整合Ios、Android

https://blog.csdn.net/Jay_1989/article/details/82771886 关闭 原创 Java服务端极光推送整合Ios、Android 2018-09-19 15:32:53 Jay_1989 阅读数 1820  收藏 更多分类专栏: Java    版权声明:本文为博主原创文章,遵循 CC 4....

Android沉浸式状态栏实现

在安卓开发当中,顶部的状态栏很多时候是和我们自己所设定的安卓背景颜色不相同的,看起来就十分别扭,就如同下图所示,状态栏是深绿色,我们的背景却是一个十分好看的渐变颜色: 在使用沉浸式状态栏之后的界面如下: 如何将顶部的状态栏设置成透明的呢,我们可以在主活动的 onCreate() 方法当中输入以下代码: if(Build.VERSION.SDK_INT&g...

状态通知栏

在android的应用层中,涉及到很多应用框架,例如:Service框架,Activity管理机制,Broadcast机制,对话框框架,标题栏框架,状态栏框架,通知机制,ActionBar框架等等。 下面就来说说经常会使用到通知机制中的通知栏框架(Notificaiton),它适用于交互事件的通知。它是位于顶层可以展开的通知列表。它会时不时的提醒你什么软件...