13讲 Android之消息提示Notification

、Notification

Notification可以理解为通知的意思一般用来显示广播信息,通知可以显示到系统的上方的状态栏(status bar)中,通知内部的显示分为两个部分:

1.notification area(通知状态栏);     2. notification drawer
(通知列表页面)。

    

( 图一 )                                                                          
( 图二 )

当应用程序向android系统发出一个notification时,通知首先以小图标的方式出现在notification area。用户可以在下拉栏,打开notification drawer,显示notification的详细情况。

提示:notification area和notification drawer都是由android系统来管理和维护的,因此用户可以随时进行查看。

优点:某些信息不需要用户马上处理,可以利用通知,即延迟消息,比如软件的更新,短信,新闻之类的。

1.内容标题 
2.大图标  3.内容  4.内容附加信息 
5.小图标  6.时间

ToastNotification的区别:

A、Toast是一种及时的消息提醒,而Notification是一种延迟的消息提醒。

B、Toast其实相当于一个组件(Widget),有些类似于没有按钮的对话框。而Notification是显示在屏幕上方状态栏中的信息。

C、 Notification需要用NotificationManager来管理,而Toast只需要简单地创建Toast对象即可。

、如何创建notification

1>实例化一个NotificationManager对象;如:manager

调用Context.getSystemService(NOTIFICATION_SERVICE)方法即可返回NotificationManager实例。

2>实例化一个NotificationCompat.Builder对象;如builder

3>调用builder的相关方法对notification进行上面提到的各种设置

4>调用builder.build()方法此方法返回一个notification对象。

5>调用manager的notify方法发送通知

NotificationManager有两个方法:notify()发出通知 cancel( )取消通知

//1.实例化一个NotificationManager对象;如:manager。通过getSystemService方法获得

NotificationManager manager=(NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);

//2.实例化一个NotificationCompat.Builder对象;如builder

NotificationCompat.Builder builder = newNotificationCompat.Builder(this)

//3.调用builder的相关方法对notification进行上面提到的各种设置;

.setSmallIcon(R.drawable.ic_launcher)               //小图标

.setContentTitle("My notification")                          //内容标题

.setContentText("Hello world!")                              //内容

.setTicker("来信息啦。。。");

//4.调用builder.build()方法此方法返回一个notification对象;

Notification notification=builder.build();

//5.调用manager的notify方法发送通知。

manager.notify(1, notification);

// manager.notify(id, notification); 发送一个通知

// manager.cancel(id); 取消通知

// 1. 通过getSystemService()方法得到NotificationManager对象:

String ns = Context.NOTIFICATION_SERVICE;

NotificationManager mNotificationManager =(NotificationManager) getSystemService(ns);

// 2. 初始化Notification:

int icon =R.drawable.notification_icon;        // 设置通知的图标

CharSequence tickerText ="Hello";               //通知提示,显示在状态栏中的文字

long when =System.currentTimeMillis();     // 设置来通知时的时间

Notification notification = newNotification(icon, tickerText, when);

// 3. 定义通知的信息和PendingIntent:

Context context = getApplicationContext();

CharSequence contentTitle = "Mynotification";

CharSequence contentText = "HelloWorld!";

Intent notificationIntent = newIntent(this, MyClass.class);        // 单击通知后会跳转到NotificationResult类

// 获取PendingIntent,点击时发送该Intent

PendingIntent contentIntent =PendingIntent.getActivity(this, 0, notificationIntent, 0);

notification.setLatestEventInfo(context,contentTitle, contentText, contentIntent);   //设置通知的标题和内容

//notification.setLatestEventInfo(NotificationActivity.this, "标题","内容", contentIntent);

// 4. 通过NotificationManager对象的notify()方法来执行一个notification的消息:

private static final int HELLO_ID = 1;

mNotificationManager.notify(HELLO_ID,notification);

