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. Android Studio:You need to use a Theme.AppCompat theme (or descendant) with this activity. AlertDialog

    学习<第一行代码>的时候遇到的问题. Process: com.example.sevenun.littledemo, PID: 2085 java.lang.RuntimeExcepti ...

  2. hdu 5402 Travelling Salesman Problem(大模拟)

    Problem Description Teacher Mai ,) to the bottom right corner (n,m). He can choose one direction and ...

  3. mac复制粘贴剪切

    win下复制粘贴剪切: Ctrl+C,Ctrl+V,Ctrl+X; mac下lion之后已经有了一直让win用户吐槽的剪切功能: 复制粘贴剪切:Command+C,Command+V,Command+ ...

  4. iPhone应用提交流程:如何将App程序发布到App Store?

    对于刚加入iOS应用开发行列的开发者来说,终于经过艰苦的Coding后完成了第一个应用后最重要的历史时刻就是将应用程序提交到iTunes App Store.Xcode 4.2开发工具已经把App提交 ...

  5. C++11中正則表達式測试

    VC++2010已经支持regex了, 能够用来编译下述代码. #include <string> #include <regex> #include <iostream ...

  6. jQuery制作焦点图(轮播图)

    焦点图(轮播图) 案例 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http:/ ...

  7. Linux基础知识(一)

    1. Unix 和 Linux之间有什么关系? Linux可以说是Unix衍生过来的,它借鉴了很多Unix的设计理念,应该说,它们类似于父子关系,Linux又被称为类Unix系统. 2. BSD是什么 ...

  8. css3 content画出各种图形

    原链接:http://www.phpjz.cn/web/201311/1700.html 之前看到一些网站用户content这个词,觉得很奇怪,原来是css3新增的一个样式,发现还挺好用的,特别是用移 ...

  9. css布局详解(二)——标准流布局(Nomal flow)

    css标准流布局(Nomal flow) 一.正常流 这是指西方语言中文本从左向右,从上向下显示,这也是我们熟悉的传统的HTML文档中的文本布局.注意,在非西方的语言中,流方向可能不同.大多数元素都在 ...

  10. linux syslog详解

    linux syslog详解 分三部分 一.syslog协议介绍 二.syslog函数 三.linux syslog配置   一.syslog协议介绍 1.介绍 在Unix类操作系统上,syslog广 ...