Android NotificationManager

本文深入探讨了Android系统中Notification的工作原理,包括其架构、时序及相关类的设计,旨在帮助开发者更好地理解Notification如何呈现给用户,并介绍了一些处理Notification的方法。

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

这篇文章不是教你如何创建不同的Notification样式,不是教你处理Notification中的某些问题,也不会列出创建Notification的某些demo。相反,这里只是给出Notification中的某些实现原理,用于我们更加方便的了解Notification是如何工作的。同时这里所指的Notification是广义的Notification;即就是Android 通过Notification机制呈现给用户的某些特定的状态
这里以Settings–>Sound & Notification –>Interruptions为例说明Notification的一些处理方法。

Notification的架构图

通常我们了解某一功能是如何运作的;准确、及时的找到其入口是关键。设置相关的入口如下图所示;

  1. 系统启动时进行的初始化动作
  2. 在Settings中的ZenMode*的UI接口

那么问题来了:为何手机reboot之后,上次设置的某些Notification策略在当前状态下还是可以被应用的?
答案只有一个:策略是保存在手机中的,不是存在于内存中的
/data/system/notificatin_policy.xml
Notification Arc

Notification的时序图

时序图

Notification相关的类图

UML

综上

针对 时序图 & 相关类图 在这里不做过多的解释。仅仅凭借这篇博文想要去了解某些机制还是远远不够的。希望这里能起到一个抛砖引玉、一个入口的的作用。

<think>我们正在讨论Android中的NotificationManager使用方法。根据用户的问题,需要提供NotificationManager的使用方法和示例代码。同时,我们需要参考站内提供的引用内容,但注意这些引用内容可能不是完全匹配,但可以借鉴。 用户需求:在Android中使用NotificationManager,包括使用方法和示例代码。 参考引用: [^1]: 展示了一个NotificationView的Activity示例,其中取消了通知。 [^2]: 展示了如何自定义Notification布局,使用RemoteViews。 我们将按照以下步骤组织回答: 1. 介绍NotificationManager的基本作用。 2. 创建通知的步骤(包括构建Notification对象、设置通知属性等)。 3. 发送通知。 4. 取消通知。 5. 自定义通知布局(参考引用[^2])。 6. 示例代码整合。 注意:由于Android版本差异,不同版本创建通知的方式可能不同,这里我们以较新的方式(使用NotificationCompat.Builder)来构建,以保证兼容性。 回答结构: - 首先介绍NotificationManager - 分步骤说明使用方法 - 提供完整示例代码 - 相关问题 注意:在代码示例中,我们将使用必要的导入,并尽量注释说明。</think>### Android NotificationManager 使用指南 NotificationManagerAndroid 系统中管理通知的核心组件,用于向用户显示系统级提醒。以下是详细使用方法和示例代码: #### 一、基础使用步骤 1. **创建通知渠道** (Android 8.0+必需) 2. **构建通知对象** 使用 `NotificationCompat.Builder` 3. **获取 NotificationManager 实例** 4. **发送通知** 调用 `notify()` 方法 5. **取消通知** 调用 `cancel()` 方法 #### 二、完整示例代码 ```java // 导入必备类 import android.app.NotificationChannel; import android.app.NotificationManager; import android.app.PendingIntent; import android.content.Context; import android.content.Intent; import android.os.Build; import androidx.core.app.NotificationCompat; public class NotificationUtils { // 创建通知渠道 (Android 8.0+) private static void createNotificationChannel(Context context) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { CharSequence name = "我的消息通道"; String description = "重要通知通道"; int importance = NotificationManager.IMPORTANCE_DEFAULT; NotificationChannel channel = new NotificationChannel("CHANNEL_ID", name, importance); channel.setDescription(description); NotificationManager nm = context.getSystemService(NotificationManager.class); nm.createNotificationChannel(channel); } } // 发送基础通知 public static void sendBasicNotification(Context context, String title, String content) { createNotificationChannel(context); // 1. 构建点击意图 Intent intent = new Intent(context, MainActivity.class); PendingIntent pendingIntent = PendingIntent.getActivity( context, 0, intent, PendingIntent.FLAG_IMMUTABLE ); // 2. 构建通知 NotificationCompat.Builder builder = new NotificationCompat.Builder(context, "CHANNEL_ID") .setSmallIcon(R.drawable.ic_notification) // 必需的小图标 .setContentTitle(title) .setContentText(content) .setPriority(NotificationCompat.PRIORITY_DEFAULT) .setContentIntent(pendingIntent) // 设置点击事件 .setAutoCancel(true); // 点击后自动消失 // 3. 发送通知 NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); nm.notify(1001, builder.build()); // 1001 是通知ID } // 取消通知 public static void cancelNotification(Context context, int notificationId) { NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); nm.cancel(notificationId); // 取消指定ID的通知 } } ``` #### 三、自定义通知布局 (参考引用[^2]) ```java // 创建自定义布局通知 public static void sendCustomNotification(Context context) { // 创建RemoteViews对象 RemoteViews remoteViews = new RemoteViews( context.getPackageName(), R.layout.custom_notification_layout ); // 设置自定义内容 remoteViews.setTextViewText(R.id.notification_title, "下载完成"); remoteViews.setTextViewText(R.id.notification_content, "点击安装"); remoteViews.setImageViewResource(R.id.notification_icon, R.drawable.ic_download); // 设置点击事件 Intent installIntent = new Intent(context, InstallActivity.class); PendingIntent pendingIntent = PendingIntent.getActivity( context, 0, installIntent, PendingIntent.FLAG_IMMUTABLE ); remoteViews.setOnClickPendingIntent(R.id.notification_root, pendingIntent); // 构建通知 Notification notification = new NotificationCompat.Builder(context, "CHANNEL_ID") .setSmallIcon(R.drawable.ic_small_icon) .setCustomContentView(remoteViews) // 设置自定义布局 .setStyle(new NotificationCompat.DecoratedCustomViewStyle()) .build(); // 发送通知 NotificationManager nm = context.getSystemService(NotificationManager.class); nm.notify(2001, notification); } ``` #### 四、关键注意事项 1. **Android 8.0+ 必须创建通知渠道**,否则通知不显示 2. 通知 ID (`notify()` 的第一个参数) 需唯一,用于后续管理通知 3. 自定义布局使用 `RemoteViews`,支持的控件有限(基本布局+TextView/ImageView等) 4. 高优先级通知需要申请 `POST_NOTIFICATIONS` 权限 (Android 13+) #### 五、通知管理操作 ```java // 取消所有通知 notificationManager.cancelAll(); // 更新通知 (使用相同ID再次调用notify()) builder.setContentText("更新后的内容"); notificationManager.notify(1001, builder.build()); ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值