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=✓& ...
随机推荐
- 51nod 1851 俄罗斯方块
玩过俄罗斯方块?那你知道俄罗斯方块共有七种吧(其实只有五种) 给一个黑白图,每次能将某些区域的格子黑白反转,至于某些区域的意思嘛,就是俄罗斯方块形状的区域咯(可水平翻转.上下翻转.旋转) 求能否将图变 ...
- ios截屏代码[转]
http://www.cnblogs.com/chenxiangxi/p/3547974.html 这位博主的连接中将ios自定义大小位置的截屏代码写的很不错,马上就能用的方法,对于只想马上用的程序员 ...
- openGL深度缓冲区问题
http://zhidao.baidu.com/question/368299839.html&__bd_tkn__=6aa9196c746cd3357f1eec74aeb127b395029 ...
- cocos2d programming guide 翻译 引导页(完结)
http://bbs.tairan.com/article-25-1.html Cocos2d官方入门指导 原文地址:http://www.cocos2d-iphone.org/wiki/doku. ...
- Spring集成JavaMail并利用线程池发送邮件
我们系统存在大量发送邮件的需求,项目使用的是Spring框架而JavaMail也能很好的跟Spring进行集成,由于发送邮件最好还是使用异步进行发送,所以这里就采用线程池+JavaMail进行邮件发送 ...
- Jquery实现逐屏加载图片
引用jquery.scrollLoading.js $(document).ready(function () { //实现图片慢慢浮现出来的效果 $("img").load(fu ...
- 洛谷——P1403 [AHOI2005]约数研究
P1403 [AHOI2005]约数研究 题目描述 科学家们在Samuel星球上的探险得到了丰富的能源储备,这使得空间站中大型计算机“Samuel II”的长时间运算成为了可能.由于在去年一年的辛苦工 ...
- [P2023][AHOI2009]维护序列(线段树)
题目描述 老师交给小可可一个维护数列的任务,现在小可可希望你来帮他完成. 有长为N的数列,不妨设为a1,a2,…,aN .有如下三种操作形式: (1)把数列中的一段数全部乘一个值; (2)把数列中的一 ...
- unity3d 网页游戏客户端工程构建方案
将一个项目分为两个编辑环境,一个是editor,一个是target. editor只是策划人员拖拖拽拽编辑场景,打包时程序自动将每个场景资源打包生成一个XXX.unity3d文件,并最后生成一个场景配 ...
- NOIP 2017 赛后反思 [补档]
首先写一下比赛的情况: D1: T1: 之前做过类似的题目, 因而知道大致的结论, 迅速完成. T2: 貌似直接模拟就可以了, 涉及到字符串信息提取, 比较麻烦, 因而想放到最后做. T3: 非常简洁 ...