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=✓& ...
随机推荐
- parallel programming. this causual litery nots represents my recent progress in parallel programming in c#.It`s interesting.
not to say extra words,let`s start the code. pasted below: using System; using System.Collections.Ge ...
- linux0.11内核源码剖析:第一篇 内存管理、memory.c【转】
转自:http://www.cnblogs.com/v-July-v/archive/2011/01/06/1983695.html linux0.11内核源码剖析第一篇:memory.c July ...
- 基于UDT connect连接通信以及文件传输--客户端
上面一篇文章中提出了服务端的,其实这里没有严格意义的服务端和客户端之分,因为我在代码中是基于UDP的,不存在服务端与客户端,两个都是对等的,只是我这里进行一下简单的区分而已.在这里,客户端所进行的主要 ...
- flask的orm框架(SQLAlchemy)-操作数据
# 原创,转载请留言联系 Flask-SQLAlchemy 实现增加数据 用 sqlalchemy 添加数据时,一定要注意,不仅仅要连接到数据表,并且你的创建表的类也必须写进来.而且字段和约束条件要吻 ...
- Centos7一键安装lnmp脚本
mkdir /root/softwarewget http://mirrors.sohu.com/mysql/MySQL-5.7/mysql-boost-5.7.18.tar.gzwget https ...
- hdu 5139(离线处理+离散化下标)
Formula Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Sub ...
- printf()函数不能直接输出string类型
因为string不是c语言的内置数据,所以直接printf输出string类型的是办不到的. 要这样输出: printf("%s\n",a.c_str()); 举例: #inclu ...
- POJ 3620 Avoid The Lakes【DFS找联通块】
Avoid The Lakes Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6826 Accepted: 3637 D ...
- BFS+最小生成树+倍增+LCA【bzoj】4242 水壶
[bzoj4242 水壶] Description JOI君所居住的IOI市以一年四季都十分炎热著称. IOI市是一个被分成纵H*横W块区域的长方形,每个区域都是建筑物.原野.墙壁之一.建筑物的区域有 ...
- 理解boot.img与静态分析Android/linux内核
一些尝试和理解. 1>提取boot.img: 其中,msm代表是高通的芯片,msm_sdcc.1是外接的SD卡挂载的目录,by-name指的是这个sd卡分区的名称.下面几行代表每个分区存储的东西 ...