第13讲- Android之消息提示Notification
第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.时间
Toast 与 Notification的区别:
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的更多相关文章
- 第12讲- Android之消息提示Toast
第12讲 Android之消息提示Toast .Toast Toast 是一个 View 视图,在应用程序上浮动显示少量的信息给用户,它永远不会获得焦点,不影响用户的输入等操作,主要用于向用户显示一些 ...
- Android实现系统下拉栏的消息提示——Notification
Android实现系统下拉栏的消息提示--Notification 系统默认样式 默认通知(通用) 效果图 按钮 <Button android:layout_width="match ...
- android的消息提示(震动与提示音)
protected AudioManager audioManager; protected Vibrator vibrator; audioManager = (AudioManager)getSy ...
- android笔记-----消息提示
在/res/values目录下的文件中定义要显示的字符串,主要是考虑到后期可能需要换成英文之类的 <string name="login_checkBlank">用户名 ...
- Android开发 ---构建对话框Builder对象,消息提示框、列表对话框、单选提示框、多选提示框、日期/时间对话框、进度条对话框、自定义对话框、投影
效果图: 1.activity_main.xml 描述: a.定义了一个消息提示框按钮 点击按钮弹出消息 b.定义了一个选择城市的输入框 点击按钮选择城市 c.定义了一个单选提示框按钮 点击按钮选择某 ...
- Android三种消息提示
Android消息提示有三种方式: 1 使用Toast显示消息提示框 Toast类用于在屏幕中显示一个提示信息框,该消息提示框没有任何控制按钮,并且不会获得焦点,经过一定时间后自动消失.通常用于显示 ...
- React Native之通知栏消息提示(android)
React Native之通知栏消息提示(android) 一,需求分析与概述 1.1,推送作为手机应用的基本功能,是手机应用的重要部分,如果自己实现一套推送系统费时费力,所以大部分的应用都会选择使用 ...
- 【Android代码片段之六】Toast工具类(实现带图片的Toast消息提示)
转载请注明出处,原文网址:http://blog.csdn.net/m_changgong/article/details/6841266 作者:张燕广 实现的Toast工具类ToastUtil封装 ...
- Android第三方开源对话消息提示框:SweetAlertDialog(sweet-alert-dialog)
Android第三方开源对话消息提示框:SweetAlertDialog(sweet-alert-dialog) Android第三方开源对话消息提示框:SweetAlertDialog(sweet- ...
随机推荐
- <php>PDO用法一
<?php //造PDO对象 $pdo = new PDO("mysql:dbname=mydb;host=localhost","root"," ...
- Java 内存区域和GC机制-java概念理解
推荐几篇关于java内存介绍的文章 Java 内存区域和GC机制 http://www.cnblogs.com/hnrainll/archive/2013/11/06/3410042.html ...
- Struts分页
1.分页的bean类PaginationSupport.java 2.写好后直接在action里面调用,计算当前页显示的数据 3.写一个公用的jsp页面,直接在需要分页的页面include就可以了 ...
- html打开个人QQ聊天页面
打开qq聊天页面(有权限需要添加好友) <a href="tencent://message/?uin=1578929883&Site=&Menu=yes" ...
- Java MongoDB 资料集合
一.Mongodb介绍及对比 1.NoSQL介绍及MongoDB入门 http://renial.iteye.com/blog/684829 2.mongoDB 介绍(特点.优点.原理) http:/ ...
- sublime怎么实现函数之间的跳转
1.安装ctags应用程序. 到CTags的官方站点下载最新版本号,将解压后的ctags.exe放到系统环境变量的搜索路径中.通常是C:\windows\system32. 假设你想放到其它目录中,记 ...
- 《31天成为IT服务达人》--机遇篇(二)
1 第二章 机遇就是选择大于努力 年假设你一咬牙(或者在晚点)买了房,十年的巨幅增值,比你如今干哪行都赚得快,可是往往有选择就有痛苦,这样的痛苦来至于对未知的恐惧和现实须要一定的付出.作为 ...
- Android开源代码解读-基于SackOfViewAdapter类实现类似状态通知栏的布局
一般来说,ListView的列表项都会采用相同的布局,只是填充的内容不同而已,这种情况下,Android提供了convertView帮我们缓存列表项,达到循环利用的目的,开发者也会使用ViewHold ...
- Controller返回值类型ActionResult
在mvc中所有的controller类都必须使用"Controller"后缀来命名 并且对Action也有一定的要求: 必须是一个public方法 必须是实例方法 没有标志NonA ...
- (转).net开发者对android开发一周的学习体会
春节期间,相对比较闲,上班时也没什么事情做.利用这一周的时间,简单的学习了一下移动方面的开发.主要是针对android,其实我对IOS更感兴趣 (因为我用iphone),苦于暂时没有苹果电脑,只能把它 ...