Android开发——Notification通知的使用及NotificationCopat.Builder常用设置API
想要看全部设置的请看这一篇 【转】NotificationCopat.Builder全部设置
常用设置:
|
设置属性 |
说明 |
|
setAutoCancel(boolean autocancel) |
设置点击信息后自动清除通知 |
|
setContent(RemoteView view) |
设置自定义通知 |
|
setContentTitle(String string) |
设置标题 |
|
setContentText(String string) |
设置内容 |
|
SetContentIntent(PendingIntent intent) |
设置点击信息后的跳转(意图) |
|
setWhen(long when) |
设置时间 |
|
setPriority(int pri) |
设置通知的重要程度 |
|
setStyle(Style style) |
设置样式 |
|
setVisibility(int visibility) |
设置锁屏显示 |
|
setDefault(int defaults) |
设置默认 |
|
setLight(int argb, int onMs, int offMs) |
设置呼吸灯闪烁效果 |
|
setSound(Uri sound) |
设置通知音效 |
|
setVibrate(long [] pattern) |
设置震动效果 |
|
setCategory(String category) |
设置通知类别 |
|
setColor(int argb) |
设置通知栏颜色 |
|
setFullScreenIntent(PendingIntent intent,boolean b) |
设置弹窗显示 |
简单步骤说明:
创建一个notification,需要NotificationManager(这是一个系统的服务类,由名字很容易知道是用来管理Notification通知类的)和Notification(通知类)
Notification创建类似Dialog的创建,通过Notification类中提供的各种方法来设置属性,最后build方法生成
NotificationManger的实例可以通过getSystemService这个方法获得
PendingIntent与Intent有关系,这是一个延迟意图,可以通过调用getActivity,getBroadcastReceiver,getService三个方法获得实例
普通使用:
NotificationCompat.Builder自动设置的默认值:
priority: PRIORITY_DEFAULT //通知的重要程度
when: System.currentTimeMillis() //时间
audio stream: STREAM_DEFAULT //音效 系统默认音效
上面的这三个我们一般情况下可以不用设置,系统会自动帮我们设置
下面就是一个简单的使用,设置了标题,内容,小图标,点击通知栏的该信息会跳转到main2Activity中,具体可以看注释,之后由这个普通使用的通知作为基础讲解NotificationCopat.Builder中的其他设置
Intent intent = new Intent(this,Main2Activity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this,0,intent,0);
NotificationManager manger = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
Notification notification = new NotificationCompat.Builder(this)
.setContentTitle("this is cotenttitle")//设置标题,必要
.setContentText("contentText")//设置内容,必要
.setWhen(System.currentTimeMillis())//设置时间,默认设置,可以忽略
.setSmallIcon(R.mipmap.ic_launcher)//设置通知栏的小图标,必须设置
.setAutoCancel(true)//设置自动删除,点击通知栏信息之后系统会自动将状态栏的通知删除,要与setContentIntent连用
.setContentIntent(pendingIntent)//设置在通知栏中点击该信息之后的跳转,参数是一个pendingIntent
.build();
manger.notify(1,notification);//id为1

setLargeIcon
Intent intent = new Intent(this,Main2Activity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this,0,intent,0);
NotificationManager manger = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
Notification notification = new NotificationCompat.Builder(this)
.setContentTitle("this is cotenttitle")//设置标题,必要
.setContentText("contentText")//设置内容,必要
.setWhen(System.currentTimeMillis())//设置时间,默认设置,可以忽略
.setSmallIcon(R.mipmap.ic_launcher)//设置通知栏的小图标,必须设置
.setAutoCancel(true)//设置自动删除,点击通知栏信息之后系统会自动将状态栏的通知删除
.setContentIntent(pendingIntent)//设置在通知栏中点击该信息之后的跳转,参数是一个pendingIntent
.setLargeIcon(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher))
//设置大图标,未设置时使用小图标代替,拉下通知栏显示的那个图标
//设置大图片 BitmpFactory.decodeResource(Resource res,int id) 根据给定的资源Id解析成位图
.build();
manger.notify(1,notification);

setPriority实现弹窗信息
Intent intent = new Intent(this,Main2Activity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this,0,intent,0);
NotificationManager manger = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
Notification notification = new NotificationCompat.Builder(this)
.setContentTitle("this is cotenttitle")//设置标题,必要
.setContentText("contentText")//设置内容,必要
.setWhen(System.currentTimeMillis())//设置时间,默认设置,可以忽略
.setSmallIcon(R.mipmap.ic_launcher)//设置通知栏的小图标,必须设置
.setAutoCancel(true)//设置自动删除,点击通知栏信息之后系统会自动将状态栏的通知删除
.setContentIntent(pendingIntent)//设置在通知栏中点击该信息之后的跳转,参数是一个pendingIntent
.setLargeIcon(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher))
//设置大图标,未设置时使用小图标代替,拉下通知栏显示的那个图标
//设置大图片 BitmpFactory.decodeResource(Resource res,int id) 根据给定的资源Id解析成位图
.setPriority(NotificationCompat.PRIORITY_MAX) //设置为最高重要程度
.setDefaults(NotificationCompat.DEFAULT_SOUND)//设置音效为默认
.build();
manger.notify(1,notification);
由于只有一条信息,我们可能看不出最高重要程度的区别,但是,我们如果再设置音效或者震动,那么这条通知就会弹窗显示

