当前位置: 首页 > 编程日记 > 正文

android之Notification通知

我们在用手机的时候,如果来了短信,而我们没有点击查看的话,是不是在手机的最上边的状态栏里有一个短信的小图标提示啊?你是不是也想实现这种功能呢?今天的Notification就是解决这个问题的。

package cn.com.chenzheng_java;import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore.Audio;
import android.view.View;
import android.widget.Button;/**** @description 状态栏通知相关* @author chenzheng_java* */
public class NotificationActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.notification);Button button = (Button) findViewById(R.id.button);button.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {addNotificaction();}});}/*** 添加一个notification*/private void addNotificaction() {NotificationManager manager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);// 创建一个NotificationNotification notification = new Notification();// 设置显示在手机最上边的状态栏的图标notification.icon = R.drawable.excel;// 当当前的notification被放到状态栏上的时候,提示内容notification.tickerText = "注意了,我被扔到状态栏了";/**** notification.contentIntent:一个PendingIntent对象,当用户点击了状态栏上的图标时,该Intent会被触发* notification.contentView:我们可以不在状态栏放图标而是放一个view* notification.deleteIntent 当当前notification被移除时执行的intent* notification.vibrate 当手机震动时,震动周期设置*/// 添加声音提示notification.defaults=Notification.DEFAULT_SOUND;// audioStreamType的值必须AudioManager中的值,代表着响铃的模式notification.audioStreamType= android.media.AudioManager.ADJUST_LOWER;//下边的两个方式可以添加音乐//notification.sound = Uri.parse("file:///sdcard/notification/ringer.mp3"); //notification.sound = Uri.withAppendedPath(Audio.Media.INTERNAL_CONTENT_URI, "6"); Intent intent = new Intent(this, Notification2Activity.class);PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);// 点击状态栏的图标出现的提示信息设置notification.setLatestEventInfo(this, "内容提示:", "我就是一个测试文件", pendingIntent);manager.notify(1, notification);}}

点击按钮时候,状态栏会显示:

看到了吧,状态栏多出来一个excel图标,当我按住图标不放,往下拖动的时候,出来了这个页面

然后,当我们点击这个对话框之后,就会触发intent,跳转到Notification2Activity.java这个activity。

----------------------------------------------------------------------------------------

注意,NotificationManager里的notify(id,notification)中的id是用来唯一标识我们当前的这个notification的标识符,我们通过cancel方法删除通知时,传递的就是这个值。可能读者在看很多文档的时候,发现这个地方指定了一个莫名奇妙的值,例如R.drawable.icon,很多朋友就纳闷了,为什么这里要指定一个图片呢。这里笔者就介绍下,为什么呢?

答案其实很简单,我们都知道,我们这里对参数的唯一要求就是,这个id要和notify方法中的一致,并且是唯一;只要满足了这两项,其他的都无所谓。notify和cancel里一致我们作为开发者,太好控制了,但是唯一呢,我们还真不好说,于是这里就有些人动小脑筋了,很巧妙的用了我们系统中的图片资源或者其他资源的索引ID,我们都知道,这些值肯定都是唯一的!

------------------------------------------------------------------------------------------

下面是从网上找的一些资料:

如果要添加一个Notification,可以按照以下几个步骤

1:获取NotificationManager:

NotificationManager m_NotificationManager=(NotificationManager)this.getSystemService(NOTIFICATION_SERVICE);

2:定义一个Notification:

  Notification  m_Notification=new Notification();

3:设置Notification的各种属性:

 //设置通知在状态栏显示的图标
m_Notification.icon=R.drawable.icon;
                
 //当我们点击通知时显示的内容
m_Notification.tickerText="Button1 通知内容.....";
                                
通知时发出的默认声音
m_Notification.defaults=Notification.DEFAULT_SOUND;

//设置通知显示的参数

Intent   m_Intent=new Intent(NotificationDemo.this,DesActivity.class);       
PendingIntent m_PendingIntent=PendingIntent.getActivity(NotificationDemo.this, 0, m_Intent, 0);

m_Notification.setLatestEventInfo(NotificationDemo.this, "Button1", "Button1通知",m_PendingIntent );

//这个可以理解为开始执行这个通知
m_NotificationManager.notify(0,m_Notification);

4:既然可以增加同样我们也可以删除。当然是只是删除你自己增加的。

  m_NotificationManager.cancel(0);   

  这里的0是一个ID号码,和notify第一个参数0一样。

这也就完成了,添加删除工作。

 

