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. iOS 使用pods报错问题 pod --version

    错误信息如下 find_spec_for_exe': can't find gem cocoapods (>= 0.a) (Gem::GemNotFoundException) from /Us ...

  2. MapReduce新版客户端API源码分析

    使用MapReduce新版客户端API提交MapReduce Job需要使用 org.apache.hadoop.mapreduce.Job 类.JavaDoc给出以下使用范例. // Create ...

  3. 关于Go语言共享内存操作的小实例

    <strong style="margin: 0px; padding: 0px; border: 0px; font-size: 15px; font-weight: bold; c ...

  4. android 开发随笔 - 开发环境搭建

    1.1 问题: 'tools.jar' seems to be not in Android Studio classpath. Please ensure JAVA_HOME points to J ...

  5. Android应用开发提高系列(4)——Android动态加载(上)——加载未安装APK中的类

    前言 近期做换肤功能,由于换肤程度较高,受限于平台本身,实现起来较复杂,暂时搁置了该功能,但也积累了一些经验,将分两篇文章来写这部分的内容,欢迎交流! 关键字:Android动态加载 声明 欢迎转载, ...

  6. VCS仿真生成vpd文件(verilog)

    VCS仿真生成vpd文件(verilog) 一.环境与文件 Linux平台  csh环境 VCS 64bit 代码文件请参考<一个简单的Verilog计数器模型> 二.开始仿真 1.com ...

  7. C# 超级简单的Telnet (TcpClient)客户端

    基于Sockets 没什么好说的,代码说明了所有 using System; using System.Collections.Generic; using System.Linq; using Sy ...

  8. 3D图片采集与展示(SurfaceView 自适应 Camera, 录制视频, 抽取帧)

    最近在做一个3D图片采集与展示. 主要功能为:自定义Camera(google 已经摈弃了Camera, 推荐使用Camera2,后续篇幅,我将会用Camera2取代Camera),围绕一个物体360 ...

  9. Jsp request

    <%@ page language="java" import="java.util.*" pageEncoding="GB18030" ...

  10. Invalid content was found starting with element 'taglib'”

    今天在使用struts-menu制作菜单,在web.xml中写入 <taglib>      <taglib-uri>/WEB-INF/struts-menu.tld</ ...