第三单元总结

本文详细介绍了Android中的Notification机制,包括为何使用Notification来展示系统消息,如何设置通知的内容、时间、图标以及自动取消功能。此外,还讨论了如何创建通知分组以优化用户体验,控制锁屏通知的可见性,以及如何构建带有进度条和自定义布局的通知,如大图通知和列表样式通知。
摘要由CSDN通过智能技术生成

Notitfcation 通知

1:

普通通知

为什么使用Notifcation

为了能够在系统的通知栏中显示系统或程序发出的消息,Android引入Notification

定义:是在系统的通知栏中呈现多样式持久性消息的类。

Notifcation的作用

显示客户端的推送消息(如有新版本发布、广告、推荐新闻等)

显示正在进行的事物(如音乐播放器、版本更新时候的下载进度等)

显示接收到短消息,即时消息等信息(如QQ、微信、新浪、短信)

Notifcation的方法

setContentTitle():设置标题

setContentText():设置内容

setWhen():设置通知的时间

setSmallIcon():设置通知的小图标

setLargeIcon() :设置通知的大图标

setAutoCancel():自动删除通知

如何使用Notifcation

获取通知管理器(招聘讲师)

实例化通知的对象(招募学生)

设置通知的属性(学生信息)

通知管理器发送通知(讲师发号施令)

NotificationManager nm= getSystemService(NOTIFICATION_SERVICE);

底层实现:

参数介绍:

NOTIFICATION_SERVICE:获取系统通知的参数

实例化通知的对象

Notification.Builder notificationBuilder= new Notification.Builder(this);

设置属性

notificationBuilder.setSmallIcon(R.mipmap.image)

notificationBuilder.setContentTitle("10086")

notificationBuilder.setContentText("充值成功,余额30.00元!");

notificationBuilder.setAutoCancel(true);

显示通知

Notification notification= notificationBuilder.build();

nm.notify( 1,notification);

核心代码

private void sendNotification() {

//创建构造者

Notification.Builder builder = new Notification.Builder(this);

//设置属性 setSamllIcon该属性必须设置

builder.setSmallIcon(R.mipmap.ic_launcher); //必须设置

builder.setContentTitle("我是标题"); //建议设置

builder.setContentText("我是内容"); //建议设置

// builder.setTicker("我是提示信息");

// builder.setContentInfo("我是附加信息"); //7.0以后已经过期

//创建对象.发送的就是这个对象

Notification build = builder.build();

//获取通知管理器,负责发通知、清除通知等

NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

//TODO :发送通知

//参数一 id 通知的id(稍后介绍意义) 参数二 通知对象

notificationManager.notify(1,build);

}

}

2:

通知分组

一个APP可能短时间之内可能会推送很多条消息给用户,占用大量空间,消息都堆在一起,也大大降低了用户的体验,所以使用分组将同类型的消息统一管理。

获取通知管理器

NotificationManager mNotifacationManager = getSystemService(NOTIFICATION_SERVICE);

创建多个通知

NotificationCompat.Builder mBuilder1 = new NotificationCompat.Builder(this);

NotificationCompat.Builder mBuilder2 = new NotificationCompat.Builder(this);

设置分组

mBuilder1.setWhen(System.currentTimeMillis())

.setContentTitle("first content title")

.setContentText("first content text")

.setSmallIcon(R.mipmap.ic_launcher)

.setGroup("a");

mBuilder1.setWhen(System.currentTimeMillis())

...

.setGroup("b");

发送通知

mNotifacationManager.notify(1, mBuilder1.build());

mNotifacationManager.notify(2, mBuilder2.build());

锁屏通知

Android 5.0(API level 21)开始,通知可以显示在锁屏上,通过设置选择是否允许敏感的通知内容显示在安全的锁屏上。

setVisibility() 方法共有三个选值:

1.VISIBILITY_PRIVATE : 显示基本信息,如通知的图标,但隐藏通知的全部内容;

2.VISIBILITY_PUBLIC : 显示通知的全部内容;

3.VISIBILITY_SECRET : 不显示任何内容,包括图标。

build的时候调用 setVisibility() 方法设置即可

例:.setVisibility(VISIBILITY_PUBLIC).build();

3:

进度条通知

获取通知管理器

NotificationManager nm= getSystemService(NOTIFICATION_SERVICE);

创建通知

NotificationCompat.Builder builder= new NotificationCompat.Builder(this);

builder.setContentText("安装中...");

builder.setProgress(0,0,true);

开启线程更新通知

new Thread(new Runnable() {

public void run() {

for(int i=0;i<100;i++){

builder.setProgress(100,i,false);

nm.notify(3,builder.build());

}

4:

自定义通知

创建自定义通知

private void userNotification() {

Notification.Builder builder = new Notification.Builder(this);

builder.setSmallIcon(R.mipmap.ic_launcher);

builder.setContentText("内容");

builder.setContentTitle("头部");

/**

* RemoteViews是可以在别的进程(系统进程)中显示的View,并且提供了一组跨进程更新它界面的操作

* 两个参数,第一个布局所在包名

* 第二个是布局Id

* 布局文件是自己创建的,随便一个线性布局,加一个textView和ImageView即可

*/

RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.simple_layout);

}

大图通知

private void notificationStyle() {

NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

Notification.Builder builder = new Notification.Builder(this);

builder.setSmallIcon(R.mipmap.ic_launcher);

builder.setContentTitle("列表通知");

// builder.setContentTitle("大图通知");

//通知内容为大图片

Notification.BigPictureStyle bigPictureStyle = new Notification.BigPictureStyle();

bigPictureStyle.bigPicture(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher));

//通知内容为列表显示

Notification.InboxStyle inboxStyle = new Notification.InboxStyle();

inboxStyle.addLine("李白");

inboxStyle.addLine("猴子");

inboxStyle.addLine("露娜");

builder.setStyle(inboxStyle);

//不能跨APP

Intent intent = new Intent(this, MainActivity.class);

//intent - PendingIntent

PendingIntent intent1 = PendingIntent.getActivity(this, 10, intent, PendingIntent.FLAG_ONE_SHOT);

builder.setFullScreenIntent(intent1, true);

builder.setContentIntent(intent1);

manager.notify(9, builder.build());

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值