Android-Notification(通知)

Notification(通知)

Notification 概述
是一种具有全局效果的通知,可以在系统的通知栏中显示,首先会表示为一个图标的形式,当用户向下滑动的时候,展示出通知具体的内容

Notification 的基本操作
Notification 的基本操作主要有创建、更新、取消这三种。一个 Notification 的必要属性有三项,如果不设置则在运行时会抛出异常:

小图标,通过 setSmallIcon() 方法设置
标题,通过 setContentTitle() 方法设置
内容,通过 setContentText() 方法设置

创建 Notification
Notification : 通知对应类,保存通知相关的数据。
NotificationManager : NotificationManager 是通知管理类,它是一个系统服务。调用 NotificationManager 的 notify() 方法可以向系统发送通知。

获取 NotificationManager 对象:
NotificationManager mNotifyManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

代码:
private void sendNotification() {
//获取NotificationManager实例
NotificationManager notifyManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
//实例化NotificationCompat.Builde并设置相关属性
NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
//设置小图标
.setSmallIcon(R.mipmap.icon_fab_repair)
//设置通知标题
.setContentTitle(“最简单的Notification”)
//设置通知内容
.setContentText(“只有小图标、标题、内容”)
//设置通知时间,默认为系统发出通知的时间,通常不用设置
//.setWhen(System.currentTimeMillis());
//通过builder.build()方法生成Notification对象,并发送通知,id=1
notifyManager.notify(1, builder.build());
}

给 Notification 设置 Action
otification 跳转到 MainActivity 的效果,代码如下:
/**

  • 发送一个点击跳转到MainActivity的消息
    */
    private void sendSimplestNotificationWithAction() {
    //获取PendingIntent
    Intent mainIntent = new Intent(this, MainActivity.class);
    PendingIntent mainPendingIntent = PendingIntent.getActivity(this, 0, mainIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    //创建 Notification.Builder 对象
    NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
    .setSmallIcon(R.mipmap.ic_launcher)
    //点击通知后自动清除
    .setAutoCancel(true)
    .setContentTitle(“我是带Action的Notification”)
    .setContentText(“点我会打开MainActivity”)
    .setContentIntent(mainPendingIntent);
    //发送通知
    mNotifyManager.notify(3, builder.build());
    }

发送具有 Action 的通知多了创建 Intent 、 PendingIntent 和 setContentIntent()

PendingIntent 是一种特殊的 Intent ,字面意思可以解释为延迟的 Intent ,用于在某个事件结束后执行特定的 Action

PendingIntent 主要可以通过以下三种方式获取:
//获取一个用于启动 Activity 的 PendingIntent 对象
public static PendingIntent getActivity(Context context, int requestCode, Intent intent, int flags);

//获取一个用于启动 Service 的 PendingIntent 对象
public static PendingIntent getService(Context context, int requestCode, Intent intent, int flags);

//获取一个用于向 BroadcastReceiver 广播的 PendingIntent 对象
public static PendingIntent getBroadcast(Context context, int requestCode, Intent intent, int flags)

PendingIntent 具有以下几种 flag:
FLAG_CANCEL_CURRENT:如果当前系统中已经存在一个相同的 PendingIntent 对象,那么就将先将已有的 PendingIntent 取消,然后重新生成一个 PendingIntent 对象。

FLAG_NO_CREATE:如果当前系统中不存在相同的 PendingIntent 对象,系统将不会创建该 PendingIntent 对象而是直接返回 null 。

FLAG_ONE_SHOT:该 PendingI

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值