------------------------------------------------------------------------------------------------------

NoticificationManager很容易可以放在状态栏,也很容易实现从statusbar进入程序 中, 
NoticificationManager中通过intent执行此程序的activity就可以了

NoticificationManager状态栏操作

NotificationManager(通知管理器): 
NotificationManager负责通知用户事件的发生. 
NotificationManager有三个公共方法: 
1. cancel(int id) 取消以前显示的一个通知.假如是一个短暂的通知,试图将隐藏,假如是一个持久的通知,将从状态条中移走. 
2. cancelAll() 取消以前显示的所有通知. 
3. notify(int id,  Notification notification) 把通知持久的发送到状态条上.


//初始化NotificationManager: 
NotificationManager nm = 
      (NotificationManager)getSystemService(NOTIFICATION_SERVICE);

Notification代表着一个通知. 
Notification的属性: 
audioStreamType 当声音响起时,所用的音频流的类型 
contentIntent 当通知条目被点击,就执行这个被设置的Intent. 
contentView 当通知被显示在状态条上的时候,同时这个被设置的视图被显示. 
defaults 指定哪个值要被设置成默认的. 
deleteIntent 当用户点击"Clear All Notifications"按钮区删除所有的通知的时候,这个被设置的Intent被执行. 
icon 状态条所用的图片. 
iconLevel 假如状态条的图片有几个级别,就设置这里. 
ledARGB LED灯的颜色. 
ledOffMS LED关闭时的闪光时间(以毫秒计算) 
ledOnMS LED开始时的闪光时间(以毫秒计算) 
number 这个通知代表事件的号码 
sound 通知的声音 
tickerText 通知被显示在状态条时,所显示的信息 
vibrate 振动模式. 
when 通知的时间戳.

将Notification发送到状态条上: 
Notification notification = new Notification(); 
Notification的设置过程…….. 
nm.notify(0, notification);   //发送到状态条上

 

------------------------------------------------------------------------------------------------------------

Notification提供了丰富的手机提示方式:

a)在状态栏(Status Bar)显示的通知文本提示,如:

notification.tickerText = "hello";

 

b)发出提示音,如:

notification.defaults = Notification.DEFAULT_SOUND;

notification.sound = Uri.parse("file:///sdcard/notification/ringer.mp3");

notification.sound = Uri.withAppendedPath(Audio.Media.INTERNAL_CONTENT_URI, "6");

 

c)手机振动,如:

notification.defaults = Notification.DEFAULT_VIBRATE;

long[] vibrate = {0,100,200,300};

notification.vibrate = vibrate;

 

d)LED灯闪烁,如:

notification.defaults = Notification.DEFAULT_LIGHTS;

notification.ledARGB = 0xff00ff00;

notification.ledOnMS = 300;

notification.ledOffMS = 1000;

notification.flags = Notification.FLAG_SHOW_LIGHTS;

 

4)发送通知:

private static final int ID_NOTIFICATION = 1;

mNotificationManager.notify(ID_NOTIFICATION, notification);

转自:http://blog.csdn.net/chenzheng_java/article/details/6249357

转载于:https://www.cnblogs.com/welcoming/p/4120879.html

相关文章:

7 OC 中class 类的结构

目录 一 OC 中class 的结构 https://opensource.apple.com/tarballs/objc4/ 在最新的objc源码中 化繁就简来看的话 是以下结构 struct objc_class : objc_object {objc_class(const objc_class&) delete;objc_class(objc_class&&) delete;void operator(con…

apple id无法创建_我们如何使用Apple的学习框架来创建我们的第一个应用程序

apple id无法创建by Jonata Corra由JonataCorra 我们如何使用Apple的学习框架来创建我们的第一个应用程序 (How we used Apple’s learning framework to create our first app) After one month of work, my team and I finished the first version of Echo, our tracker iOS…

个人作业1:小学四则运算——基于控制台

a.需求分析: 自动生成小学四则运算题目的命令行 “软件”,满足以下需求: 除了整数以外,还要支持真分数的四则运算,真分数的运算,例如:1/6 1/8 7/24运算符为 , −, , 并且要求能处理用户…

getchar返回int类型

#include <stdio.h> /* copy input to output; 2nd version */main(){int c;c getchar();while(c ! EOF){putchar(c);c getchar();}} 直觉告诉我getchar返回值应该是char类型的&#xff0c;这个地方为什么不能用char类型来存储getchar()的返回值呢&#xff1f; 其实文中…

