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"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="通知栏的使用" /><Buttonandroid:id="@+id/btn_send"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="发送通知"/><Buttonandroid: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;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);//通过系统服务来创建NotificationManagermanager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);btn_send = (Button) findViewById(R.id.btn_send);btn_send.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {sendNotication();}});btn_cancel = (Button) findViewById(R.id.btn_cancel);btn_cancel.setOnClickListener(new View.OnClickListener() {@Overridepublic 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);}}
转载于:https://www.cnblogs.com/zhengcheng/p/4437183.html
Android学习(二十)Notification通知栏相关推荐
- Android学习|控件——Notification通知
Android学习|控件--Notification通知 一.前提 二.两个对象的的构建 1.创建NotificationManager 2.使用Builder构造器来创建Notification 2 ...
- 十一、Android学习第十天——项目开始(转)
(转自:http://wenku.baidu.com/view/af39b3164431b90d6c85c72f.html) 十一.Android学习第十天--项目开始 Android知识点的学习告一 ...
- OpenCV学习(二十四 ):角点检测(Corner Detection):cornerHarris(),goodFeatureToTrack()
OpenCV学习(二十四 ):角点检测(Corner Detection):cornerHarris(),goodFeatureToTrack() 参考博客: Harris角点检测原理详解 Harri ...
- OpenCV学习(二十二) :反向投影:calcBackProject(),mixChannels()
OpenCV学习(二十二) :反向投影:calcHist(),minMaxLoc(),compareHist() 参考博客: 反向投影backproject的直观理解 opencv 反向投影 颜色直方 ...
- OpenCV学习(二十) :分水岭算法:watershed()
OpenCV学习(二十) :分水岭算法:watershed() 参考博客: OpenCV-分水岭算法 图像处理--分水岭算法 OpenCV学习(7) 分水岭算法(1) Opencv分水岭算法--wat ...
- PyTorch框架学习二十——模型微调(Finetune)
PyTorch框架学习二十--模型微调(Finetune) 一.Transfer Learning:迁移学习 二.Model Finetune:模型的迁移学习 三.看个例子:用ResNet18预训练模 ...
- C1认证学习二十六(基础选择器)
C1认证学习二十六(基础选择器) 任务背景 CSS选择器是CSS规则的一部分,用来指定需要设置的样式的HTML元素,通过选择器可以实现CSS对HTML元素一对一或者一对多或者多对一的控制了啦. 任务目 ...
- 知识图谱论文阅读(八)【转】推荐系统遇上深度学习(二十六)--知识图谱与推荐系统结合之DKN模型原理及实现
学习的博客: 推荐系统遇上深度学习(二十六)–知识图谱与推荐系统结合之DKN模型原理及实现 知识图谱特征学习的模型分类汇总 知识图谱嵌入(KGE):方法和应用的综述 论文: Knowledge Gra ...
- Android进阶(二十八)上下文菜单ContextMenu使用案例
上下文菜单ContextMenu使用案例 前言 回顾之前的应用程序,发现之前创建的选项菜单无法显示了.按照正常逻辑来说,左图中在"商品信息"一栏中应该存在选项菜单,用户可进行分享等 ...
- VUE学习(二十)、插槽
VUE学习(二十).插槽 一.默认插槽 1.Category.vue <template><div class="category"><h3>{ ...
最新文章
- 大脑如何编码视觉信息?动态电极到图像(DETI) 映射技术也许有助于我们揭示其原理...
- 如何使用 Spinnaker 和 Kubernetes 进行数据库变更发布?
- 网站优化中应该注意的细节有哪些?
- Luogu3694 邦邦的大合唱站队 (状压DP)
- 【FFmpeg】ffplay 播放视频命令 ( 播放 | 暂停 | 停止 | 音量控制 | 进度控制 | 音频流 / 视频流 / 字幕流 / 节目切换 )
- 转载-如何做好项目的需求与业务调研▲▲▲
- 每天一道LeetCode-----逆序链表
- MyBatis从入门到精通:update用法、delete用法
- vue中warning_vue项目运行提示Warnings while compiling.警告的解决方法
- HTML5 Canvas平移,放缩,旋转演示
- 6-2 函数式编程例一
- 《深入理解Java虚拟机》 第4章 虚拟机性能监控与故障处理工具
- javascript垃圾回收机制
- 学习笔记(01):5天Python闯关训练营-103期-re模块使用案例
- poj Gone Fishing 枚举加贪心 当初做的很纠结啊!!终于A了,与大家分享一下经验
- 2017年前端该学些什么(译)
- Tomcat项目get请求中参数乱码问题
- pb 打开文件,写入文件
- cei()、linspace()、arrange()、full()、eye()、empty()、random()
- 不讲废话,全程硬核,处理结构化数据的终极解决方案