public abstract class NotificationListenerService
extends Service
| java.lang.Object | ||||
| ↳ | android.content.Context | |||
| ↳ | android.content.ContextWrapper | |||
| ↳ | android.app.Service | |||
| ↳ | android.service.notification.NotificationListenerService | |||
当发布或删除新通知或更改其排名时接收系统呼叫的服务。
要扩展此类,您必须在清单文件中声明服务并具有BIND_NOTIFICATION_LISTENER_SERVICE权限,并包含具有SERVICE_INTERFACE操作的意图过滤器。 例如:
<service android:name=".NotificationListener"
android:label="@string/service_name"
android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE">
<intent-filter>
<action android:name="android.service.notification.NotificationListenerService" />
</intent-filter>
</service>
在执行任何操作之前,服务应该等待onListenerConnected()事件。 requestRebind(ComponentName)方法是唯一可安全地在onListenerConnected()之前或onListenerDisconnected()之后onListenerDisconnected() 。
Nested classes |
|
|---|---|
class |
NotificationListenerService.Ranking 存储当前活动通知的排名相关信息。 |
class |
NotificationListenerService.RankingMap 提供对当前活动通知的排名信息的访问权限。 |
Constants |
|
|---|---|
int |
HINT_HOST_DISABLE_CALL_EFFECTS
|
int |
HINT_HOST_DISABLE_EFFECTS
|
int |
HINT_HOST_DISABLE_NOTIFICATION_EFFECTS
|
int |
INTERRUPTION_FILTER_ALARMS
|
int |
INTERRUPTION_FILTER_ALL
|
int |
INTERRUPTION_FILTER_NONE
|
int |
INTERRUPTION_FILTER_PRIORITY
|
int |
INTERRUPTION_FILTER_UNKNOWN
|
String |
SERVICE_INTERFACE
|
int |
SUPPRESSED_EFFECT_SCREEN_OFF 当屏幕关闭时,是否不应通过DND抑制通知。 |
int |
SUPPRESSED_EFFECT_SCREEN_ON 屏幕打开时,DND是否禁止通知可视化中断。 |
Inherited constants |
|---|
android.app.Service
|
android.content.Context
|
android.content.ComponentCallbacks2
|
Public constructors |
|
|---|---|
NotificationListenerService() |
|
Protected methods |
|
|---|---|
void |
attachBaseContext(Context base) 为此ContextWrapper设置基本上下文。 |
Inherited methods |
|
|---|---|
android.app.Service
|
|
android.content.ContextWrapper
|
|
android.content.Context
|
|
java.lang.Object
|
|
android.content.ComponentCallbacks2
|
|
android.content.ComponentCallbacks
|
|
int HINT_HOST_DISABLE_CALL_EFFECTS
Listener hints常量 - 主设备UI应禁用电话呼叫声,buyt不通知声音。 这不会改变中断过滤器,只会影响效果。
常量值:4(0x00000004)
int HINT_HOST_DISABLE_EFFECTS
Listener hints常数 - 主设备UI应禁用通知声音,振动和其他视觉或听觉效果。 这不会改变中断过滤器,只会影响效果。
常数值:1(0x00000001)
int HINT_HOST_DISABLE_NOTIFICATION_EFFECTS
Listener hints常量 - 主设备UI应禁用通知声音,但不能拨打电话。 这不会改变中断过滤器,只会影响效果。
常量值:2(0x00000002)
int INTERRUPTION_FILTER_ALARMS
Interruption filter常数 - 仅警报中断过滤器。
常量值:4(0x00000004)
int INTERRUPTION_FILTER_ALL
Interruption filter常量 - 正常中断过滤器。
常数值:1(0x00000001)
int INTERRUPTION_FILTER_NONE
Interruption filter常量 - 不中断过滤器。
常量值:3(0x00000003)
int INTERRUPTION_FILTER_PRIORITY
Interruption filter常数 - 优先中断过滤器。
常量值:2(0x00000002)
int INTERRUPTION_FILTER_UNKNOWN
Interruption filter常数 - 由于任何原因该值不可用时返回。 例如,在通知侦听器连接之前。
也可以看看:
常量值:0(0x00000000)
String SERVICE_INTERFACE
Intent必须声明为由服务处理。
常量值:“android.service.notification.NotificationListenerService”
int SUPPRESSED_EFFECT_SCREEN_OFF
当屏幕关闭时,是否不应通过DND抑制通知。
常数值:1(0x00000001)
int SUPPRESSED_EFFECT_SCREEN_ON
屏幕打开时,DND是否禁止通知可视化中断。
常量值:2(0x00000002)
void cancelAllNotifications ()
通知通知管理员关于解除所有通知。
如果您的监听器具有允许用户忽略所有通知的用户界面,请使用此功能,类似于Android的状态栏和通知面板的行为。 它应该在用户调用UI的“关闭所有”功能后调用; 得到通知后,通知管理员将实际删除所有活动通知,并且您将获得多个onNotificationRemoved(StatusBarNotification)回调。
在执行此操作之前,该服务应该等待 onListenerConnected()事件。
void cancelNotification (String pkg, String tag, int id)
此方法在API级别21中已弃用。
改为使用cancelNotification(String) 。 从LOLLIPOP开始,此方法将不再取消通知。 它将继续取消targetSdkVersion早于LOLLIPOP申请通知。
通知通知管理员关于解雇单个通知。
如果您的监听器具有允许用户解除单个通知的用户界面,请使用此功能,类似于Android的状态栏和通知面板的行为。 应该在用户使用您的UI解散单个通知后调用它; 一旦被通知,通知管理员将实际删除通知,并且您将收到onNotificationRemoved(StatusBarNotification)回onNotificationRemoved(StatusBarNotification) 。
注意:如果您的监听程序允许用户通过点击/单击/等等来触发通知 contentIntent ,那么 如果有问题的通知设置了 FLAG_AUTO_CANCEL标志,则应该在此时调用此方法。
执行此操作之前,该服务应等待 onListenerConnected()事件。
| Parameters | |
|---|---|
pkg |
String: Package of the notifying app. |
tag |
String: Tag of the notification as specified by the notifying app in notify(String, int, android.app.Notification). |
id |
int: ID of the notification as specified by the notifying app in notify(String, int, android.app.Notification). |
void cancelNotification (String key)
通知通知管理员关于解雇单个通知。
如果您的监听器具有允许用户解除单个通知的用户界面,请使用此功能,类似于Android的状态栏和通知面板的行为。 应该在用户使用您的UI解散单个通知后调用它; 得到通知后,通知管理员将实际删除通知,您将收到onNotificationRemoved(StatusBarNotification)回onNotificationRemoved(StatusBarNotification) 。
注意:如果您的监听程序允许用户通过点击/单击/等等来触发通知 contentIntent ,那么 如果有问题的通知设置了 FLAG_AUTO_CANCEL标志,则应该在该时间调用此方法。
执行此操作之前,该服务应等待 onListenerConnected()事件。
| Parameters | |
|---|---|
key |
String: Notification to dismiss from getKey(). |
void cancelNotifications (String[] keys)
通知通知管理员关于解除特定通知。
如果您的监听器具有允许用户一次性关闭多个通知的用户界面,请使用此选项。
执行此操作之前,该服务应等待 onListenerConnected()事件。
| Parameters | |
|---|---|
keys |
String: Notifications to dismiss, or null to dismiss all. |
StatusBarNotification[] getActiveNotifications (String[] keys)
通过密钥请求一个或多个通知。 如果您一直在跟踪通知但不想保留这些位,现在需要返回并从这些通知中提取更多数据。
执行此操作之前,服务应等待 onListenerConnected()事件。
| Parameters | |
|---|---|
keys |
String: the keys of the notifications to request |
| Returns | |
|---|---|
StatusBarNotification[] |
An array of notifications corresponding to the requested keys, in the same order as the key list. |
StatusBarNotification[] getActiveNotifications ()
请求未完成通知的列表(即当前用户可见的列表)。 当您不知道已发布的内容时很有用。
执行此操作之前,该服务应等待 onListenerConnected()事件。
| Returns | |
|---|---|
StatusBarNotification[] |
An array of active notifications, sorted in natural order. |
int getCurrentInterruptionFilter ()
获取主机上当前的通知中断过滤器处于活动状态。
中断过滤器定义哪些通知被允许中断用户(例如,通过声音和振动)并且被全局应用。 听众可以通过matchesInterruptionFilter()找出特定的通知是否与中断过滤器匹配。
如果通知主机不支持或拒绝应用请求的过滤器,或者另一个组件在此期间更改了过滤器,则当前过滤器可能与先前请求的过滤器不同。
使用 onInterruptionFilterChanged(int)收听更新。
执行此操作之前,该服务应等待 onListenerConnected()事件。
| Returns | |
|---|---|
int |
One of the INTERRUPTION_FILTER_ constants, or INTERRUPTION_FILTER_UNKNOWN when unavailable. |
int getCurrentListenerHints ()
获取代表当前状态的提示集。
如果提示表示所有侦听器共享的状态或通知主机不支持或拒绝授予的功能,则当前状态可能与请求的状态不同。
在执行此操作之前,该服务应该等待 onListenerConnected()事件。
| Returns | |
|---|---|
int |
Zero or more of the HINT_ constants. |
NotificationListenerService.RankingMap getCurrentRanking ()
返回当前排名信息。
返回的对象表示当前的排名快照,仅适用于当前活动的通知。
一般来说,您应该使用通过诸如onNotificationPosted(StatusBarNotification, RankingMap)等事件传递的onNotificationRemoved(StatusBarNotification, RankingMap) 。 只有在需要访问此类事件外时才应使用此方法,例如在初始化后立即检索RankingMap。
执行此操作之前,该服务应等待 onListenerConnected()事件。
| Returns | |
|---|---|
NotificationListenerService.RankingMap |
A NotificationListenerService.RankingMap object providing access to ranking information |
IBinder onBind (Intent intent)
这不是您正在寻找的生命周期事件。
在执行任何操作之前,该服务应该等待 onListenerConnected()事件。
| Parameters | |
|---|---|
intent |
Intent: The Intent that was used to bind to this service, as given to Context.bindService. Note that any extras that were included with the Intent at that point will not be seen here. |
| Returns | |
|---|---|
IBinder |
Return an IBinder through which clients can call on to the service. |
void onDestroy ()
由系统调用以通知服务它已不再使用并正在被删除。 这个服务应该清理它所拥有的任何资源(线程,注册接收者等)。 返回后,将不会有更多的调用这个服务对象,它实际上已经死了。 不要直接调用这个方法。
void onInterruptionFilterChanged (int interruptionFilter)
实施此方法以在 interruption filter更改时得到通知。
| Parameters | |
|---|---|
interruptionFilter |
int: The current interruption filter. |
void onListenerConnected ()
实现此方法以了解监听器何时启用并连接到通知管理器。 您目前可以安全拨打getActiveNotifications() 。
void onListenerDisconnected ()
实施此方法以了解监听器何时与通知管理器断开连接。此次呼叫后您将不会收到任何事件,并且此时只能呼叫 requestRebind(ComponentName) 。
void onListenerHintsChanged (int hints)
实施此方法以在 Listener hints更改时得到通知。
| Parameters | |
|---|---|
hints |
int: The current listener hints. |
void onNotificationPosted (StatusBarNotification sbn, NotificationListenerService.RankingMap rankingMap)
实施此方法以了解应用程序发布的新通知。
| Parameters | |
|---|---|
sbn |
StatusBarNotification: A data structure encapsulating the original Notification object as well as its identifying information (tag and id) and source (package name). |
rankingMap |
NotificationListenerService.RankingMap: The current ranking map that can be used to retrieve ranking information for active notifications, including the newly posted one. |
void onNotificationPosted (StatusBarNotification sbn)
实施此方法以了解应用程序发布的新通知。
| Parameters | |
|---|---|
sbn |
StatusBarNotification: A data structure encapsulating the original Notification object as well as its identifying information (tag and id) and source (package name). |
void onNotificationRankingUpdate (NotificationListenerService.RankingMap rankingMap)
在通知排名变化时实施此方法以通知您。
| Parameters | |
|---|---|
rankingMap |
NotificationListenerService.RankingMap: The current ranking map that can be used to retrieve ranking information for active notifications. |
void onNotificationRemoved (StatusBarNotification sbn)
实施此方法以了解何时删除通知。
这可能是因为用户已使用系统UI(或其他通知侦听程序)解除通知或由于应用程序已撤消通知。
注意:您收到的StatusBarNotification对象将是“光”; 也就是说, getNotification()的结果可能会丢失一些重量级字段,例如contentView和largeIcon 。 但是, StatusBarNotification上的所有其他字段(足以与之前致电onNotificationPosted(StatusBarNotification)此呼叫匹配)将保持不变。
| Parameters | |
|---|---|
sbn |
StatusBarNotification: A data structure encapsulating at least the original information (tag and id) and source (package name) used to post the Notification that was just removed. |
void onNotificationRemoved (StatusBarNotification sbn, NotificationListenerService.RankingMap rankingMap)
实施此方法以了解何时删除通知。
这可能是因为用户已使用系统UI(或其他通知侦听程序)解除通知或由于应用程序已撤消通知。
注意:您收到的StatusBarNotification对象将是“光”; 也就是说, getNotification()的结果可能会丢失一些重量级字段,例如contentView和largeIcon 。 但是, StatusBarNotification上的所有其他字段都将保持原样,并且此前的电话号码与onNotificationPosted(StatusBarNotification)电话号码匹配。
| Parameters | |
|---|---|
sbn |
StatusBarNotification: A data structure encapsulating at least the original information (tag and id) and source (package name) used to post the Notification that was just removed. |
rankingMap |
NotificationListenerService.RankingMap: The current ranking map that can be used to retrieve ranking information for active notifications. |
void requestInterruptionFilter (int interruptionFilter)
设置所需的 interruption filter 。
这仅仅是一个请求,主机可能或可能不会选择根据其他监听器请求或其他全局状态来应用所请求的中断过滤器。
使用 onInterruptionFilterChanged(int)收听更新。
执行此操作之前,该服务应该等待 onListenerConnected()事件。
| Parameters | |
|---|---|
interruptionFilter |
int: One of the INTERRUPTION_FILTER_ constants. |
void requestListenerHints (int hints)
设置所需的 listener hints 。
这仅仅是一个请求,主机可能会或可能不会选择根据其他监听器请求或其他全局状态采取行动。
使用 onListenerHintsChanged(int)收听更新。
在执行此操作之前,该服务应该等待 onListenerConnected()事件。
| Parameters | |
|---|---|
hints |
int: One or more of the HINT_ constants. |
void requestRebind (ComponentName componentName)
请求在先前调用(@link requestUnbind)之后侦听器被反弹。
对于尚未被用户授予权限的侦听器,此方法将失败。
| Parameters | |
|---|---|
componentName |
ComponentName
|
void requestUnbind ()
要求该服务被解除绑定。
在调用requestRebind(ComponentName)之前,这将不再接收更新。 此次通话后,系统可能会将该服务归档。
执行此操作之前,该服务应等待onListenerConnected()事件。 我知道这很诱人,但你必须等待。
void setNotificationsShown (String[] keys)
通知通知管理员这些通知已被用户查看。 只有当用户充分确信用户正在查看通知时才能调用此功能,例如由于明确的用户交互而在屏幕上显示通知时。
在执行此操作之前,该服务应该等待 onListenerConnected()事件。
| Parameters | |
|---|---|
keys |
String: Notifications to mark as seen. |
void attachBaseContext (Context base)
为此ContextWrapper设置基本上下文。 所有的调用都会被委托给基础上下文。 如果已经设置了基本上下文,则引发IllegalStateException。
| Parameters | |
|---|---|
base |
Context: The new base context for this wrapper. |