1. 通知的使用场合

  当某个应用程序希望向用户发出一些提示信息,而该应用程序又不在前台运行时,就可以借助通知来实现。发出一条通知后,手机最上方的状态栏中会显示一个通知的图标,下拉状态栏后可以看到通知的详细内容。
2. 通知的创建步骤
  (1)获取NotificationManager实例,可以通过调用Conten的getSystenService()方法得到,getSystemService()方法接收一个字符串参数用于确定获取系统的哪个服务, 这里我们传入Context.NOTIFICATION_SERVICE 即可。获取NotificationManager实例如下:

  NotificationManager manager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);

  (2)创建Notification对象,该对象用于存储通知的各种所需信息,我们可以使用它的有参构造函数来创建。构造函数有三个参数,第一个参数指定通知图标,第二个参数用于指定通知的ticker 内容,当通知刚被创建的时候,它会在系统的状态栏一闪而过,属于一种瞬时的提示信息。第三个参数用于指定通知被创建的时间,以毫秒为单位,当下拉系统状态栏时,这里指定的时间会显示在相应的通知上。创建一个Notification 对象可以写成:

  Notification notification = new Notification(R.drawable.ic_launcher,"This is a ticker text",System.currentTimeMillis());

  (3)调用Notification的setLatestEventIfo()方法对通知的布局进行设定,这个方法接收四个参数,第一个参数是Context。第二个参数用于指定通知的标题内容,下拉系统状态栏就可以看到这部分内容。第三个参数用于指定通知的正文内容,同样下拉系统状态栏就可以看到这部分内容。第四个参数用于指定实现通知点击事件的PendingIntent对象,如果暂时用不到可以先传入null。因此,对通知的布局进行设定就可以写成:

  notification.setLatestEventInfo(context, "This is content title", "This iscontent text", null);
  (4)调用NotificationManager的notify()方法显示通知。notify()方法接收两个参数,第一个参数是id,要保证为每个通知所指定的id 都是不同的。第二个参数则是Notification 对象,这里直接将我们刚刚创建好的Notification 对象传入即可。显示一个通知就可以写成:

  manager.notify(1, notification);
3.代码示例

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" > <Button
android:id="@+id/send_notice"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Send notice"
/> </LinearLayout>

布局

public class MainActivity extends Activity implements OnClickListener {

    private Button sendNotice;

    @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sendNotice = (Button) findViewById(R.id.send_notice);
sendNotice.setOnClickListener(this);
} @Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.send_notice:
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification notification = new Notification(
R.drawable.ic_launcher, "This is a ticker text", System.currentTimeMillis()); notification.setLatestEventInfo(this, "This is content title",
"This is content text", null);
manager.notify(1, notification); default:
break;
}
}
}

 

 

												

安卓中通知(Notification)的基本使用方法的更多相关文章

  1. 【Android】安卓中常用的图片加载方法

    一.通过相机选图片: 布局文件: <?xml version="1.0" encoding="utf-8"?> <LinearLayout x ...

  2. 通知 Notification 详解

    效果 通知栏-刚收到通知时 通知栏-收到通知几秒后 标准视图 大视图-下滑前是标准视图 大视图-下滑后显示大视图 自定义通知 讲解 Notification,俗称通知,是一种具有全局效果的通知,它展示 ...

  3. 适配 通知 Notification 通知渠道 前台服务 MD

    Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...

  4. 在 Xamarin.Android 中使用 Notification.Builder 构建通知

    0 背景 在 Android 4.0 以后,系统支持一种更先进的 Notification.Builder 类来发送通知.但 Xamarin 文档含糊其辞,多方搜索无果,遂决定自己摸索. 之前的代码: ...

  5. 安卓状态栏通知Status Bar Notification

    安卓系统通知用户三种方式: 1.Toast Notification 2.Dialog Notification 3.Status Bar Notification Status Bar Notifi ...

  6. vue 使用element-ui中的Notification自定义按钮并实现关闭功能以及如何处理多个通知

    使用element-ui中的Notification,只有一个message属性是有很大的操作空间,其余的都是写死的,无法进行扩展,达不到想要的效果.所以只能在message上下功夫. 在elemen ...

  7. 转-安卓中实现两端对齐,中间fill_parent的方法

    安卓中实现两端对齐,中间fill_parent的方法 Java代码:   <?xml version="1.0″ encoding="utf-8″?> <Line ...

  8. 安卓中onBackPressed ()方法的使用

    一.onBackPressed()方法的解释 这个方法放在 void android.app.Activity.onBackPressed() 在安卓API中它是这样解释的: public void ...

  9. Android中使用Notification实现进度通知栏(Notification示例三)

    我们在使用APP的过程中,软件会偶尔提示我们进行版本更新,我们点击确认更新后,会在通知栏显示下载更新进度(已知长度的进度条)以及安装情况(不确定进度条),这就是我们今天要实现的功能.实现效果如下: 在 ...

随机推荐

  1. Python3的tcp socket接收不定长数据包接收到的数据不全。

    Python Socket API参考出处:http://blog.csdn.net/xiangpingli/article/details/47706707 使用socket.recv(pack_l ...

  2. 用IFrame作预览pdf,图片

    <iframe id="my_img" src="@ViewBag.path" width="100%" frameborder=&q ...

  3. PL/SQL developer(绿色版)安装及配置

    1.PL/SQL Developer下载地址:百度网盘: 2.tsname.ora配置: orcl = (DESCRIPTION = (ADDRESS_LIST = (ADDRESS )) ) (CO ...

  4. SQL-字符串连接聚合函数

    原文:http://blog.csdn.net/java85140031/article/details/6820699 问题: userId  role_name         role_id 1 ...

  5. Android开发之经常使用的时间格式

    /**   * 获取如今时间   *    * @return 返回时间类型 yyyy-MM-dd HH:mm:ss   */ public static Date getNowDate() {   ...

  6. 几个shell程序设计小知识(shell常识部分)

    [转自]http://blog.chinaunix.net/uid-168249-id-2860686.html 引用:一.用户登陆进入系统后的系统环境变量:  $HOME 使用者自己的目录  $PA ...

  7. failed to push some refs to 'git@github.com:*/learngit.git'

    https://jingyan.baidu.com/article/f3e34a12a25bc8f5ea65354a.html 出现错误的主要原因是github中的README.md文件不在本地代码目 ...

  8. win快捷键技巧

    只按Win键,这个所有人都知道,打开和关闭开始菜单. Win+E:打开我的电脑 Win+F:搜索文件 Win+D:显示桌面 Win+M:最小化所有窗口 Win + Pause:显示系统属性对话框 Wi ...

  9. centos上编译bitcoin

    需要预先安装的东西 autoconf automake labtool openssl-devel boost-devel libevent

  10. Extjs学习笔记--(六,选择器)

    文档对象dom是javascript与页面元素的桥梁 选择器的作用就是通过元素的标签名,属性名,css属性名对页面进行快速,准确的定位及选择 Extjs的选择器:Ext.DomQuery Ext.qu ...