一、通知栏的内容

  1、图标

  2、标题

  3、内容

  4、时间

  5、点击后的相应

二、如何实现通知栏

  1、获取NotificationManager。

  2、显示通知栏:notify(id,notification);

  3、取消通知栏:cancle(id);

  4、构造Notification并设置显示内容;

  5、通知栏通知可以设置声音提示,指示灯,以及震动效果。

三、示例代码:发送通知和取消通知

<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"
tools:context=".MainActivity"> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="通知栏的使用" /> <Button
android:id="@+id/btn_send"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="发送通知"/> <Button
android:id="@+id/btn_cancel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="取消通知"/>
</LinearLayout>

添加权限:

    <!--指示灯的权限-->
<uses-permission android:name="android.permission.FLASHLIGHT" />
<!--震动的权限-->
<uses-permission android:name="android.permission.VIBRATE" />

后台代码:

package com.example.zhengcheng.myapplication;

import android.app.Activity;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.app.Notification; public class MainActivity extends Activity { Button btn_send; //发送按钮
Button btn_cancel; //取消按钮
NotificationManager manager; //通知控制类
int notificationID; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); //通过系统服务来创建NotificationManager
manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); btn_send = (Button) findViewById(R.id.btn_send);
btn_send.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
sendNotication();
}
}); btn_cancel = (Button) findViewById(R.id.btn_cancel);
btn_cancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
manager.cancel(notificationID);
}
}); } /**
* 发送通知信息
*/
private void sendNotication() {
Intent intent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0); Notification.Builder builder = new Notification.Builder(this);
builder.setSmallIcon(R.mipmap.ic_launcher); //设置图标
builder.setTicker("手机状态栏提示"); //手机状态栏提示
builder.setWhen(System.currentTimeMillis()); //设置时间
builder.setContentTitle("通知栏标题"); //设置标题栏
builder.setContentText("我来自NotificationDemo"); //设置通知栏内容
builder.setContentIntent(pendingIntent); //设置点击通知栏时弹出哪一个Activity // builder.setDefaults(Notification.DEFAULT_SOUND); //设置提示声音
// builder.setDefaults(Notification.DEFAULT_LIGHTS); //设置指示灯
// builder.setDefaults(Notification.DEFAULT_VIBRATE); //设置震动 builder.setDefaults(Notification.DEFAULT_ALL); //设置上面所有的效果
Notification notification = builder.build(); //Android4.1以上的版本,
// Notification notification = builder.getNotification(); //如果4.1以下的版本使用 builder.getNotification();
manager.notify(notificationID, notification); } }

