一、通知栏的内容

  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. Java中同一个类中不同的synchronized方法是否可以并发执行?

    答案是: 不可以,因为都是获取到对象本身的锁. 多个线程访问同一个类的synchronized方法时, 都是串行执行的 ! 就算有多个cpu也不例外 ! synchronized方法使用了类java的 ...

  2. Java的运算符及结合性

    优先级 运算符 结合性 1 ()[]. 从左向右 2 !+(正)-(负)~ ++ -- 从右向左 3 * / % 从左向右 4 +(加)-(减) 从左向右 5 <<      >&g ...

  3. 实现实体类和Xml相互转化

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.X ...

  4. c /c++变参函数(转)

    原文转自 https://blog.csdn.net/wwzcx/article/details/8940092 实现c/c++语言的变参函数. 变参函数 :void fun(para,...) 变参 ...

  5. Webpack & The Hot Module Replacement热模块替换原理解析

    Webpack & The Hot Module Replacement热模块替换原理解析 The Hot Module Replacement(HMR)俗称热模块替换.主要用来当代码产生变化 ...

  6. Mac下jdk多版本管理

    网上试了.bash_profile中增加路径设置别名的方法,但是始终无法切换,只能使用jenv了. 1. 下载 jenv(来自官网) git clone https://github.com/gcui ...

  7. python3中用HTMLTestRunner.py报ImportError: No module named 'StringIO'如何解决【转载】

    原文转自:http://bbs.chinaunix.net/thread-4154743-1-1.html python3中用HTMLTestRunner.py报ImportError: No mod ...

  8. [BZOJ4756][Usaco2017 Jan]Promotion Counting 树状数组

    4756: [Usaco2017 Jan]Promotion Counting Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 305  Solved: ...

  9. MyEclipse 10.0安装图解

  10. linux之rpm指令

    rmp原本是Red Hat Linux发行版专门用来管理Linux各项套件的程序,由于它遵循GPL规则且功能强大方便,因而广受欢迎.逐渐受到其他发行版的采用.RPM套件管理方式的出现,让Linux易于 ...