Android Notification简介

本文深入讲解了Android系统中通知的创建、发送、取消流程,包括使用Notification.Builder和NotificationManager类的具体方法。探讨了如何通过PendingIntent定义点击事件,以及通知的提醒方式,如声音、震动和闪烁。此外,还介绍了自定义remoteview的方法和Notification显示等级的概念。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1. 创建通知

使用Notification.Builder来创建,低版本的可以使用NotificationCompat.Builder来替代。

Notification notification = new Notification.Builder(this)
            .setSmallIcon(R.drawable.small)
            .setContentTitle("标题")
            .setContentText("内容")
            .setTicker("您有新消息") // 状态栏显示信息
            .setAutoCancel(true) // 点击自动退出
            .build();

如果调用setLargeIcon()方法,小图标会放在右下角。
在这里插入图片描述

2. 发送和取消通知

NotificationManager是所有通知的管理类,负责发送通知、消除通知等操作。

NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

NotificationManager常用方法

public void notify(int id, Notification notification) // 发送通知
public void cancel(int id) // 取消通知
public void cancelAll() // 取消所有通知

3. 点击事件

通知通过设置PendIntent来定义点击事件。

// 点击事件
Builder.setContentIntent(PendingIntent)
// 删除事件
Builder.setDeleteIntent(PendingIntent)

PendingIntent用于延迟的Intent,主要通过三种方式获取

// 启动Activity
public static PendingIntent getActivity(Context context, int requestCode,
        Intent intent, @Flags int flags)

// 启动Service
public static PendingIntent getService(Context context, int requestCode,
        @NonNull Intent intent, @Flags int flags)

// 发送广播
public static PendingIntent getBroadcast(Context context, int requestCode,
        Intent intent, @Flags int flags)

PendingIntentflags

  • FLAG_CANCEL_CURRENT:如果当前系统中已经存在相同的PendingIntent对象,那么取消原有的,重新生成一个PendingIntent对象。
  • FLAG_NO_CREATE:如果当前系统中不存在相同的PendingIntent对象,系统将不会创建该PendingIntent对象而是直接返回null
  • FLAG_ONE_SHOT:该PendingIntent只作用一次。在该PendingIntent对象通过send()方法触发过后,PendingIntent将自动调用cancel()进行销毁,那么如果你再调用send()方法的话,系统将会返回SendIntentException
  • FLAG_UPDATE_CURRENT:如果系统中有一个和你描述的PendingIntent对等的PendingInent,那么系统将使用该PendingIntent对象,但是会使用新的Intent来更新之前PendingIntent中的Intent对象数据,例如更新Intent中的Extras

判断两个相同的PendingIntent对象,主要是依据requestCodeintent.filterEquals()是否相同。

Intent intentA = new Intent();
intentA.putExtra("paramA", "paramA");
Intent intentB = new Intent();
intentB.putExtra("paramB", "paramB");
// intentA.filterEquals(intentB) 返回true;

4. 通知的提醒方式

  • 声音提醒
    // 默认
    notification.defaults |= Notification.DEFAULT_SOUND;
    
    // 指定声音
    notification.sound = Uri.parse("file:///xxxx");
    
  • 震动提醒
    // 默认
    notification.defaults |= Notification.DEFAULT_VIBRATE;
    
    // 指定震动效果
    long[] vibrate = {0, 200, 300, 400};
    notification.vibrate = vibrate;
    
  • 闪烁提醒
    // 默认
    notification.defaults |= Notification.DEFAULT_LIGHTS;
    
    notification.ledARGB = 0xff00ff00; // 灯的颜色
    notification.ledOnMS = 300; // 灯显示的毫秒数,300毫秒
    notification.ledOffMS = 1000; // 灯关闭的毫秒数,1000毫秒
    notification.flags |= Notification.FLAG_SHOW_LIGHTS; // 必须加上这个标志
    

5. 自定义remoteview

使用RemoteViews来自定义通知。

RemoteViews remoteViews = new RemoteViews("com.blog.demo",
        R.layout.layout_remote_view_notification);
remoteViews.setTextViewText(R.id.tv_name, "Jack");
remoteViews.setTextViewText(R.id.tv_address, "BeiJing");
notification.contentView = remoteViews;

布局文件layout_remote_view_notification.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="40dp"
    android:background="#ffa6a5aa">
    <TextView
        android:id="@+id/tv_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_alignParentLeft="true"
        android:layout_marginLeft="15dp"
        android:textSize="16sp"
        android:textColor="#ffffffff" />
    <TextView
        android:id="@+id/tv_address"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_alignParentRight="true"
        android:layout_marginRight="15dp"
        android:textSize="16sp"
        android:textColor="#ffcccccc" />
</RelativeLayout>

效果如下
在这里插入图片描述

6. Notification显示等级

Android5.0加入了Notification显示等级

notification.visibility = Notification.VISIBILITY_PRIVATE;
  • VISIBILITY_PUBLIC,在任何情况都会显示通知
  • VISIBILITY_PRIVATE,只有在没有锁屏时才会显示通知
  • VISIBILITY_SECRET,在安全锁和没有锁屏的情况下才能够显示通知
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值