Android通知的使用及设置
Android使用通知
- 一、通知的简介
- 二、通知的用法
- 1.通知的基本用法
- 2.通知的进阶用法
- 3.通知的高级用法
一、通知的简介
通知(Notification)是Android系统中比较有特色的一个功能,当某个应用程序希望向用户发出提示信息,而该应用程序又不在前台程序运行时,就可以借助通知来实现。发出一条通知后,手机最上方的状态栏中会显示一个通知的图标,下拉菜单状态栏后可以看到通知的详细内容。Android的通知功能获得了大量用户的认可和喜爱,就连IOS 5.0以后也加入了相关的功能。
二、通知的用法
1.通知的基本用法
无论是在哪里创建通知,整体的步骤都是相同的,下面我们来学习一下创建通知的详细步骤。
1)首先需要一个 NotificationManager 来对通知进行管理,可以调用Context的 getSystemService() 方法来获取到。getSystemService()方法接收一个字符串参数用于确定获取系统的哪个服务,这里我们传入 Context.NOTIFICATION_SERVICE 即可。因此获取NotificationManager的实例就可以写成:
NotificationManager manager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
2)接下来需要使用一个Builder构造器来创建Notification对象。support-v4 库中提供了一个NotificationCompat 类,使用这个类的构造器来创建Notification对象,就可以保证我们的程序在所有Android系统版本上都能正常工作,代码如下所示:
Notification notification = new NotificationCompat.Builder(context).setContentTitle("This is content title").setContentText("This is content text").setWhen(System.currentTimeMillis()).setSmallIcon(R.drawable.small_icon).setLargeIcon(BitmapFactory.decorateResource(getResources(),R.mipmap.ic_laucher)).build();
方法名 | 功能 |
---|---|
setContentTitle | 指定通知的标题内容 |
setContentText | 指定通知的正文内容 |
setWhen | 指定通知被创建的时间 |
setSmallIcon | 设置通知的小图标 |
setLargeIcon | 设置通知的大图标 |
bulid | 创建通知 |
以上工作完成后,只需要调用NotificationManager的**notify()**方法就可以让通知显示出来。
notify():接收两个参数
第一个参数是 id,保证每个通知的 id 都是不同的
第二个参数是 Notification对象,直接将上面创建的Notification对象传入即可
因此,显示一个通知就可以写成:
manager.notify(1,notification);
具体实现:
新建一个NotificationTest项目,并修改activity_main.xml中的代码,如下所示:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical"android:layout_width="match_parent"android:layout_height="match_parent"><Buttonandroid:id="@+id/send_notice"android:text="Send Notice"android:layout_width="wrap_content"android:layout_height="wrap_content" /></LinearLayout>
现在我们来优化一下NotificationTest项目,给刚才的通知加上点击功能,让用户点击它的时候可以启动另一个活动。我们创建notification_layout.xml:
<?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="This is notification layout"/></RelativeLayout>
这样就把NotificationActivity这个活动准备好了,下面我们修改MainActivity中的内容,给通知加入点击功能,如下:
Intent intent = new Intent(this,NotificationActivity.class);
PendingIntent pi = PendingIntent.getActivity(this,0,intent,0);
然后在NotificationCompat.Builder的后面加上 setContentIntent(pi);
最后,我们再来实现通知取消的功能。方法有两种:
一是NotificationCompat.Builder后在连缀一个 setAutoCancel() 方法
二是显式调用 NotificationManager的 cancel() 方法将它取消:
NotificationManager manager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);manager.cancel();
2.通知的进阶用法
继续观察NotificationCompat.Builder这个类,可以发现有许多API我们没有用到,接下来列举几种常用的通知API。
方法 | 功能 |
---|---|
setSound | 设置通知的声音提示 |
setVibrate | 设置通知时手机的震动 |
setLights | 设置手机LED灯闪烁 |
setDefault | 设置通知的默认样式 |
setVibrate 手机震动需要在AndroidManifest.xml文件当中加入如下声明:
<uses-permission android:name="android.permission.VIBRATE"/>
3.通知的高级用法
(一)当我们使用 setContentText 时候,会发现当出现长文字,导致在通知栏当中文本内容被省略了,此时可以用到 setStyle() 的方法:
...
.setStyle(new NotificationCompat.BigTextStyle().bigText("...."))
.build();
而在通知栏当中加入大图片可以用类似的方法:
...
.setStyle(new NotificationCompat.BigPictureStyle().bigPicture(BitmapFactory.decorateResource(getResources(),R.drawable.big_image)))
.build();
以上就是 setStyle() 方法的重要内容了
(二)setPriority() 方法可以设置通知的重要程度。
setPriority() 接收一个参数来设置这条通知的重要程度,分别为以下五种:
参数 | 功能 |
---|---|
PRIORITY_DEFAULT | 默认的重要程度,和不设置是一样的 |
PRIORITY_MIN | 最低的重要度 |
PRIORITY_LOW | 较低的重要度 |
PRIORITY_HIGH | 较高的重要度 |
PRIORITY_MAX | 最高的重要度 |
设置的代码如下所示:
...
.sePriority(NotificationCompat.PRIORITY_MAX )
.build();
以上便是Android系统通知的设置方法。
参考:
《第一行代码-Android(第2版)》
原文链接:https://blog.csdn.net/BarongDog/article/details/90709116
Android通知的使用及设置相关推荐
- android10代码开启横幅通知,Android通知以编程方式启用横幅设置
是否有任何方法可以通过编程方式在应用程序通知中打开横幅设置(显示在状态栏顶部)? // send notification to NotificationManager Notification.Bu ...
- android 获取权限管理,Android常用权限获取和设置
Android常用权限获取和设置 1 活动管理器 权限 代码 ActivityManager activityManager = (ActivityManager) getSystemService( ...
- 20_Android中apk安装器,通过WebView来load进一个页面,Android通知,程序退出自动杀死进程,通过输入包名的方式杀死进程
场景:实现安装一个apk应用程序的过程.界面如下: 编写如下应用,应用结构如下: <RelativeLayout xmlns:android="http://schemas.an ...
- android activity 被notification启动,Android通知Notification全面剖析
原标题:Android通知Notification全面剖析 通知 通知是您可以在应用的常规 UI 外部向用户显示的消息.当您告知系统发出通知时,它将先以图标的形式显示在通知区域中.用户可以打开抽屉式通 ...
- xamarin android 通知,在 Xamarin.Android 中使用 Notification.Builder 构建通知
0 背景 在 Android 4.0 以后,系统支持一种更先进的 Notification.Builder 类来发送通知.但 Xamarin 文档含糊其辞,多方搜索无果,遂决定自己摸索. 之前的代码: ...
- Android通知频道,通知点
In this tutorial, we'll be looking into how the introduction of Android Oreo has brought a drastic c ...
- Android通知,PendingIntent示例
Welcome to Android Notification Example using android PendingIntent. In this tutorial we're going to ...
- android通知悬浮通知_Android通知直接回覆
android通知悬浮通知 Android Notification Direct Reply action lets us reply to the notification message, it ...
- android 通知静音_如何使电话静音(但不包括短信和通知)
android 通知静音 If you don't want to hear your phone ring, but do want to hear text messages and other ...
- Android 系统属性读取和设置详解
Android 系统属性读取和设置详解 一.在adb中进行属性读取和设置 1.Settings Provider设置和读取 获取 设置 2.SystemProperties属性读取和设置 二.Andr ...
最新文章
- netsh命令修改ip
- 计算机文件无法显示后缀,但一般情况下电脑默认是看不到文件格式显示(也就是文件扩展名)...
- select标签中的选项分组
- 程序员最讨厌的100件事,瞬间笑喷了,哈哈~~
- uva 1605 ——Building for UN
- 第十三届蓝桥杯青少年STEMA(2021.08-2021.03)C++
- php会员可见内容代码,DedeCMS内容隐藏指定字段仅对会员显示对游客不可见的实现方法...
- 前景检测算法(十四)--SuBSENSE算法
- VR 、AR 谁让你眼前一亮
- 2010年的20款游戏
- C++学习总结(1)
- Java实现端口扫描器
- 前端UI框架网址大全----后续会有添加
- glassfish简单介绍
- PAT 1066. Root of AVL Tree (25) 回レ!雪月AVL
- java double丢失精度问题,加减乘除计算出错出现99999
- Android监听Home键
- CGAN(条件生成-对抗网络)简述教程
- 【测绘程序设计试题集】 试题04 最短路径计算
- 互联网金融的信息安全(一)新环境的安全形势