android - 监听应用程序通知设置的变化
问题描述
我的应用程序能够触发一些通知。
我希望能够检测用户何时更改通知设置,例如当它全局禁用它们时,当他禁用一个频道时,或者当他切换“允许通知点”时......
我试过NotificationListenerService方法:
public class AppNotificationListenerService extends NotificationListenerService {
private static final String TAG = "AppNLS";
@Override
public void onCreate() {
super.onCreate();
Log.d(TAG, "onCreate: ");
}
@Override
public IBinder onBind(Intent intent) {
Log.d(TAG, "onBind: ");
return super.onBind(intent);
}
@Override
public boolean onUnbind(Intent intent) {
Log.d(TAG, "onUnbind: ");
return super.onUnbind(intent);
}
@Override
public void onNotificationPosted(StatusBarNotification sbn) {
super.onNotificationPosted(sbn);
Log.d(TAG, "onNotificationPosted: ");
}
@Override
public void onNotificationRemoved(StatusBarNotification sbn) {
super.onNotificationRemoved(sbn);
Log.d(TAG, "onNotificationRemoved: ");
}
@Override
public void onListenerConnected() {
super.onListenerConnected();
Log.d(TAG, "onListenerConnected: ");
}
@Override
public void onListenerDisconnected() {
super.onListenerDisconnected();
Log.d(TAG, "onListenerDisconnected: ");
}
@Override
public void onNotificationChannelModified(String pkg, UserHandle user, NotificationChannel channel, int modificationType) {
super.onNotificationChannelModified(pkg, user, channel, modificationType);
Log.d(TAG, "onNotificationChannelModified: ");
}
@Override
public void onListenerHintsChanged(int hints) {
super.onListenerHintsChanged(hints);
Log.d(TAG, "onListenerHintsChanged: ");
}
@Override
public void onNotificationPosted(StatusBarNotification sbn, RankingMap rankingMap) {
super.onNotificationPosted(sbn, rankingMap);
Log.d(TAG, "onNotificationPosted: ");
}
@Override
public void onNotificationRemoved(StatusBarNotification sbn, RankingMap rankingMap) {
super.onNotificationRemoved(sbn, rankingMap);
Log.d(TAG, "onNotificationRemoved: ");
}
@Override
public void onNotificationRemoved(StatusBarNotification sbn, RankingMap rankingMap, int reason) {
super.onNotificationRemoved(sbn, rankingMap, reason);
Log.d(TAG, "onNotificationRemoved: ");
}
@Override
public void onNotificationRankingUpdate(RankingMap rankingMap) {
super.onNotificationRankingUpdate(rankingMap);
Log.d(TAG, "onNotificationRankingUpdate: ");
}
@Override
public void onNotificationChannelGroupModified(String pkg, UserHandle user, NotificationChannelGroup group, int modificationType) {
super.onNotificationChannelGroupModified(pkg, user, group, modificationType);
Log.d(TAG, "onNotificationChannelGroupModified: ");
}
@Override
public void onInterruptionFilterChanged(int interruptionFilter) {
super.onInterruptionFilterChanged(interruptionFilter);
Log.d(TAG, "onInterruptionFilterChanged: ");
}
}
在清单中,我添加了服务:
<service android:name=".services.AppNotificationListenerService"
android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE" >
<intent-filter>
<action android:name="android.service.notification.NotificationListenerService" />
</intent-filter>
</service>
服务已正确创建绑定,我看到onNotificationPosted()
并onNotificationRemoved()
在通知区域显示/删除通知时。
但是,当我全局启用/禁用应用程序的通知时,不会调用任何方法。当我启用/禁用频道通知时,唯一调用的方法是 onNotificationRankingUpdate()
,但在切换其他应用程序的频道时也会调用此方法。我只需要从我的应用程序中。
这是正确的方式还是我想要实现的目标是以另一种方式完成的?
解决方案
推荐阅读
- java - 无法连接spring cloud consul(无法设置主机/端口)
- python - 如何在保留所有行的同时连接行并且每组有一个结果值
- xquery - Marklogic 未定义函数 fn:string-pad()
- python - 删除重复的单词,但保留句子中的重复数字
- python - 如何只打印字典中的一项
- git - 如何预览合并冲突?
- android - Android游戏客户端,Fragment上未显示成就解锁弹出窗口
- html - 如何对齐列内的多个元素?
- python - AWSCLI 在 Mac 上安装成功,然后 bash 命令失败
- c# - Visual Studio 2012 中的 ViewState 语法错误?