实现弹窗显示的两种方法:一,当重要程度为最高,且设置了音效或者是震动 ;二,调用setFullScreenIntent()
setFullScreenIntent
Intent intent = new Intent(this,Main2Activity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this,0,intent,0);
NotificationManager manger = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
Notification notification = new NotificationCompat.Builder(this)
.setContentTitle("this is cotenttitle")//设置标题,必要
.setContentText("contentText")//设置内容,必要
.setWhen(System.currentTimeMillis())//设置时间,默认设置,可以忽略
.setSmallIcon(R.mipmap.ic_launcher)//设置通知栏的小图标,必须设置
.setAutoCancel(true)//设置自动删除,点击通知栏信息之后系统会自动将状态栏的通知删除
.setContentIntent(pendingIntent)//设置在通知栏中点击该信息之后的跳转,参数是一个pendingIntent
.setLargeIcon(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher))
//设置大图标,未设置时使用小图标代替,拉下通知栏显示的那个图标
//设置大图片 BitmpFactory.decodeResource(Resource res,int id) 根据给定的资源Id解析成位图
.setFullScreenIntent(pendingIntent,false)
.build();
manger.notify(1,notification);
图片与之前那张一样,我就不放出来了,值得说的是这个方法的第二个参数,为什么是false呢,如果是true的话,当你在玩游戏的时候,屏幕是全屏,然后这条通知就不需要点击,直接就会跳转到某个界面去,除非是来电了才会用true,其他情况若是使用的话,用户估计直接卸载APP
setDefault
之前也是使用了这个设置与setPriority连用实现了弹窗消息,简单解释,这个可以设置音效,震动和呼吸灯的默认效果,也可以单独设置某个的默认,之前我就是单独设置了音效的默认
有四个参数可以选择,由英文也可以看出是什么意思了吧,这里就不多解释了

setStyle