8 iOS中KVO 的本质

前言本质 Automatic key-value observing is implemented using a technique called isa-swizzling 这计划的意思就是 自动的键值观察的实现基于 isa-swizzling 原理 1.KVO是基于runtime机制实现的 2.当某个类的属性对象第一次被观察时&#xff0c;系统就会在运行期动态地创…

完成工作表-使用Google Spreadsheets作为数据后端

by Gilad Dayagi通过吉拉德达亚吉 完成工作表-使用Google Spreadsheets作为数据后端 (Get Sheet Done — using Google Spreadsheets as your data backend) If you want to rapidly prototype your next web apps, try using Google Spreadsheets as your data backend.如果您…

BIEE-CSS样式大全

字体属性&#xff1a;(font) 大小 {font-size: x-large;}(特大) xx-small;(极小) 一般中文用不到&#xff0c;只要用数值就可以&#xff0c;单位&#xff1a;PX、PD 样式 {font-style: oblique;}(偏斜体) italic;(斜体) normal;(正常) 行高 {line-height: normal;}(正常) 单位&…

基于verilog的FPGA编程经验总结(XILINX ISE工具)

1.用ISE仿真的时候.所用变量一定要初始化. ISE默认初始量为"XXXXX", 而Quarters是默认为"00000"的, 其实实际上, 下到FPGA里后也是默认为0的,只是可以说ISE严谨得令人DT吧.比如说用一个累加器, result ABresult ,必须保证在某一刻A, B, result都为定值时,…

6 OC 中的isa 指针

目录 一 isa 指针 二 类对象中的superclass 一 isa 指针 isa 指针 &#xff0c;OC 中的对象都是有的 如下图所示&#xff0c;实例对象isa 指针指向 类对象&#xff0c;类对象的isa 指针指向 元类对象 二 类对象中的superclass superclass 有什么用呢&#xff1f; 比如说创…

btf-raft共识算法_了解Raft共识算法:学术文章摘要

btf-raft共识算法by Shubheksha通过Shubheksha 了解Raft共识算法&#xff1a;学术文章摘要 (Understanding the Raft consensus algorithm: an academic article summary) This post summarizes the Raft consensus algorithm presented in the paper In Search of An Underst…

iOS asset 中定义颜色,xib中便捷访问

在aseet 中定义一个颜色 这样就可以在xib 中访问颜色了&#xff0c;这样就不用重复的去输入

三种序列化方式性能比较

一下代码比较了二进制序列化、xml序列化、Protobuf序列化的运行时间&#xff0c;可是代码显得十分冗余&#xff0c;是否有大神可以指点一二&#xff0c;万分感谢 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO; usi…

mac mini 装UBUNTU后没有WIFI解决办法

1、在终端中运行如下命令&#xff0c;重新安装b43相关的全部驱动和firmware: 复制代码代码如下:sudo apt-get install bcmwl-kernel-source #Broadcom 802.11 Linux STA 无线驱动源sudo apt-get install broadcom-sta-commonsudo apt-get install broadcom-sta-sourcesudo apt-…

区块链c端应用小程序_区块链如何真正起作用? 我建立了一个应用程序向您展示。...

区块链c端应用小程序by Sean Han通过肖恩韩 区块链如何真正起作用&#xff1f; 我建立了一个应用程序向您展示。 (How does blockchain really work? I built an app to show you.) According to Wikipedia, a blockchain is:根据维基百科&#xff0c;一个区块链是&#xff1…

HDU 4913 Least common multiple

/* hdu4913 Least common multiple http://acm.hdu.edu.cn/showproblem.php?pid4913 离散化 线段树 统计逆序数思想 tips: 1、线段树中一定要到处都取模&#xff0c;否则wa。。。 2、lazy是乘积的形式出现&#xff0c;不是加和*/ #include <cstdio> #include <algori…

JS ES6 实用笔记

微信小程序开发交流qq群 581478349 承接微信小程序开发。扫码加微信。 这篇博文我会一直更新。 一.导出导入的两中方式 1.export //demo1.js export const a 6 导入语法为&#xff1a; import {a} form demo1 2.export default //demo2.js export default const b 6 …

extjs editgrid增加一行

