如何取消Heads-Up Notification方式

从Android L开始,如果notification priority设置为HIGH,MAX,或者fullscreenIntent不为空,在非锁屏界面收到notification时屏幕上方会显示一个小悬浮窗口提醒用户,方便用户在不退出当前浏览界面的前提下快速响应该notification,即Heads-Up Notification(简称HUN)。如下图:

如何取消Heads-Up Notification方式_第1张图片

如果需要禁止某个应用notification以HUN方式显示,又不想降低notification或者拿掉fullscreenIntent,毕竟这两种方式会改变notification其他行为。

修改的方法是在systemUi中的baseStatusbar.java中根据应用的包名来拦截。

例如。要禁止来电以Heads-Up Notification方式显示,可修改如下:

protected boolean shouldInterrupt(StatusBarNotification sbn) {
        if (mNotificationData.shouldFilterOut(sbn)) {
            if (DEBUG) {
                Log.d(TAG, "Skipping HUN check for " + sbn.getKey() + " since it's filtered out.");
            }
            return false;
        }
       if (mHeadsUpNotificationView.isSnoozed(sbn.getPackageName())) {
            return false;
        }
        Notification notification = sbn.getNotification();
        boolean isNoisy = (notification.defaults & Notification.DEFAULT_SOUND) != 0
                || (notification.defaults & Notification.DEFAULT_VIBRATE) != 0
                || notification.sound != null
                || notification.vibrate != null;
        boolean isHighPriority = sbn.getScore() >= INTERRUPTION_THRESHOLD;
        boolean isFullscreen = notification.fullScreenIntent != null;
        boolean hasTicker = mHeadsUpTicker && !TextUtils.isEmpty(notification.tickerText);
        boolean isAllowed = notification.extras.getInt(Notification.EXTRA_AS_HEADS_UP,
                Notification.HEADS_UP_ALLOWED) != Notification.HEADS_UP_NEVER;
        boolean accessibilityForcesLaunch = isFullscreen
                && mAccessibilityManager.isTouchExplorationEnabled();
        /// M: Modify ScreenOn API @{
        final DisplayManager displayManager = (DisplayManager) mContext
                .getSystemService(Context.DISPLAY_SERVICE);
		/*
        boolean isScreeOn = displayManager.getDisplay(Display.DEFAULT_DISPLAY).getRealState()
                == Display.STATE_ON; 
		*/
		boolean isScreeOn = false;//for ALPS02003734 klocwork
         if(null != displayManager.getDisplay(Display.DEFAULT_DISPLAY))
		{			
			isScreeOn = displayManager.getDisplay(Display.DEFAULT_DISPLAY).getRealState() == Display.STATE_ON;   
		}
        boolean interrupt = (isFullscreen || (isHighPriority && (isNoisy || hasTicker)))
                && isAllowed
                && !accessibilityForcesLaunch
                // && mPowerManager.isScreenOn()
                && isScreeOn
                && (!mStatusBarKeyguardViewManager.isShowing()
                        || mStatusBarKeyguardViewManager.isOccluded())
                && !mStatusBarKeyguardViewManager.isInputRestricted()
                && (sbn.getPackageName().equals("com.android.dialer"));//根据包名拦截

               .... 
如此这样就可以实现拦截指定应用悬浮窗的通知.


你可能感兴趣的:(Android,悬浮窗拦截)