这里我就简单地说两种,长文本显示和大图片显示,更多的请看这一篇Android开发——Notification通知的各种Style详解
一、BigTextStyle 长文本显示
Intent intent = new Intent(this,Main2Activity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this,0,intent,0);
NotificationManager manger = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
Notification notification = new NotificationCompat.Builder(this)
.setContentTitle("this is cotenttitle")//设置标题,必要
.setContentText("contentText")//设置内容,必要
.setWhen(System.currentTimeMillis())//设置时间,默认设置,可以忽略
.setSmallIcon(R.mipmap.ic_launcher)//设置通知栏的小图标,必须设置
.setAutoCancel(true)//设置自动删除,点击通知栏信息之后系统会自动将状态栏的通知删除
.setContentIntent(pendingIntent)//设置在通知栏中点击该信息之后的跳转,参数是一个pendingIntent
.setLargeIcon(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher))
//设置大图标,未设置时使用小图标代替,拉下通知栏显示的那个图标
//设置大图片 BitmpFactory.decodeResource(Resource res,int id) 根据给定的资源Id解析成位
.setStyle(new NotificationCompat.BigTextStyle().bigText("长内容长内容长内容长内容长内容长内容长内容长内容长内容长内容长
内容长内容长内容长内容长内容长内容长内容长内容长内容长内容"))
.build();
manger.notify(1,notification);

BigPictureStyle 大图片显示
Intent intent = new Intent(this,Main2Activity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this,0,intent,0);
NotificationManager manger = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
Notification notification = new NotificationCompat.Builder(this)
.setContentTitle("this is cotenttitle")//设置标题,必要
.setContentText("contentText")//设置内容,必要
.setWhen(System.currentTimeMillis())//设置时间,默认设置,可以忽略
.setSmallIcon(R.mipmap.ic_launcher)//设置通知栏的小图标,必须设置
.setAutoCancel(true)//设置自动删除,点击通知栏信息之后系统会自动将状态栏的通知删除
.setContentIntent(pendingIntent)//设置在通知栏中点击该信息之后的跳转,参数是一个pendingIntent
.setLargeIcon(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher))
//设置大图标,未设置时使用小图标代替,拉下通知栏显示的那个图标
//设置大图片 BitmpFactory.decodeResource(Resource res,int id) 根据给定的资源Id解析成位
.setStyle(new NotificationCompat.BigPictureStyle().bigPicture(BitmapFactory.decodeResource(getResources(),R.drawable.index)))
.build();
manger.notify(1,notification);

setContent
参数是一个RemoteView,由名字大概可以知道是一个View,定义一个布局文件,之后与其绑定,之后,为布局中的按钮设置onClick事件,之后调用setContent,remoteView作为参数传入
RemoteViews remoteView = new RemoteViews(getPackageName(),R.layout.notification);
remoteView.setOnClickPendingIntent(R.id.last_btn,pendingIntent);
个人觉得自定义布局可以做成像音乐播放器的通知栏那样,不过,今天就暂时简单的学习,之后有学到再补充
Android开发——Notification通知的使用及NotificationCopat.Builder常用设置API的更多相关文章
- Android开发——Notification通知的各种Style详解
本来是想与之前讲解使用Notification通知使用一起写的,查看了资料,觉得有必要将这Style部分单独拿出来讲解 前篇:Android开发——Notification通知的使用及Notifica ...
- Android开发中如何强制横屏和强制竖屏设置
Android开发中如何强制横屏和强制竖屏设置 强制横屏设置: 按照下面代码示例修改Activity的onResume方法 @Override protected void onResume() { ...
- android之Notification通知
我们在用手机的时候,如果来了短信,而我们没有点击查看的话,是不是在手机的最上边的状态栏里有一个短信的小图标提示啊?你是不是也想实现这种功能呢?今天的Notification就是解决这个问题的. pac ...
- Android开发之通过反射获取到挂断电话的API
Android开发黑名单工具类,需要用到挂断电话的API,但是该API处于隐藏状态,需要通过反射得到该方法.. 步骤: 1.通过当前类获取到ServiceManager的字节码 Class< ? ...
- Android开发自学笔记—1.1(番外)AndroidStudio常用功能介绍
一.界面区介绍 1.项目组织结构区,用于浏览项目文件,默认Project以Android组织方式展示. 2.设计区,默认在打开布局文件时为设计模式,可直接拖动控件到界面上实现所见即所得,下方的Desi ...
- Android开发UI之ListView中的Button点击设置
在ListView的Item中,如果有Button控件,那么要实现Button和Item点击都有响应,可以将Item的Layout中Button的focusable属性设为false,然后设置layo ...
- Android开发 - Retrofit 2 使用自签名的HTTPS证书进行API请求
为了确保数据传输的安全,现在越来越多的应用使用Https的方式来进行数据传输,使用https有很多有点,比如: HTTPS协议是由SSL+HTTP协议构建的可进行加密传输.身份认证的网络协议,要比ht ...
- android开发学习——关于activity 和 fragment在toolbar上设置menu菜单
在做一个项目,用的是Android Studio 系统的抽屉源码,但是随着页面的跳转,toolbar的title需要改变,toolbar上的menu菜单也需要改变,在网上找了好久,也尝试了很多,推荐给 ...
- 【转】NotificationCopat.Builder全部设置
1.方法:setContentTitle(CharSequence title) 功能:设置通知栏标题. 例子:setContentTitle("测试标题"). 2.方法: ...
随机推荐
- vue单页面应用刷新网页后vuex的state数据丢失的解决方案
1. 产生原因其实很简单,因为store里的数据是保存在运行内存中的,当页面刷新时,页面会重新加载vue实例,store里面的数据就会被重新赋值. 2. 解决思路一种是state里的数据全部是通过请求 ...
- 【mysql】must reset your password using ALTER USER statement before executing this statement
问题描述: must reset your password using ALTER USER statement before executing this statement 登录centos服务 ...
- CSS文字的跑马灯特效
上学时同学有个来电带跑马灯的手机,可把我羡慕坏了,可等我买的起手机时,跑马灯不流行了,甚伤萝卜心! 今天就用CSS做个文字的跑马灯特效,缅怀一下本萝卜逝去的青春! 道具:会敲代码的巧手.七窍玲珑心.会 ...
- Azure Web连接到Azure MySql Db
这个问题折腾了好一会,简单记录一下. 两种方式: 输入"规则名称"."起始 IP"和"结束 IP",然后单击"保存". ...
- react-native模拟机调试步骤详解 ——亲测有效!!!!
步骤 1 下载安装夜神模拟器,去夜神官网下载即可!然后安装完成!进入到初始化项目的目录,打开cmd命令,运行adb connect 127.0.0.1:62001 链接模拟器 2 链接完成之后,运行安 ...
- leetcode-查找和替换模式
一.题目描述 你有一个单词列表 words 和一个模式 pattern,你想知道 words 中的哪些单词与模式匹配.如果存在字母的排列 p ,使得将模式中的每个字母 x 替换为 p(x) 之后,我 ...
- [Swift]LeetCode41. 缺失的第一个正数 | First Missing Positive
Given an unsorted integer array, find the smallest missing positive integer. Example 1: Input: [1,2, ...
- Linux 实现服务器之间时间同步
在主服务器上安装NTP时间同步服务器 yum -y install ntp vim /etc/ntp.conf 手动添加两行 server 127.127.1.0 fudge 127.127.1.0 ...
- Dockerfile指令介绍
FROM:指定基础镜像 在Dockerfile中FROM是必备的指令,用于指定基础的镜像. FROM centos:latest LABEL:指定镜像标签 LABEL指令用来指定镜像的标签. 格式: ...
- 什么样的IT业务适合外包?
很多公司都应该遇到过这样的问题,什么样的IT业务才适合外包呢? 百度大家都可以百度的到一些信息,比如: 针对这样的推荐答案,是否也需要认真思考一下,我需要外包的业务是什么样的业务?是不是真的适合外包? ...