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. <s:iterator> 序号

    <s:iterator />的序号,解决这个问题有两种办法. 方法一:通过set标签实现: <s:set name="a" value=1/> <s: ...

  2. hdu 5313 Bipartite Graph(dfs染色 或者 并查集)

    Problem Description Soda has a bipartite graph with n vertices and m undirected edges. Now he wants ...

  3. testng 提供参数

    获取页面元素属性,并把属性作为参数传递个测试方法,两桶不同的写法 1. @DataProvider public Iterator<Object[]> dp() { mySleep(500 ...

  4. PHP设计模式笔记五:策略模式 -- Rango韩老师 http://www.imooc.com/learn/236

    策略模式 1.概述:策略模式,将一组特定的行为和算法封装成类,以适应某些特定的上下文环境,这种模式称为策略模式 例如:一个电商网站系统,针对男性女性用户要各自跳转到不同的商品类目,并且所有广告位展示不 ...

  5. Redis安全

    安全 执行在可信环境 Redis的安全设计是在"Redis执行在可信环境"这个前提下做出的.在生产环境执行时不能同意外界直接连接到Redisserver上.而应该通过应用程序进行中 ...

  6. [Cycle.js] Fine-grained control over the DOM Source

    Currently in our main() function,  we get click$ event. function main(sources) { const click$ = sour ...

  7. DIV 与 Table 嵌套

    当然可以了.对于DIV定义表示一块可显示 HTML 的区域. Specifies a container that renders HTML. 注释此元素在 Internet Explorer 3.0 ...

  8. hdu 1240 Asteroids! (三维bfs)

    Asteroids! Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total ...

  9. [Android算法] bitmap 将图片压缩到指定的大小

    Bitmap压缩到指定大小: private void imageZoom() {//图片允许最大空间 单位:KBdouble maxSize =400.00;//将bitmap放至数组中,意在bit ...

  10. 理清fineuploader无刷新上传的一些事

    1.fineuploader是一款不依赖与jquery的异步无刷新上传组件,fineuploader采用ajax方式实现对文件上传,返回值都是以json的格式,对后台服务器操作和前端dom对象一些操作 ...