notification的使用

示例:

 1 NotificationManager nm = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
 2 Notification n = new Notification(R.drawable.chat, "Hello,there!", System.currentTimeMillis());
 3 n.flags = Notification.FLAG_AUTO_CANCEL;
 4 Intent i = new Intent(arg0.getContext(), NotificationShow.class);
 5 i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_NEW_TASK);
 6 //PendingIntent
 7 PendingIntent contentIntent = PendingIntent.getActivity(
 8         arg0.getContext(),
 9         R.string.app_name,
10         i,
11         PendingIntent.FLAG_UPDATE_CURRENT);
12
13 n.setLatestEventInfo(
14         arg0.getContext(),
15         "Hello,there!",
16         "Hello,there,I‘m john.",
17         contentIntent);
18 nm.notify(R.string.app_name, n);

展开代码

下面依次对每一段代码进行分析:

NotificationManager nm = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);

创建 NotificationManager,其中创建的 nm 对象负责“发出”与“取消”  Notification。

Notification n = new Notification(R.drawable.chat, "Hello,there!", System.currentTimeMillis());
n.flags = Notification.FLAG_ONGOING_EVENT; 

创建 Notification ,参数依次为:icon的资源id,在状态栏上展示的滚动信息,时间。其中创建的 n 对象用来描述出现在系统通知栏的信息,之后我们将会看到会在 n 对象上设置点击此条通知发出的Intent。

n.flags = Notification.FLAG_AUTO_CANCEL;  

设置 n.flags 为 Notification.FLAG_AUTO_CANCEL ,该标志表示当用户点击 Clear 之后,能够清除该通知。

Intent i = new Intent(arg0.getContext(), NotificationShow.class);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_NEW_TASK);

  

创建一个Intent,该Intent使得当用户点击该通知后发出这个Intent

请注意,如果要以该Intent启动一个Activity,一定要设置 Intent.FLAG_ACTIVITY_NEW_TASK 标记。

Intent.FLAG_ACTIVITY_CLEAR_TOP :如果在当前Task中,有要启动的Activity,那么把该Acitivity之前的所有Activity都关掉,并把此Activity置前以避免创建Activity的实例

Intent.FLAG_ACTIVITY_NEW_TASK :系统会检查当前所有已创建的Task中是否有该要启动的Activity的Task,若有,则在该Task上创建Activity,若没有则新建具有该Activity属性的Task,并在该新建的Task上创建Activity。更多请参见 “ (转载)Android下Affinities和Task ”

PendingIntent contentIntent = PendingIntent.getActivity(
        arg0.getContext(),
        R.string.app_name,
        i,
        PendingIntent.FLAG_UPDATE_CURRENT);

  

PendingIntent 为Intent的包装,这里是启动Intent的描述,PendingIntent.getActivity 返回的PendingIntent表示,此PendingIntent实例中的Intent是用于启动 Activity 的Intent。PendingIntent.getActivity的参数依次为:Context,发送者的请求码(可以填0),用于系统发送的Intent,标志位。

其中 PendingIntent.FLAG_UPDATE_CURRENT  表示如果该描述的PendingIntent已存在,则改变已存在的PendingIntent的Extra数据为新的PendingIntent的Extra数据。

这里再简要说一下 Intent 与 PendingIntent 的区别:

Intent :意图,即告诉系统我要干什么,然后系统根据这个Intent做对应的事。如startActivity相当于发送消息,而Intent是消息的内容。

PendingIntent :包装Intent,Intent 是我们直接使用 startActivity , startService 或 sendBroadcast 启动某项工作的意图。而某些时候,我们并不能直接调用startActivity , startServide 或 sendBroadcast ,而是当程序或系统达到某一条件才发送Intent。如这里的Notification,当用户点击Notification之后,由系统发出一条Activity 的 Intent 。因此如果我们不用某种方法来告诉系统的话,系统是不知道是使用 startActivity ,startService 还是 sendBroadcast 来启动Intent 的(当然还有其他的“描述”),因此这里便需要PendingIntent。

n.setLatestEventInfo(
        arg0.getContext(),
        "Hello,there!",
        "Hello,there,I‘m john.",
        contentIntent); 

设置显示在通知下拉框中的信息,参数依次为:Context,标题,内容,PendingIntent。

nm.notify(R.string.app_name, n);

启动Notification,参数依次为:在你的程序中标识Notification的id值(用来区分同一程序中的不同Notifycation,如果程序中只有一个Notification那么这里随便你填什么都可以,不过类型必须要为int),要通知的Notification。

如何使自己的Notification像Android QQ一样能出现在 “正在运行的”栏目下面

其实很简单,只需设置Notification.flags = Notification.FLAG_ONGOING_EVENT;便可以了。

如何改变 Notification 在“正在运行的”栏目下面的布局

创建 RemoteViews 并赋给 Notification.contentView ,再把 PendingIntent 赋给 Notification.contentIntent 便可以了,如:

