美文网首页
Android P通过settings字段动态控制通知栏显示与隐

Android P通过settings字段动态控制通知栏显示与隐

作者: l900 | 来源:发表于2021-07-02 11:42 被阅读0次

    客户定制需求:

    能够动态控制系统的通知显示<隐藏>在状态栏

    大致修改如下:

    找到处理通知栏的代码位置:

    frameworks\base\services\core\java\com\android\server\notification\NotificationManagerService.java
    

    每次通知都会调用到这里,用以显示通知在状态栏

    void enqueueNotificationInternal(final String pkg, final String opPkg, final int callingUid,
                final int callingPid, final String tag, final int id, final Notification notification,
                int incomingUserId) {
    

    因此可以在enqueueNotificationInternal 方法中添加如下判断:如果当前的通知栏是显示状态则显示,否则跳过显示通知操作。<Settings.Secure.NOTIFICATION_ENABLED :添加到settings数据库中的字段,需要自行添加>

    int value = android.provider.Settings.Secure.getInt(getContext().getContentResolver(), android.provider.Settings.Secure.NOTIFICATION_ENABLED, 0);       
            if(value==1){
                return;
            } 
    

    定义settings NOTIFICATION_ENABLED观察者,监听值的变化,控制通知栏的显示或隐藏

    ContentObserver mContentOb = new ContentObserver(new Handler()) {
        
            @Override
            public void onChange(boolean selfChange) {
                
                int value = android.provider.Settings.Secure.getInt(getContext().getContentResolver(), android.provider.Settings.Secure.NOTIFICATION_ENABLED, 0);       
                if(value == 1){ 
                    setNotificationHideByUser(true);
                }else{
                    setNotificationHideByUser(false);
                }
            }
        };
        
    

    注册settings观察者,监听NOTIFICATION_ENABLED 的值

    private final class SettingsObserver extends ContentObserver {
    ···
    
    void observe() {
                ContentResolver resolver = getContext().getContentResolver();
                resolver.registerContentObserver(NOTIFICATION_BADGING_URI,
                        false, this, UserHandle.USER_ALL);
                resolver.registerContentObserver(NOTIFICATION_LIGHT_PULSE_URI,
                        false, this, UserHandle.USER_ALL);
                resolver.registerContentObserver(NOTIFICATION_RATE_LIMIT_URI,
                        false, this, UserHandle.USER_ALL);
                update(null);
                /** add by 1900 for hide or show notification*/
                  resolver.registerContentObserver(Settings.Secure.getUriFor(Settings.Secure.NOTIFICATION_ENABLED),false, mContentOb);  
               /* add end */
            }
    ···
    
    

    定义处理通知栏显示隐藏的方法:遍历所有在队列中的通知,并显示或者隐藏

    private void setNotificationHideByUser(boolean isHide){     
            Log.d("1900","setNotificationHideByUser start isHide " +isHide);
            int size = mNotificationList.size();
            List<NotificationRecord> changedNotifications = new ArrayList<>();
            for(int i=0;i<size;i++){            
                NotificationRecord rec = mNotificationList.get(i);               
                rec.setHidden(isHide);
                changedNotifications.add(rec);    
            }
            if(isHide)
                mListeners.notifyHiddenLocked(changedNotifications);
            else
                mListeners.notifyUnhiddenLocked(changedNotifications);
        }
    

    相关文章

      网友评论

          本文标题:Android P通过settings字段动态控制通知栏显示与隐

          本文链接:https://www.haomeiwen.com/subject/dqmeultx.html