通知——Notification

本文深入解析Android界面提醒的三种方式:Toast、Dialog和Notification,包括它们的实现原理、适用场景和代码示例。针对不同版本Android系统(16及以上和16以下),提供兼容性解决方案。

一、界面提醒概述

界面交互,在用户体验设计中是很重要的一环。在Android界面提醒有如下三种方式:

  • Toast
  • Dialog
  • notification -通知

二、Notification概述

通知是运行在一个叫做 com.android.systemui的进程里面。通知要使用过程中分高版本(16版本以上)、低版本(16版本以下),两个版本有不同实现。其中高版本兼容低版本。

  • 高版本
//1.先拿到通知管理器
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
//下面要求是11版本以上的
Notification noti = new Notification.Builder(this)
.setContentTitle("标题——呵呵")
.setContentText("内容—哈哈哈")
.setSmallIcon(R.drawable.ic_launcher)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher))//这句要求API16以上
.build();

manager.notify(1, noti);
  • 低版本
//1.先拿到通知管理器
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Intent intent = new Intent();
intent.setAction(Intent.ACTION_CALL);
intent.setData(Uri.parse("tel://10086"));
//1.拿到PendingIntent
PendingIntent contentIntent=PendingIntent.getActivity(this, 1, intent, 0);
Notification noti2 = new Notification(R.drawable.ic_launcher, "你有一条未读短信", System.currentTimeMillis());

noti2.setLatestEventInfo(this, "标题——哈哈", "内容—呵呵", contentIntent);
manager.notify(1, noti2);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值