Ext.onReady(function(){ /* * EditorGridPanel的工作过程 * 1、用户点击单元格 * 2、单元格按照预设的组件显示单元格的内容并处于编辑状态 * 3、离开单元格的编辑状态 * 4、更新编辑后的内容&#xff0c;出现三角号表示已经被修改过 * 5、程序内部变化&#xff1a;将记录设置…

unity 骨骼击碎_保证击碎$ 100挑战的创新策略

unity 骨骼击碎by Glenn Gonda由Glenn Gonda 保证击碎$ 100挑战的创新策略 (A Creative Strategy Guaranteed to Crush the $100 Challenge) Before I became a software engineer, I made my living as a recording studio engineer. I have a non-traditional background an…

mac下安装libpng环境

用go写一个爬虫工具时需要使用一个go的库&#xff0c;而这个库有需要使用libpng库&#xff0c;不然编译就会提示说 png.h找不到等之类的信息&#xff0c;于是想到应该和windows一样需要安装gcc环境&#xff0c;然后让gcc里安装libpng这个库&#xff0c; 解决办法&#xff1a; 终…

linux oracle修改编码utf8

$ sqlplus /nolog SQL> connect sys/oracle as sysdba SQL> startup 如何设置ORACLE数据库的编码&#xff08;ZHS16GBK&#xff09;修改成UTF8 SQL> shutdown immediate; SQL> startup mount; SQL> alter system enable restricted session; SQL> alter sy…

Vue.js 数据绑定渲染Demo

微信小程序开发交流qq群 581478349 承接微信小程序开发。扫码加微信。 <div id"app">{{ message }} </div>var app new Vue({el: #app,data: {message: Hello Vue!} })Hello Vue!

angular搭建项目步骤_建立健康的Angular项目应采取的步骤

angular搭建项目步骤by Ashish Gaikwad通过Ashish Gaikwad 建立健康的Angular项目应采取的步骤 (Steps you should take to build a healthy Angular project) 使用Jenkins SonarQube创建您的“ Angular Fitbit” (Create your “Angular Fitbit” with Jenkins SonarQube) …

数据库的三大范式和事物

来源&#xff1a;http://blog.csdn.net/w__yi/article/details/19934319 1.1 第一范式&#xff08;1NF&#xff09;无重复的列 1.2 第二范式&#xff08;2NF&#xff09;属性完全依赖于主键 [ 消除部分子函数依赖 ] 1.3 第三范式&#xff08;3NF&#xff09;属性不依赖于其它非…

过滤器和包装器

作者&#xff1a;禅楼望月 过滤器要做的事情 请求过滤器 完成安全检查 重新格式化请求首部或体 建立请求审计或日志响应过滤器 压缩响应流 追加或修改响应流 创建一个完全不同的响应注意不能把过滤器的顺序依赖性硬编码进程序中&#xff0c;它应该由DD控制。 过滤器很像Servlet…

Missing space before value for key 'path'vue.js解决空格报错

微信小程序开发交流qq群 581478349 承接微信小程序开发。扫码加微信。 找到 webpack.base.config.js文件注释掉下面的东西&#xff01;&#xff01; module: { rules: [ /*{ test: /\.(js|vue)$/, loader: eslint-loader, enforce: "p…

现代hy-9600音响_从音响工程师到软件工程师-为什么我要学习编码

现代hy-9600音响by Kalalau Cantrell通过Kalalau Cantrell 从音响工程师到软件工程师-为什么我要学习编码 (From Sound Engineer to Software Engineer — Why I’m Learning to Code) I seriously started teaching myself to code several months ago. I say “seriously” …

微信服务号、公众号、企业号注册

转载于:https://www.cnblogs.com/zhoulaoshi/p/6536850.html

a标签onclick事件解析

微信小程序开发交流qq群 581478349 承接微信小程序开发。扫码加微信。 简单介绍<a>标签的常用点击事件的写法及作用 a href"javascript:void(0);" οnclick"js_method()" //javascript:void(0);作用是返回undefined&#xff0c;地址不发生跳转&am…

安卓版文字扫描识别软件

安卓版文字扫描识别软件 文字识别软件被越来越多的人使用&#xff0c;在使用的过程中也发现了一些问题。总结这些问题发现&#xff0c;很多人对软件能够批量识别这个问题比较关注。如果实现批量识别就可以节省时间。但是一些软件还不能实现批量识别&#xff0c;还有的软件能够做…

中级前端笔试_在短短8个月内如何获得中级前端开发人员的角色

中级前端笔试by Matthew Burfield通过马修伯菲尔德(Matthew Burfield) 在短短8个月内如何获得中级前端开发人员的角色 (How I got a mid-level front end developer role in just 8 months) Three weeks ago I landed a mid-level front-end developer role at a startup. Our…