android 屏蔽系统通知

1.源码路径:
frameworks/base/packages/SystemUI/src/com/android/systemui/statusbar/NotificationListener.java​

2.NotificationListener类是负责处理监听通知更新并将其传递给向用户显示的NotificationPresenter,也就是SystemUI中监听到系统通知的起点.

public class NotificationListener extends NotificationListenerWithPlugins {
    private static final String TAG = "NotificationListener";
    private static final boolean DEBUG = StatusBar.DEBUG;

    private final Context mContext;
    private final NotificationManager mNotificationManager;
    private final Handler mMainHandler;
    private final List<NotificationHandler> mNotificationHandlers = new ArrayList<>();
    private final ArrayList<NotificationSettingsListener> mSettingsListeners = new ArrayList<>();


    public NotificationListener(
            Context context,
            NotificationManager notificationManager,
            @Main Handler mainHandler) {
        mContext = context;
        mNotificationManager = notificationManager;
        mMainHandler = mainHandler;
    }

  
    public void addNotificationHandler(NotificationHandler handler) {
        if (mNotificationHandlers.contains(handler)) {
            throw new IllegalArgumentException("Listener is already added");
        }
        mNotificationHandlers.add(handler);
    }

   
    public void addNotificationSettingsListener(NotificationSettingsListener listener) {
        mSettingsListeners.add(listener);
    }

    @Override
    public void onListenerConnected() {
        if (DEBUG) Log.d(TAG, "onListenerConnected");
        onPluginConnected();
        final StatusBarNotification[] notifications = getActiveNotifications();
        if (notifications == null) {
            Log.w(TAG, "onListenerConnected unable to get active notifications.");
            return;
        }
        final RankingMap currentRanking = getCurrentRanking();
        mMainHandler.post(() -> {
            final List<Ranking> newRankings = new ArrayList<>();
            for (StatusBarNotification sbn : notifications) {
                newRankings.add(getRankingOrTemporaryStandIn(currentRanking, sbn.getKey()));
            }
            final RankingMap completeMap = new RankingMap(newRankings.toArray(new Ranking[0]));

            for (StatusBarNotification sbn : notifications) {
                for (NotificationHandler listener : mNotificationHandlers) {
                    listener.onNotificationPosted(sbn, completeMap);
                }
            }
            for (NotificationHandler listener : mNotificationHandlers) {
                listener.onNotificationsInitialized();
            }
        });
        onSilentStatusBarIconsVisibilityChanged(
                mNotificationManager.shouldHideSilentStatusBarIcons());
    }

    @Override
    public void onNotificationPosted(final StatusBarNotification sbn,
            final RankingMap rankingMap) {
        if (DEBUG) Log.d(TAG, "onNotificationPosted: " + sbn);
       
 /*
1.当收到通知时,会调用该方法,然后把通知Handler更新界面,所以可以在这个屏蔽通知的显示,可以通过参数设置,或者通过设置系统参数,这样可以通过配置参数就可以自由控制通知的显示,如下面,设置android_statusbar_visible参数,默认值为false,即不显示通知,


   if(!SystemProperties.getBoolean("android_statusbar_visible",false)) return;

*/
       
        if (sbn != null && !onPluginNotificationPosted(sbn, rankingMap)) {
            mMainHandler.post(() -> {
                processForRemoteInput(sbn.getNotification(), mContext);

                for (NotificationHandler handler : mNotificationHandlers) {
                    handler.onNotificationPosted(sbn, rankingMap);
                }
            });
        }
    }

    @Override
    public void onNotificationRemoved(StatusBarNotification sbn, RankingMap rankingMap,
            int reason) {
        if (DEBUG) Log.d(TAG, "onNotificationRemoved: " + sbn + " reason: " + reason);

/*
2.  当通知被移除时,也需要坐下过滤
   
   if(!SystemProperties.getBoolean("android_statusbar_visible",false)) return;

*/
        if (sbn != null && !onPluginNotificationRemoved(sbn, rankingMap)) {
            mMainHandler.post(() -> {
                for (NotificationHandler handler : mNotificationHandlers) {
                    handler.onNotificationRemoved(sbn, rankingMap, reason);
                }
            });
        }
    }

    @Override
    public void onNotificationRemoved(StatusBarNotification sbn, RankingMap rankingMap) {
        onNotificationRemoved(sbn, rankingMap, UNDEFINED_DISMISS_REASON);
    }

    @Override
    public void onNotificationRankingUpdate(final RankingMap rankingMap) {
        if (DEBUG) Log.d(TAG, "onRankingUpdate");

/*
3.这里是更新通知在通知列表上的排列,通知也需要坐下过滤

   if(!SystemProperties.getBoolean("android_statusbar_visible",false)) return;

*/
        if (rankingMap != null) {
            RankingMap r = onPluginRankingUpdate(rankingMap);
            mMainHandler.post(() -> {
                for (NotificationHandler handler : mNotificationHandlers) {
                    handler.onNotificationRankingUpdate(r);
                }
            });
        }
    }
    ......
}

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值