Android学习(二十)Notification通知栏的更多相关文章

  1. 十一、Android学习第十天——项目开始(转)

    (转自:http://wenku.baidu.com/view/af39b3164431b90d6c85c72f.html) 十一.Android学习第十天——项目开始 Android知识点的学习告一 ...

  2. Android进阶(二十)AndroidAPP开发问题汇总(四)

    · Android进阶(二十)AndroidAPP开发问题汇总(四) android:layout_width和android:width的区别 基中的android:layout_width和and ...

  3. Android进阶(二十八)上下文菜单ContextMenu使用案例

    上下文菜单ContextMenu使用案例 前言 回顾之前的应用程序,发现之前创建的选项菜单无法显示了.按照正常逻辑来说,左图中在"商品信息"一栏中应该存在选项菜单,用户可进行分享等 ...

  4. Java开发学习(二十二)----Spring事务属性、事务传播行为

    一.事务配置 上面这些属性都可以在@Transactional注解的参数上进行设置. readOnly:true只读事务,false读写事务,增删改要设为false,查询设为true. timeout ...

  5. Java开发学习(二十四)----SpringMVC设置请求映射路径

    一.环境准备 创建一个Web的Maven项目 参考Java开发学习(二十三)----SpringMVC入门案例.工作流程解析及设置bean加载控制中环境准备 pom.xml添加Spring依赖 < ...

  6. Java开发学习(二十五)----使用PostMan完成不同类型参数传递

    一.请求参数 请求路径设置好后,只要确保页面发送请求地址和后台Controller类中配置的路径一致,就可以接收到前端的请求,接收到请求后,如何接收页面传递的参数? 关于请求参数的传递与接收是和请求方 ...

  7. Java开发学习(二十六)----SpringMVC返回响应结果

    SpringMVC接收到请求和数据后,进行了一些处理,当然这个处理可以是转发给Service,Service层再调用Dao层完成的,不管怎样,处理完以后,都需要将结果告知给用户. 比如:根据用户ID查 ...

  8. Java开发学习(二十八)----拦截器(Interceptor)详细解析

    一.拦截器概念 讲解拦截器的概念之前,我们先看一张图: (1)浏览器发送一个请求会先到Tomcat的web服务器 (2)Tomcat服务器接收到请求以后,会去判断请求的是静态资源还是动态资源 (3)如 ...

  9. Android学习(十二) ContentProvider

    一.ContentProvider简介       当应用继承ContentProvider类,并重写该类用于提供数据和存储数据的方法,就可以向其他应用共享其数据.虽然使用其他方法也可以对外共享数据, ...

  10. android 学习随笔十二(网络:使用异步HttpClient框架)

    使用异步HttpClient框架发送get.post请求 在https://github.com/ 搜索 asyn-http https://github.com/search?utf8=✓& ...

随机推荐

  1. UVa10891 Game of Sum

    给定n个数字,A和B可以从这串数字的两端任意选数字,一次只能从一端选取.两人都采用最优策略,A先手,问A和B各自得到数字的和的差值最大为多少? 区间DP F[i][j]表示区间i~j内A能得到的最大数 ...

  2. 云服务器基本环境配置(php5.6+mysql+nginx)

    1.安装nginx sudo apt-get install nginx nginx 基本配置 server{ listen ; server_name www.xxxxxx.cn; root /va ...

  3. [Usaco2005 dec]Layout

    题目描述 当排队等候喂食时,奶牛喜欢和它们的朋友站得靠近些.FJ有N(2<=N<=1000)头奶牛,编号从1到N,沿一条直线站着等候喂食.奶牛排在队伍中的顺序和它们的编号是相同的.因为奶牛 ...

  4. strtol函數的用法 atof, atoi, atol, strtod, strtoul

    相关函数: atof, atoi, atol, strtod, strtoul表头文件: #include <stdlib.h>定义函数: long int strtol(const ch ...

  5. Cookies/Session机制详解

    # 转载自:https://blog.csdn.net/fangaoxin/article/details/6952954/ 会话(Session)跟踪是Web程序中常用的技术,用来跟踪用户的整个会话 ...

  6. python的递归算法学习(1)

    递归函数在函数内部,可以调用其他函数.如果一个函数在内部调用自身本身,这个函数就是递归函数.举个例子,我们来计算阶乘 n! = 1 * 2 * 3 * ... * n,用函数 fact(n)表示,可以 ...

  7. 我为什么喜欢Go语言

    从2000年至今,也写了11年代码了,期间用过VB.Delphi.C#.C++.Ruby.Python,一直在寻找一门符合自己心意和理念的 语言.我很在意写代码时的手感和执行的效率,所以在Go出现之前 ...

  8. Netty源码学习(一)Netty线程模型

    给你一台4路E7-4820V2(32核心64线程),512G内存的服务器,你该如何编程才能支持百万长连接? 最直接的想法是采用BIO的模式,为每个连接新建一个线程,在一一对应的线程中直接处理连接上的数 ...

  9. unittest (python标准库-开发工具-单元测试框架)

    unittest官方文档摘录 翻译 reffer to: https://docs.python.org/3/library/unittest.html#unittest.TextTestRunner ...

  10. java序列化的机制与原理

    有关Java对象的序列化和反序列化也算是Java基础的一部分,下面对Java序列化的机制和原理进行一些介绍. Java序列化算法透析 Serialization(序列化)是一种将对象以一连串的字节描述 ...