在 Android 12.0(API 级别 31)及更高版本中,系统提供了 CallStyle
通知模板,用于将通话通知与其他类型的通知区分开来。使用此模板创建呼入或正在进行的通话通知。该模板支持包含来电者信息和接听或拒绝通话等所需操作的大格式通知。
由于呼入和正在进行的通话是高优先级事件,因此这些通知在通知栏中会获得最高优先级。这种排名也使系统能够将这些优先通话转发到其他设备。
CallStyle
通知模板包含以下必需的操作:
- 呼入电话的接听或拒绝。
- 正在进行的通话的挂断。
- 电话筛选的接听或挂断。
此样式中的操作显示为按钮,系统会自动添加适当的图标和文本。不支持手动标记按钮。有关通知设计原则的更多信息,请参阅通知。

所需操作作为 Intent 传递,例如以下部分中的 hangupIntent
和 answerIntent
。每一个都是系统维护的令牌的引用。令牌是一个轻量级对象,可以在不同的应用和进程之间传递。系统负责管理令牌的生命周期,并确保即使创建它的应用不再运行,PendingIntent
仍然可用。当您将 PendingIntent
提供给另一个应用时,您即授予该应用执行指定操作(例如拒绝或接听)的权限。即使创建 Intent 的应用当前未运行,也会授予此权限。有关更多信息,请参阅 PendingIntent
的参考文档。
从 Android 14(API 级别 34)开始,您可以将通话通知配置为不可关闭。为此,请通过 Notification.Builder#setOngoing(true)
将 CallStyle
通知与 Notification.FLAG_ONGOING_EVENT
一起使用。
以下是使用 CallStyle
通知搭配各种方法的示例。
Kotlin
// Create a new call, setting the user as the caller. val incomingCaller = Person.Builder() .setName("Jane Doe") .setImportant(true) .build()
Java
// Create a new call with the user as the caller. Person incomingCaller = new Person.Builder() .setName("Jane Doe") .setImportant(true) .build();
呼入电话
使用 forIncomingCall()
方法为呼入电话创建通话风格通知。
Kotlin
// Create a call style notification for an incoming call. val builder = Notification.Builder(context, CHANNEL_ID) .setContentIntent(contentIntent) .setSmallIcon(smallIcon) .setStyle( Notification.CallStyle.forIncomingCall(caller, declineIntent, answerIntent)) .addPerson(incomingCaller)
Java
// Create a call style notification for an incoming call. Notification.Builder builder = Notification.Builder(context, CHANNEL_ID) .setContentIntent(contentIntent) .setSmallIcon(smallIcon) .setStyle( Notification.CallStyle.forIncomingCall(caller, declineIntent, answerIntent)) .addPerson(incomingCaller);
正在进行的通话
使用 forOngoingCall()
方法为正在进行的通话创建通话风格通知。
Kotlin
// Create a call style notification for an ongoing call. val builder = Notification.Builder(context, CHANNEL_ID) .setContentIntent(contentIntent) .setSmallIcon(smallIcon) .setStyle( Notification.CallStyle.forOngoingCall(caller, hangupIntent)) .addPerson(second_caller)
Java
// Create a call style notification for an ongoing call. Notification.Builder builder = new Notification.Builder(context, CHANNEL_ID) .setContentIntent(contentIntent) .setSmallIcon(smallIcon) .setStyle( Notification.CallStyle.forOngoingCall(caller, hangupIntent)) .addPerson(second_caller);
筛选电话
使用 forScreeningCall()
方法为电话筛选创建通话风格通知。
Kotlin
// Create a call style notification for screening a call. val builder = Notification.Builder(context, CHANNEL_ID) .setContentIntent(contentIntent) .setSmallIcon(smallIcon) .setStyle( Notification.CallStyle.forScreeningCall(caller, hangupIntent, answerIntent)) .addPerson(second_caller)
Java
// Create a call style notification for screening a call. Notification.Builder builder = new Notification.Builder(context, CHANNEL_ID) .setContentIntent(contentIntent) .setSmallIcon(smallIcon) .setStyle( Notification.CallStyle.forScreeningCall(caller, hangupIntent, answerIntent)) .addPerson(second_caller);
提供更多 Android 版本兼容性
将 API 版本 30 或更早版本上的 CallStyle
通知与前台服务相关联,以便赋予它们在 API 级别 31 或更高版本中获得的高排名。此外,API 版本 30 或更早版本上的 CallStyle
通知可以通过使用 setColorized()
方法将通知标记为彩色来获得类似的排名。
将 Telecom API 与 CallStyle
通知一起使用。有关更多信息,请参阅Telecom 框架概览。