zoukankan      html  css  js  c++  java
  • android listen

    android监听机制,应该是一种观察者模式。

    摘抄网上教程,观察者模式的结构如下:

    其中涉及的角色有:

      ●  抽象主题(Subject)角色:抽象主题角色把所有对观察者对象的引用保存在一个聚集(比如ArrayList对象)里,每个主题都可以有任何数量的观察者。抽象主题提供一个接口,可以增加和删除观察者对象,抽象主题角色又叫做抽象被观察者(Observable)角色。

      ●  具体主题(ConcreteSubject)角色:将有关状态存入具体观察者对象;在具体主题的内部状态改变时,给所有登记过的观察者发出通知。具体主题角色又叫做具体被观察者(Concrete Observable)角色。

      ●  抽象观察者(Observer)角色:为所有的具体观察者定义一个接口,在得到主题的通知时更新自己,这个接口叫做更新接口。

      ●  具体观察者(ConcreteObserver)角色:存储与主题的状态自恰的状态。具体观察者角色实现抽象观察者角色所要求的更新接口,以便使本身的状态与主题的状态 像协调。如果需要,具体观察者角色可以保持一个指向具体主题对象的引用。

    android4.4 InCallUI中,用一个类CallList来维护当前所有的Call信息,当某个call状态发生改变之后,要通知到UI进行界面更新;这个类似于一个被观察者:

        public void addListener(Listener listener) {
            Preconditions.checkNotNull(listener);
            mListeners.add(listener);
        }

        public void removeListener(Listener listener) {
            Preconditions.checkNotNull(listener);
            mListeners.remove(listener);
        }

        private void notifyListenersOfChange() {
            for (Listener listener : mListeners) {
                listener.onCallListChange(this);
            }
        }

    可以添加一些列观察者,并负责notify到他们。

    在被观察者类中定义了一些接口。

        /**
         * Listener interface for any class that wants to be notified of changes
         * to the call list.
         */
        public interface Listener {
            /**
             * Called when a new incoming call comes in.
             * This is the only method that gets called for incoming calls. Listeners
             * that want to perform an action on incoming call should respond in this method
             * because {@link #onCallListChange} does not automatically get called for
             * incoming calls.
             */
            public void onIncomingCall(Call call);

            /**
             * Called anytime there are changes to the call list.  The change can be switching call
             * states, updating information, etc. This method will NOT be called for new incoming
             * calls and for calls that switch to disconnected state. Listeners must add actions
             * to those method implementations if they want to deal with those actions.
             */
            public void onCallListChange(CallList callList);

            /**
             * Called when a call switches to the disconnected state.  This is the only method
             * that will get called upon disconnection.
             */
            public void onDisconnect(Call call);
        }

    观察者要实现这些接口,以便得到通知后,要做相应的处理工作。

    class InCallPresenter implements CallList.Listener {

        /**
         * Called when a call becomes disconnected. Called everytime an existing call
         * changes from being connected (incoming/outgoing/active) to disconnected.
         */
        @Override
        public void onDisconnect(Call call) {
            hideDialpadForDisconnect();
            maybeShowErrorDialogOnDisconnect(call);

            // We need to do the run the same code as onCallListChange.
            onCallListChange(CallList.getInstance());

            if (isActivityStarted()) {
                mInCallActivity.dismissKeyguard(false);
            }
        }

    }

  • 相关阅读:
    clearstatcache清除文件状态缓存
    使用mysql创建自己的物化视图
    python——复制目录结构小脚本
    开发一个jQuery插件——多级联动菜单
    pack、unpack自制二进制“数据库”
    博客爬取系统
    session放入缓存(redis)、DB
    centos+nginx从零开始配置负载均衡
    几个容易被忽略的mysql知识
    数组方式进行表单提交
  • 原文地址:https://www.cnblogs.com/caidi/p/4091423.html
Copyright © 2011-2022 走看看