第13讲- Android之消息提示Notification的更多相关文章

  1. 第12讲- Android之消息提示Toast

    第12讲 Android之消息提示Toast .Toast Toast 是一个 View 视图,在应用程序上浮动显示少量的信息给用户,它永远不会获得焦点,不影响用户的输入等操作,主要用于向用户显示一些 ...

  2. Android实现系统下拉栏的消息提示——Notification

    Android实现系统下拉栏的消息提示--Notification 系统默认样式 默认通知(通用) 效果图 按钮 <Button android:layout_width="match ...

  3. android的消息提示(震动与提示音)

    protected AudioManager audioManager; protected Vibrator vibrator; audioManager = (AudioManager)getSy ...

  4. android笔记-----消息提示

    在/res/values目录下的文件中定义要显示的字符串,主要是考虑到后期可能需要换成英文之类的 <string name="login_checkBlank">用户名 ...

  5. Android开发 ---构建对话框Builder对象,消息提示框、列表对话框、单选提示框、多选提示框、日期/时间对话框、进度条对话框、自定义对话框、投影

    效果图: 1.activity_main.xml 描述: a.定义了一个消息提示框按钮 点击按钮弹出消息 b.定义了一个选择城市的输入框 点击按钮选择城市 c.定义了一个单选提示框按钮 点击按钮选择某 ...

  6. Android三种消息提示

    Android消息提示有三种方式: 1  使用Toast显示消息提示框 Toast类用于在屏幕中显示一个提示信息框,该消息提示框没有任何控制按钮,并且不会获得焦点,经过一定时间后自动消失.通常用于显示 ...

  7. React Native之通知栏消息提示(android)

    React Native之通知栏消息提示(android) 一,需求分析与概述 1.1,推送作为手机应用的基本功能,是手机应用的重要部分,如果自己实现一套推送系统费时费力,所以大部分的应用都会选择使用 ...

  8. 【Android代码片段之六】Toast工具类(实现带图片的Toast消息提示)

    转载请注明出处,原文网址:http://blog.csdn.net/m_changgong/article/details/6841266  作者:张燕广 实现的Toast工具类ToastUtil封装 ...

  9. Android第三方开源对话消息提示框:SweetAlertDialog(sweet-alert-dialog)

    Android第三方开源对话消息提示框:SweetAlertDialog(sweet-alert-dialog) Android第三方开源对话消息提示框:SweetAlertDialog(sweet- ...

随机推荐

  1. 第19讲- UI组件之_Button、checkbox、radio

    第19讲 UI组件之_Button.checkbox.radio 四.按钮Button Button继承自TextView,间接继承自View.当用户对按钮进行操作的时候,触发相应事件,如点击,触摸. ...

  2. mysql插入数据时,中文乱码

    MySQL 插入数据时,中文乱码问题的解决(转) 当向 MySQL 数据库插入一条带有中文的数据形如 insert into employee values(null,'张三','female','1 ...

  3. python数据类型—列表(增改删查,统计,取值,排序)

    列表是最常用的数据类型之一,通过列表可以对数据实现方便的存储,修改等操作. 先声明一个空列表: >>> names = [] >>> names [] 可以存多个值 ...

  4. Javascript:一个优雅的时钟

    实现效果: 准备工作: 1# 定时器 相关知识了解 2#javascript Date(日期)对象 3# 准备效果所用图片 实现原理: 1# 获取当前时间: var time=new Date(); ...

  5. [RxJS] Starting a Stream with SwitchMap & switchMapTo

    From an event map to another event we can use switchMap(), switchMap() accept an function which retu ...

  6. Android 编程下的计时器

    在安卓 APP 的手机号注册逻辑中,经常会有将激活码发送到手机的环节,这个环节中绝大多数的应用考虑到网络延迟或服务器压力以及短信服务商的延迟等原因,会给用户提供一个重新获取激活码的按钮.如下图所示: ...

  7. ORA-01652:无法通过128(在表空间TEMP中)扩展temp段

    在Oracle数据库中进行order by or group by.索引的创建和重创建.distinct操作.union & intersect & minus sort-merge ...

  8. 《第一行代码》学习笔记14-UI(3)

    1. (1)所有控件都是直接或间接继承自View,所用的所有布局都是直接或间接继承自ViewGroup的. (2)View是Android中一种最基本的UI组件,可以在屏幕上绘制一块矩形区域,并能响应 ...

  9. 三组I/O复用模型的比较

    概论: select.poll和epoll三组I/O复用系统调用,这3组系统调用都能同时监听多个文件描述符.它们将等待由timeout参数指定的超时时间,直到一个或者多个文件描述符上有事件发生时返回. ...

  10. fiddler2使用文档

    http://www.trinea.cn/android/android-network-sniffer/