Android 侦听应用程序通知设置中的更改
我的应用程序能够触发一些通知 我希望能够检测到用户何时更改通知设置,例如,何时全局禁用通知设置,何时禁用一个频道,或者何时切换“允许通知点” 我尝试过这种方法:Android 侦听应用程序通知设置中的更改,android,android-notifications,Android,Android Notifications,我的应用程序能够触发一些通知 我希望能够检测到用户何时更改通知设置,例如,何时全局禁用通知设置,何时禁用一个频道,或者何时切换“允许通知点” 我尝试过这种方法: public class AppNotificationListenerService extends NotificationListenerService { private static final String TAG = "AppNLS"; @Override public void onCrea
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()
,但在切换其他应用程序的频道时也会调用该方法。我只需要从我的应用程序
这是正确的方法还是我想用另一种方法实现的?如中所述,API 28增加了系统广播,用于动作\通知\频道\阻塞\状态\更改了和动作\通知\频道\组\阻塞\状态\更改了
,应该适合您的需要()。谢谢!不幸的是,它只适用于Android API。那么应用程序的全局通知呢(这只适用于频道)?你知道有什么方法可以得到通知,它已经被启用/禁用了吗?刚刚看到这篇文章,有一个应用程序通知的操作:操作\u应用程序\u块\u状态\u更改了
()我在Manifest.xml中创建了广播接收器
,操作意图,但是接收(上下文:上下文?,意图:意图)
未被调用