通知可以在活动、广播接收器,服务里创建

一、通知基本用法

  1. Context.getSystemService(Context.NOTIFICATION_SERVICE)获取到NotificationManager对通知管理:
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE)
  1. Builder构造器创建Notification对象,channelId用来表示消息通道的ID,自定义一个填上即可
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "unique");
  1. 基本设置
builder.setContentTitle("这是标题").setAutoCancel(true).setContentText("这是文本").setContentInfo("这是内容").setSubText("这是小字").setTicker("滚动消息......").setWhen(System.currentTimeMillis()) // 出现时间.setSmallIcon(R.mipmap.ic_launcher) //小图标.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher)); // 大图标// 大于Android 8.0的版本适配
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {NotificationChannel notificationChannel = null;notificationChannel = new NotificationChannel("unique", "TEST", NotificationManager.IMPORTANCE_HIGH);notificationChannel.enableLights(true); //LED灯notificationChannel.setLightColor(Color.YELLOW);manager.createNotificationChannel(notificationChannel);}
  1. 显示通知,第一个参数是自定义的id,第二个参数是对象
Notification notification = builder.build();
manager.notify(1, notification);

新建一个空项目 day14_NotificationTest

主布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"><Buttonandroid:layout_width="match_parent"android:layout_height="wrap_content"android:id="@+id/send_notice"android:text="发起通知"/></LinearLayout>

主活动:

public class MainActivity extends AppCompatActivity implements View.OnClickListener {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);Button button_sendNotice = findViewById(R.id.send_notice);button_sendNotice.setOnClickListener(this);}@Overridepublic void onClick(View v) {switch (v.getId()) {case R.id.send_notice:Intent intent = new Intent(this, NotificationActivity.class);PendingIntent pi = PendingIntent.getActivity(this, 0, intent, 0);NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "unique");builder.setContentTitle("这是标题").setAutoCancel(true).setContentText("这是文本").setContentInfo("这是内容").setSubText("这是小字").setTicker("滚动消息......").setWhen(System.currentTimeMillis()) // 出现时间.setSmallIcon(R.mipmap.ic_launcher) //小图标.setContentIntent(pi)  //大图标.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher)); // 大图标Notification notification = builder.build();manager.notify(1, notification);break;default:break;}}
}

三、通知的响应

通知响应依赖于PendingIntent,分别用于活动,广播,和服务,其对应静态方法:getActivity(),getBroadcast(),getService()

  • 第一个参数:Context
  • 第二个参数:通常直接写0
  • 第三个参数:Intent
  • 第四个参数:可以选择四个:FLAG_ONE_SHOTFLAG_NO_CREATEFLAG_CANCEL_CURRENTFLAG_UPDATE_CURRENT。但通常写0

新建一个空活动 NotificationActivity,对应布局notification_layout

通知布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_centerInParent="true"android:textSize="24sp"android:text="这一页是通知布局"/></RelativeLayout>

主活动给通知加入点击功能:

    public void onClick(View v) {switch (v.getId()) {case R.id.send_notice:Intent intent = new Intent(this, NotificationActivity.class);PendingIntent pi = PendingIntent.getActivity(this, 0, intent, 0);NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "001");builder.setContentTitle("这是标题").setContentText("这是文本").setContentInfo("这是内容").setSubText("这是小字").setTicker("滚动消息......").setWhen(System.currentTimeMillis()).setSmallIcon(R.mipmap.ic_launcher).setContentIntent(pi).setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher));。。。。。。

运行:

让通知消失的两种方法:

  1. NotificationCompat.Builder中连缀一个setAutoCancel(true)方法,通知被点击后就会自动清除:
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "001").setAutoCancel(true);
  1. 显示调用NotificationMangercancel()将它取消:
public class NotificationActivity extends AppCompatActivity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.notification_layout);NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);manager.cancel(1);}
}

这里的 1,是通知的id,之前设置好的。

四、通知的进阶技巧

设置通知的音效:

builder.setSound(Uri.fromFile(new File("铃声路径")));

设置振动【偶数表示振动的毫秒数,奇数表示接着静止的毫秒数】:

builder.setVibrate(new long[]{0, 1000, 1000, 1000});
// 8.0 以上要添加这个:
channel.setVibrationPattern(pattern);

不过设置振动需要先添加权限:

<uses-permission android:name="android.permission.VIBRATE"/>

显示长文字:

builder.setStyle(new NotificationCompat.BigTextStyle().bigText("唧唧复唧唧,木兰当户织。不闻机杼声,惟闻女叹息。\n" +"问女何所思,问女何所忆。女亦无所思,女亦无所忆。昨夜见军帖,可汗大点兵,军书十二卷,卷卷有爷名。阿爷无大儿,木兰无长兄,愿为市鞍马,从此替爷征。\n" +"东市买骏马,西市买鞍鞯,南市买辔头,北市买长鞭。旦辞爷娘去,暮宿黄河边,不闻爷娘唤女声,但闻黄河流水鸣溅溅。旦辞黄河去,暮至黑山头,不闻爷娘唤女声,但闻燕山胡骑鸣啾啾。\n" +"万里赴戎机,关山度若飞。朔气传金柝,寒光照铁衣。将军百战死,壮士十年归。\n" +"归来见天子,天子坐明堂。策勋十二转,赏赐百千强。可汗问所欲,木兰不用尚书郎,愿驰千里足,送儿还故乡。\n" +"爷娘闻女来,出郭相扶将;阿姊闻妹来,当户理红妆;小弟闻姊来,磨刀霍霍向猪羊。开我东阁门,坐我西阁床,脱我战时袍,著我旧时裳。当窗理云鬓,对镜帖花黄。出门看火伴,火伴皆惊忙:同行十二年,不知木兰是女郎。\n" +"雄兔脚扑朔,雌兔眼迷离;双兔傍地走,安能辨我是雄雌?"))