PendingIntent contentIntent = PendingIntent.getActivity(
    arg0.getContext(),
    R.string.app_name,
    i,
    PendingIntent.FLAG_UPDATE_CURRENT);

RemoteViews rv = new RemoteViews(Main.this.getPackageName(), R.layout.notification_view);
rv.setImageViewResource(R.id.image, R.drawable.chat);
rv.setTextViewText(R.id.text, "Hello,there,I‘m john.");
n.contentView = rv;
n.contentIntent = contentIntent;

nm.notify(R.string.app_name, n);

注意,如果使用了contentView,那么便不要使用Notification.setLatestEventInfo。如果setLatestEventInfo在赋给 Notification.contentView 的代码之后,那么contentView的效果将被覆盖,显示的便是 setLatestEventInfo 的效果;如果 setLatestEventInfo 在 Notification.contentView 的代码之前,那么显示的便是 Notification.contentView 的效果,也就是说不管你想要setLatestEventInfo 或 contentView 的自定义效果,请保证始终只有一句设置代码,因为在最后一句绑定的时候,之前的设置contentView或setLatestEventInfo的代码都是完全没有必要的。

时间: 2024-08-05 13:26:35

notification的使用的相关文章

通知栏Notification在不同手机上显示的问题总结

可以参照http://blog.csdn.net/vipzjyno1/article/details/25248021,这里面关于通知的写的不错,也很全面,我的这篇主要是记录自己在适配上遇到的问题. 通知的统一的创建方式: NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(mContext); 而通知的管理则是使用NotificationManager是用来管理通知的,使用如下:先初始化用到的系统服务,然后调

Apple Notification Center Service--ANCS【转】

Apple Notification Center Service 转自:http://studentdeng.github.io/blog/2014/03/22/ancs/ MAR 22ND, 2014 | COMMENTS 名词解释与约定 名词解释 Apple Notification Center Service 简称 ANCS. ANCS 服务(iOS设备,如iPhone,iPad等)的publisher 称为 Notification Provider. 任意的ANCS服务的clien

Notification Centers 通知中心

Notification Centers 通知中心 A notification center manages the sending and receiving of notifications. It notifies all observers of notifications meeting specific criteria. The notification information is encapsulated in NSNotification objects. Client o

Mobile Push Notification

In one embodiment, a method includes sending to a mobile client computing device a first notification through a real-time push service, the first notification including content and being associated with a stateful object; the method also includes, in

Java 线程第三版 第四章 Thread Notification 读书笔记

一.等待与通知 public final void wait() throws InterruptedException 等待条件的发生. public final void wait(long timeout) throws InterruptedException 等待条件的发生.如果通知没有在timeout指定的时间内发生,它还是会返回. public final void wait(long timeout, int nanos) throws InterruptedException

Android-自定义Notification

Android-自定义Notification 2014年4月26日 消息栏的消息,想必各位Android发烧友很清楚知道是什么,比如我们下载了一个应用,它可能会定时推送些消息到我们的手机中,比如微信消息送达的时候,可能会在通知栏显示.本博文介绍如何自定义一个Notification,很简单的东西,这里小巫只是把它整理出来,奉上demo. 先来看看效果图: 附上源码:http://download.csdn.net/detail/wwj_748/7259815 有兴趣的朋友可以加本人创建的群,里

通知(Notification)

1.通知的基本用法 //创建 NotificationManager 实例 NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); Notification notification = new NotificationCompat.Builder(this) .setContentTitle("This is title") //标题 .setContent

Notification的基本用法以及使用RemoteView实现自定义布局

Notification的作用 Notification是一种全局效果的通知,在系统的通知栏中显示.既然作为通知,其基本作用有: 显示接收到短消息.即时信息等 显示客户端的推送(广告.优惠.新闻等) 显示正在进行的事物(后台运行的程序,如音乐播放进度.下载进度) Notification的基本操作: Notification的基本操作主要有创建.更新和取消三种.一个Notification的必要属性有三项,如果不设置的话在运行时会抛出异常: 小图标,通过setSmallIcon方法设置 标题,通

Android中使用Notification实现宽通知栏(Notification示例二)

Notification是在你的应用常规界面之外展示的消息.当app让系统发送一个消息的时候,消息首先以图表的形式显示在通知栏.要查看消息的详情需要进入通知抽屉(notificationdrawer)中查看.通知栏和通知抽屉 (notificationdrawer)都是系统层面控制的,你可以随时查看,不限制于app. Notification 的设计: 作为android UI中很重要的组成部分,notification拥有专属于自己的设计准则. Notification的界面元素在通知抽屉中的

安卓学习之通知(Notification)

安卓中创建通知需要借助NotificationManager来对通知进行管理.可以调用Context的getsSystemService()方法来获得. getsSystemService()方法接收一个参数,这个参数是字符串,用于指定哪一个服务.Context.NOTIFICATION_SERVICE 就是指定通知服务. 这个方法返回一个Object对象,所欲需要进行强制转换. NotificationManager manager = (NotificationManager) getSys