Android学习(二十)Notification通知栏
一、通知栏的内容
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通知栏的更多相关文章
- 十一、Android学习第十天——项目开始(转)
(转自:http://wenku.baidu.com/view/af39b3164431b90d6c85c72f.html) 十一.Android学习第十天——项目开始 Android知识点的学习告一 ...
- Android进阶(二十)AndroidAPP开发问题汇总(四)
· Android进阶(二十)AndroidAPP开发问题汇总(四) android:layout_width和android:width的区别 基中的android:layout_width和and ...
- Android进阶(二十八)上下文菜单ContextMenu使用案例
上下文菜单ContextMenu使用案例 前言 回顾之前的应用程序,发现之前创建的选项菜单无法显示了.按照正常逻辑来说,左图中在"商品信息"一栏中应该存在选项菜单,用户可进行分享等 ...
- Java开发学习(二十二)----Spring事务属性、事务传播行为
一.事务配置 上面这些属性都可以在@Transactional注解的参数上进行设置. readOnly:true只读事务,false读写事务,增删改要设为false,查询设为true. timeout ...
- Java开发学习(二十四)----SpringMVC设置请求映射路径
一.环境准备 创建一个Web的Maven项目 参考Java开发学习(二十三)----SpringMVC入门案例.工作流程解析及设置bean加载控制中环境准备 pom.xml添加Spring依赖 < ...
- Java开发学习(二十五)----使用PostMan完成不同类型参数传递
一.请求参数 请求路径设置好后,只要确保页面发送请求地址和后台Controller类中配置的路径一致,就可以接收到前端的请求,接收到请求后,如何接收页面传递的参数? 关于请求参数的传递与接收是和请求方 ...
- Java开发学习(二十六)----SpringMVC返回响应结果
SpringMVC接收到请求和数据后,进行了一些处理,当然这个处理可以是转发给Service,Service层再调用Dao层完成的,不管怎样,处理完以后,都需要将结果告知给用户. 比如:根据用户ID查 ...
- Java开发学习(二十八)----拦截器(Interceptor)详细解析
一.拦截器概念 讲解拦截器的概念之前,我们先看一张图: (1)浏览器发送一个请求会先到Tomcat的web服务器 (2)Tomcat服务器接收到请求以后,会去判断请求的是静态资源还是动态资源 (3)如 ...
- Android学习(十二) ContentProvider
一.ContentProvider简介 当应用继承ContentProvider类,并重写该类用于提供数据和存储数据的方法,就可以向其他应用共享其数据.虽然使用其他方法也可以对外共享数据, ...
- android 学习随笔十二(网络:使用异步HttpClient框架)
使用异步HttpClient框架发送get.post请求 在https://github.com/ 搜索 asyn-http https://github.com/search?utf8=✓& ...
随机推荐
- codechef AUG17 T1 Chef and Rainbow Array
Chef and Rainbow Array Problem Code: RAINBOWA Chef likes all arrays equally. But he likes some array ...
- 使用select2插件并添加拼音首字母检索
项目中要使用下拉检索的时候要支持拼音首字母.本来拼音可以写后台,这里放前台了. 放代码 1. pinyin.js ,最后为了使用方便,直接为string对象添加了扩展方法 /* File Create ...
- 非常好的博客!!!linux内存管理概述【转】
转自:http://blog.csdn.net/bullbat/article/details/7166140 inux内存管理建立在基本的分页机制基础上,在linux内核中RAM的某些部分将会永久的 ...
- 【数据库】E-R模型
E-R模型 实体:客观存在并可相互区别的事物称为实体.可以是具体的人.事.物或抽象的概念. 属性:实体所具有的某一特性称为属性.一个实体可以由若干个属性来刻画. 联系:现实世界中事物内部以及事物之间的 ...
- RobotFramework自动化4-批量操作案例【转载】
本篇转自博客:上海-悠悠 原文地址:http://www.cnblogs.com/yoyoketang/tag/robotframework/ 前言 有时候一个页面上有多个对象需要操作,如果一个个去定 ...
- J.U.C并发框架源码阅读(十)ConcurrentLinkedQueue
基于版本jdk1.7.0_80 java.util.concurrent.ConcurrentLinkedQueue 代码如下 /* * ORACLE PROPRIETARY/CONFIDENTIAL ...
- (1)WPF概述
一.wpf特点 winfrom使用GID/GID+ 图形引擎,wpf使用directx的图形引擎 通过directx提供硬件加速 类型web的布局模型 丰富的绘图模型.文本模型, 支持音频视频 可创建 ...
- (6)sql/puls
host 在sql/puls中使用cmd或linux操作系统的命令
- 洛谷——P1614 爱与愁的心痛
题目背景 (本道题目隐藏了两首歌名,找找看哪~~~) <爱与愁的故事第一弹·heartache>第一章 <我为歌狂>当中伍思凯神曲<舞月光>居然没赢给萨顶顶,爱与愁 ...
- c# await 到底等待的是什么?
static void Main(string[] args) { Print(); Console.WriteLine("5 :::" + Thread.CurrentThrea ...