显示图片:

builder.setStyle(new NotificationCompat.BigPictureStyle().bigPicture(BitmapFactory.decodeResource(getResources(), R.drawable.cat)))


设置重要程度:

builder.setPriority(NotificationCompat.PRIORITY_MAX)

Android 多媒体(一)——使用通知相关推荐

  1. Android 多媒体【转】

    第11章  Android多媒体 通过调用Android的API,可以实现相册,播放器,录音和摄像等功能.这一章需要掌握如下功能: q      多媒体的ContentProvider的调用 q    ...

  2. android多媒体框架学习 详解

    原址 一:多媒体框架概述 jellybean 的多媒体跟以前的版本,通过对比没啥变化,最大的变化是google终于舍得给multimedia建个独立的git了(framework/av),等你好久了! ...

  3. android多媒体框架学习 详解 最新版本

    一:多媒体框架概述 jellybean 的多媒体跟以前的版本,通过对比没啥变化,最大的变化是google终于舍得给multimedia建个独立的git了(framework/av),等你好久了!也体现 ...

  4. Android 多媒体之音频----(日积月累)

    在开发上,习惯的将音频.视频功能的使用称之为多媒体,实际上如果讲的宽泛一些的话,相机的使用,比如拍照,录制视频等,也可以划分到多媒体的范畴里面. 从本节课开始,我们就来看看Android中多媒体的AP ...

  5. Android多媒体MediaStore运用

    MediaScanner与音乐信息扫描 在Android系统中,多媒体库是通过MediaScanner去扫描磁盘文件,对元信息的处理,并通过MediaProvider保存到MediaStore中.下图 ...

  6. [Android多媒体二]调用系统录音机录音并存储到指定位置,适配安卓 7.0

    接上一篇文章,[Android多媒体一]调用系统相机拍照并存储到指定位置,适配安卓 7.0 本文讲述如何调用系统录音机,完成录音后,对录音进行指定位置的保存. 一.开始编写 首先,还是理清一下思路,在 ...

  7. Android 多媒体之音频

    在开发上,习惯的将音频.视频功能的使用称之为多媒体,实际上如果讲的宽泛一些的话,相机的使用,比如拍照,录制视频等,也可以划分到多媒体的范畴里面. 从本节课开始,我们就来看看Android中多媒体的AP ...

  8. Android 多媒体综述

    Android 多媒体综述 多媒体系统是Android中最为庞大的系统,涉及了硬件抽象层.编解码.OpenCore多媒体框架.Android多媒体框架.Java层接口多方面的内容. 一.引言 本系列内 ...

  9. Android多媒体之GLES2战记第四集--移形换影

    视野限制了人对这个宇宙的认知,但没有视野,人将会一无所知 上集说到勇者坠入黑暗之渊,凭借对世界的认知构建出了世界系 到此为止,OpenGL的世界观已经映入脑海,新手十二副本已经通过 接下来等待他们的将 ...

  10. Android多媒体开发

    Android多媒体开发系列文章 Android多媒体开发:录音机 Android多媒体开发:照相机 TextureView+MediaPlayer实现在线短视频播放 Android多媒体开发:第三方 ...

最新文章

  1. 人工智能时代的“认知劳动”
  2. DeepMind详解新WaveNet:比原来快千倍,语音更自然 | 附论文
  3. Welcome Party
  4. 阻塞队列的使用案例-注册成功后增加积分
  5. EOJ_1021_可旋栈
  6. LeetCode每日打卡 - 汉明距离总和
  7. Linux每个目录的介绍
  8. OpenShift 4 概念 - OpenShift是如何通过ImageStream访问Image的
  9. vSphere 6.5 High Availability New Features – Proactive HA(vSphere 6.5高可用性新功能–主动HA)
  10. Google 推荐和鼓励的 13 条代码审查标准,建议收藏!
  11. Dubbo 生态添新兵,Dubbo Admin 发布 v0.1
  12. 判断是否是微信浏览器还是企业微信浏览器
  13. php实现mkv视频播放,mkv怎么合并视频文件
  14. 会计考计算机是WPS还是Ms,wps和office的区别 wps和office哪个好
  15. 新版标准日本语初级_第四十八课
  16. Android app crash的问题排查思路与反思
  17. 一小时学会使用SpringBoot整合阿里云SMS短信服务
  18. 云班课在计算机,云班课电脑版
  19. Full Convert相互转换器,转换数据库格式简单的方法
  20. 自然语言处理实战:机器学习常见工具与技术

热门文章

  1. 解决NVIDIA显卡 GPU显示器不可用的问题(亲测系列)
  2. 关于element ui中autofocus属性失效的问题
  3. 物联网网格:避免物联网碎片化的起点(下)
  4. 记账本——项目概述分析与小组分工
  5. Windows PowerShell 1.0 正式版微软官方下载全地址
  6. 数据库(mysql)sql语句优化
  7. 半个月通过软考高级架构师,分享经验
  8. 职场上个人的核心技术_如何打造个人职场核心竞争力
  9. 绘画新手怎么画好结构素描
  10. 非科班出身学习软件测试可以么