Android学习|控件——Notification通知
Android学习|控件——Notification通知
- 一、前提
- 二、两个对象的的构建
- 1、创建NotificationManager
- 2、使用Builder构造器来创建Notification
- 2.1、NotificationChannel
- 3、创建两个对象完整代码
- 4、Notifition常用方法
- 三、实现一个简单的通知
- 四、实现点击后跳转到app的通知
一、前提
实现通知,要创建两个对象 ----- Notification、NotificationManager进行操作。
二、两个对象的的构建
1、创建NotificationManager
NotificationManager类是一个通知管理器类,这个对象是由系统维护的服务,是以单例模式的方式获得,所以一般并不直接实例化这个对象。
在Activity中,可以使用Activity.getSystemService(String)方法获取NotificationManager对象,Activity.getSystemService(String)方法可以通过Android系统级服务的句柄,返回对应的对象。
在这里需要返回NotificationManager,所以直接传递Context.NOTIFICATION SERVICE即可。
manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
2、使用Builder构造器来创建Notification
使用Notificationcompat类的Bulder构造器来创建Notification对象,可以保证程序在所有的版本上都能正常工作。
Android8.0新增了通知渠道这个概念,如果没有设置,则通知无法在Android8.0的机器上显示。
Notification notification = new NotificationCompat.Builder(this,“leo”).build();
源码可以看出:NotificationCompat.Builder()构造器中包含两个参数,一个是Context通知,一个是channelId,而channelId是android8.0引入的NotificationChannel通知渠道。
所以这块需要进行版本判断,并创建NotificationChannel对象,同时NotificationChannel构造中的id要与Notification中的channelId保持一致,importance为通知类型,下面有介绍。
(下图为NotificationChannel构造函数)。
2.1、NotificationChannel
通知渠道:Android 8.0引入了通知渠道,其允许您为要显示的每种通知类型创建用户可自定义的渠道。
通知重要程度设置,NotificationManager类中:
IMPORTANCE_NONE:关闭通知
IMPORTANCE_MIN:开启通知,不会弹出,但没有提示音,状态栏中无显示
IMPORTANCE_Low:开启通知,不会弹出,不发出提示音,状态栏中显示
IMPORTANCE_DEFAULT:开启通知,不会弹出,发出提示音,状态栏中显示
IMPORTANCE_HIGH:开启通知,会弹出,发出提示音,状态栏中显示
引用:NotificationManager.IMPORTANCE_HIGH
new NotificationChannel(“leo”, “测试通知”, NotificationManager.IMPORTANCE_HIGH);
3、创建两个对象完整代码
manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);//版本大于等于 安卓8.0if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){ NotificationChannel channel = new NotificationChannel("leo", "测试通知", NotificationManager.IMPORTANCE_HIGH);manager.createNotificationChannel(channel);}Notification notification = new NotificationCompat.Builder(this,"leo").build();
4、Notifition常用方法
1、setContentitle(string string):设置标题
2、setContentText(String string):设置文本内容
3、setSmallion(int icon):设置小图标
4、setLargelcon(Bitmap icon):设置通知的大图标
5、setColor(int argb):设置小图标的颜色
6、setContentintent(Pendingintent intent):设置点击通知后的跳转意图
7、setAutoCancellboolean boolean):设置点击通知后自动清除通知
8、setWhen(long when):设置通知被创建的时间
以上方法使用需要在使用构造器、创建时候build()构造前设置
注意点
Android从5.0系统开始,对于通知栏图标的设计进行了修改。
现在Google要求,所有应用程序的通知栏图标,应该只使用alpha图层来进行绘制,而不应该包括RGB图层(总结:图片不能为rgb有色图)。
notification = new NotificationCompat.Builder(this,"leo").setContentTitle("重大消息!!!") //设置标题.setContentText("世界那么大,你想去看看嘛") //设置通知文字.setSmallIcon(R.drawable.ic_baseline_sms_24) //设置左边的小图标.setLargeIcon(BitmapFactory.decodeResource(getResources(),R.drawable.img1)) //设置大图标.setColor(Color.parseColor("#ffff0000")).setContentIntent(pendingIntent) // 设置点击通知之后 进入相关页面(此处进入NotificationActivity类,执行oncreat方法打印日志).setAutoCancel(true) //设置点击通知后 通知通知栏不显示.build();
三、实现一个简单的通知
package com.example.mynotification;import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.NotificationCompat;import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.os.Build;
import android.os.Bundle;
import android.view.View;public class MainActivity extends AppCompatActivity {private NotificationManager manager;private Notification notification;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){ //版本大于等于 安卓8.0NotificationChannel channel = new NotificationChannel("leo", "测试通知", NotificationManager.IMPORTANCE_HIGH);manager.createNotificationChannel(channel);}notification = new NotificationCompat.Builder(this,"leo").setContentTitle("重大消息!!!") //设置标题.setContentText("世界那么大,你想去看看嘛") //设置通知文字.setSmallIcon(R.drawable.ic_baseline_sms_24) //设置左边的小图标.setLargeIcon(BitmapFactory.decodeResource(getResources(),R.drawable.img1)) //设置大图标.build();}//开启通知按钮 点击事件public void setNotification(View view) {manager.notify(1,notification); //点击事件启动唤醒manager,用于通知notification}//关闭通知按钮 点击事件public void cacelNotification(View view) {}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"><Buttonandroid:layout_width="200dp"android:layout_height="80dp"android:text="开启通知"android:onClick="setNotification"></Button><Buttonandroid:layout_width="200dp"android:layout_height="80dp"android:text="关闭通知"android:onClick="cacelNotification"></Button></LinearLayout>
四、实现点击后跳转到app的通知
主要通过构造Notification时setContentintent(Pendingintent intent):设置点击通知后的跳转意图
这块为了方便起见,只创建一个类,让其在控制台打印日志
代码如下:
1、主类MainActivity如下
package com.example.mynotification;import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.NotificationCompat;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.view.View;public class MainActivity extends AppCompatActivity {private NotificationManager manager;private Notification notification;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){ //版本大于等于 安卓8.0NotificationChannel channel = new NotificationChannel("leo", "测试通知", NotificationManager.IMPORTANCE_HIGH);manager.createNotificationChannel(channel);}// 设置取消后的动作Intent intent = new Intent(this, NotificationActivity.class);PendingIntent pendingIntent = PendingIntent.getActivity(this,0,intent,0);//初始化 notificationnotification = new NotificationCompat.Builder(this,"leo").setContentTitle("重大消息!!!") //设置标题.setContentText("世界那么大,你想去看看嘛") //设置通知文字.setSmallIcon(R.drawable.ic_baseline_sms_24) //设置左边的小图标.setLargeIcon(BitmapFactory.decodeResource(getResources(),R.drawable.img1)) //设置大图标.setColor(Color.parseColor("#ffff0000")).setContentIntent(pendingIntent) // 设置点击通知之后 进入相关页面(此处进入NotificationActivity类,执行oncreat方法打印日志).setAutoCancel(true) //设置点击通知后 通知通知栏不显示.build();}//开启通知按钮 点击事件public void setNotification(View view) {manager.notify(1,notification); //点击事件启动唤醒manager,用于通知notification}//关闭通知按钮 点击事件public void cacelNotification(View view) {manager.cancel(1); //取消通知}
}
2、创建的打印日志的类如下
package com.example.mynotification;import android.app.Activity;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.util.Log;
import androidx.annotation.Nullable;public class NotificationActivity extends Activity {@Overrideprotected void onCreate(@Nullable Bundle savedInstanceState) {super.onCreate(savedInstanceState);Log.e("leo", "进入了NotificationActivity");}
}
3、xml如下
<?xml version="1.0" encoding="utf-8"?>
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"><Buttonandroid:layout_width="200dp"android:layout_height="80dp"android:text="开启通知"android:onClick="setNotification"></Button><Buttonandroid:layout_width="200dp"android:layout_height="80dp"android:text="关闭通知"android:onClick="cacelNotification"></Button></LinearLayout>
效果如下
点击后进入空白的activity,控制台打印日志
ps::通知、跳转什么的一些正常,但是就是控制台没有打印输出,这块可能是没转创建的NotificationActivity.java的类型。解决如下:
点击类名NotificationActivity、然后选左边的灯泡,然后选第一个Add Activity to manifest。
点完后可以看到,java文件左边应该是转成了xml
Android学习|控件——Notification通知相关推荐
- Android Studio 控件
学习视频:https://www.bilibili.com/video/BV1Jb4y187C4?p=7&spm_id_from=pageDriver 介绍 首先将工程make一下 下载需要讲 ...
- Android提供了哪些主要UI控件,Android必备:Android UI控件的了解与学习
因为工做须要,最近一段时间,须要进行Android App开发的学习,以前简单的进行过Android的了解,对于基本的Android环境的搭建等已经有过整理,一个Android App是由一个或多个A ...
- Android学习--02(猜猜我的星座App源码+Android常用控件TextView+EditText+Button+ImangeView+DatePicker+App间通信+跳转页面)
猜猜我的星座App 1 Android常用控件 1.1 TextView控件 1.1.1 简介 1.1.2属性 1.1.3 扩展属性 1.1.4 TextView的使用方法 1.1.5总结 1.2 E ...
- Android 开源控件与常用开发框架开发工具类
Android的加载动画AVLoadingIndicatorView 项目地址: https://github.com/81813780/AVLoadingIndicatorView 首先,在 bui ...
- Android神奇“控件”-----RemoteViews
本篇文章已授权微信公众号 guolin_blog (郭霖)独家发布 转载请注明 http://blog.csdn.net/wrg_20100512/article/details/53940485 好 ...
- Android开源控件ViewPager Indicator的使用方法
1月16日厦门 OSC 源创会火热报名中,奖品多多哦 摘要 Android开源控件ViewPager Indicator的使用介绍 ViewPagerIndicator 目录[-] 1. V ...
- 日历控件的android代码,Android日历控件PickTime代码实例
Android日历控件PickTime代码实例 发布时间:2020-10-03 16:05:51 来源:脚本之家 阅读:86 作者:手撕高达的村长 最近做项目,需要设置用户的生日,所以做这样一个功能. ...
- android控件使用大全,Android常见控件使用详解
本文实例为大家分享了六种Android常见控件的使用方法,供大家参考,具体内容如下 1.TextView 主要用于界面上显示一段文本信息 2.Button 用于和用户交互的一个按钮控件 //为Butt ...
- android组件用法说明,Android第三方控件PhotoView使用方法详解
Android第三方控件PhotoView使用方法详解 发布时间:2020-10-21 15:06:09 来源:脚本之家 阅读:74 作者:zhaihaohao1 PhotoView的简介: 这是一个 ...
最新文章
- 河南工程学院计算机科学与技术在哪个校区,河南工学院新生在哪个校区_有几个校区及地址介绍...
- 动机模型_解读冰山模型:强烈的动机是成功的开始
- poj 1256 Anagram—next_permutation的神奇应用
- Nginx 实现网站 http、https 配置
- 亚马逊低调收购Biba 或下月发布视频消息服务
- 修改对象的某个属性的值_如何理解Python中的面向对象编程?
- 360导航源码php,仿360网址导航源码v3.0
- 深度学习之MNIST数据集
- USB免驱NFC读写器 Android系统中NFC读写范例
- 从二维码图片到镂空二维码stl模型(1)
- 牛腩新闻发布--触发器语句
- Android 获取系统语言,区分简繁体中文
- 帝国php改密码后登录不进去,帝国cms后台不能登录的解决方法
- Gradle慢需要6min 现在20s解决了。
- 面向物流行业的文档管理系统
- reviewer中文_中文审稿意见怎么写
- 音视频播放器—变速播放
- php保留两位小叔_PHP价格格式化,保留两位小数
- RNA-seq全流程分析
- 心电图ECG常用滤波器之陷波器