android listen

android listen,第1张

概述android监听机制,应该是一种观察者模式。摘抄网上教程,观察者模式的结构如下:其中涉及的角色有:●抽象主题(Subject)角色:抽象主题角色把所有对观察者对象的引用保存在一个聚集(比如ArrayList对象)里,每个主题都可以有任何数量的观察者。抽象主题提供一个接口,可以增加和删除观

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);        }    }}

 

 

转自https://www.cnblogs.com/caIDi/p/4091423.HTML

总结

以上是内存溢出为你收集整理的android listen全部内容,希望文章能够帮你解决android listen所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1069493.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-26
下一篇2022-05-26

发表评论

登录后才能评论

评论列表(0条)

    保存