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

}

android listen的更多相关文章

  1. 【定有惊喜】android程序员如何做自己的API接口?php与android的良好交互(附环境搭建),让前端数据动起来~

    一.写在前面 web开发有前端和后端之分,其实android还是有前端和后端之分.android开发就相当于手机app的前端,一般都是php+android或者jsp+android开发.androi ...

  2. Android来电监听和去电监听

    我觉得写文章就得写得有用一些的,必须要有自己的思想,关于来电去电监听将按照下面三个问题展开 1.监听来电去电有什么用? 2.怎么监听,来电去电监听方式一样吗? 3.实战,有什么需要特别注意地方? 监听 ...

  3. Android中基于CGroup的memory子系统HAL层分析-lmkd

    Android在内存管理上于Linux有些小的区别,其中一个就是引入了lowmemorykiller.从lowmemorykiller.c位于drivers/staging/android也可知道,属 ...

  4. Android基础总结(七)

    广播(掌握) 广播的概念 现实:电台通过发送广播发布消息,买个收音机,就能收听 Android:系统在产生某个事件时发送广播,应用程序使用广播接收者接收这个广播,就知道系统产生了什么事件. Andro ...

  5. Android RadioGroup和RadioButton详解

    实现RadioButton由两部分组成,也就是RadioButton和RadioGroup配合使用.RadioGroup是单选组合框,可以容纳多个RadioButton的容器.在没有RadioGrou ...

  6. Android端简易蓝牙聊天通讯App(原创)

    欢迎转载,但请注明出处!谢谢.http://www.cnblogs.com/weizhxa/p/5792775.html 最近公司在做一个蓝牙串口通讯的App,有一个固定的蓝牙设备,需要实现手机连接相 ...

  7. 分享 Ionic 开发 Hybrid App 中遇到的问题以及后期发布 iOS/Android 的方方面面

    此篇文章主要整理了最近在使用 Ionic 开发 Hybrid App 过程中遇到的一些疑难点以及后期发布生成 iOS 和 Android 版本过程中的种种问题. 文章目录 Ionic 简介和项目需求介 ...

  8. Android网络连接判断与处理

    博客分类: Android 获取网络信息需要在AndroidManifest.xml文件中加入相应的权限. <uses-permission android:name="android ...

  9. 【墙内备份】Android 6.0 APIs

    Android 6.0 APIs In this documentSHOW MORE Fingerprint Authentication Confirm Credential App Linking ...

随机推荐

  1. java基础第三天

  2. APP模板框架

    HTML页面 <!DOCTYPE html><html lang="en"><head> <meta charset="UTF- ...

  3. css和css3学习

    css和css3学习 css布局理解 css颜色大全 样式的层叠和继承 css ::before和::after伪元素的用法 中文字体font-family常用列表 cursor属性 css选择器 F ...

  4. <T> List<T>前面<T>的意思

    先看例子: import java.util.*; class Fruit { public String toString() { return "Fruit"; } } cla ...

  5. IIS 和 各个协议

    1,IIS是Internet information service是internet信息服务的简写,它支持三大服务器,WWW,FTP,SMTP(简单Mail传输协议): 2,NNTP(网络新闻传输协 ...

  6. C# 语言规范_版本5.0 (第13章 接口)

    1. 接口 一个接口定义一个协定.实现某接口的类或结构必须遵守该接口定义的协定.一个接口可以从多个基接口继承,而一个类或结构可以实现多个接口. 接口可以包含方法.属性.事件和索引器.接口本身不提供它所 ...

  7. nl2br()与nl2p()函数,php在字符串中的新行(\n)之前插入换行符

    使用情景 很多场合我们只是简单用textarea获取用户的长篇输入,而没有用编辑器.用户输入的换行以“\n”的方式入库,输出的时候有时候会没有换行,一大片文字直接出来了.这个时候可以根据库里的“\n” ...

  8. java调用wsdl xfire和cxf两种方式

    xfire 如下: String spID = ""; String password = ""; String accessCode = "&quo ...

  9. CSS-负边距原理

    一.负边距原理 正边距以相邻模块的位置为参考点进行移动,并对周围模块进行合理地排挤. 负边距即margin的四个边界值为负值. 在html中使用负边距margin-left和margin-top相当于 ...

  10. /home 和 /root

    /root     Linux超级权限用户root的家目录./home 如果我们建立一个用户,用户名是"xx",那么在/home目录下就有一个对应的/home/xx路径,用来存放用 ...