android RIL Java 详解 -------- RegistrantList
2014-09-24 00:14
225 查看
Android RIL java 和 RIL C 使用的是异步通信机制,RIL java 中一个线程中发送消息给RIL C,RIL C 响应(RESPONSE_SOLICITED 和 RESPONSE_UNSOLICITED),RIL java再去处理,在这个RIL java 处理这些相应是主要是使用了RegistrantList 消息处理机制。
从设计模式的角度来看RegistrantList消息处理机制属于观察者模式,
1、观察者(Registrant 实例)将自己注册到被观察对象(RegistrantList实例)中,被观察对象将观察者存放在一个容器(registrants)里。
2、被观察对象(RegistrantList实例)发生了某种变化,从容器中得到所有注册过的观察者,将变化通知观察者。
3、被观察者要撤销观察,被观察者从容器中将观察者去除。
下面是一个列子用于说明如何使用RegistratList 消息处理机制,
重点代码
1、观察者类
2、被观察者类
从设计模式的角度来看RegistrantList消息处理机制属于观察者模式,
1、观察者(Registrant 实例)将自己注册到被观察对象(RegistrantList实例)中,被观察对象将观察者存放在一个容器(registrants)里。
2、被观察对象(RegistrantList实例)发生了某种变化,从容器中得到所有注册过的观察者,将变化通知观察者。
3、被观察者要撤销观察,被观察者从容器中将观察者去除。
下面是一个列子用于说明如何使用RegistratList 消息处理机制,
protected RegistrantList mOnRegistrants = new RegistrantList(); //注册Radio On 观察者 public void registerForOn(Handler h, int what, Object obj) { Registrant r = new Registrant (h, what, obj); synchronized (mStateMonitor) { mOnRegistrants.add(r); if (mState.isOn()) { r.notifyRegistrant(new AsyncResult(null, null, null)); } } } //删除观察者 public void unregisterForOn(Handler h) { synchronized (mStateMonitor) { mOnRegistrants.remove(h); } } //更新观察者 protected void setRadioState(RadioState newState) { if (mState.isOn() && !oldState.isOn()) { Log.d(LOG_TAG,"Notifying: Radio On"); mOnRegistrants.notifyRegistrants(); } } protected void init(Context context, PhoneNotifier notifier) { //调用注册观察者,配置回调函数及参数 mCM.registerForOn(this, EVENT_RADIO_ON, null); }
重点代码
1、观察者类
/* * Copyright (C) 2006 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package android.os; import android.os.Handler; import android.os.Message; import java.lang.ref.WeakReference; import java.util.HashMap; /** @hide */ public class Registrant { //构造观察者 public Registrant(Handler h, int what, Object obj) { refH = new WeakReference(h); this.what = what; userObj = obj; } public void clear() { refH = null; userObj = null; } public void notifyRegistrant() { internalNotifyRegistrant (null, null); //通知更新 } public void notifyResult(Object result) { internalNotifyRegistrant (result, null); } public void notifyException(Throwable exception) { internalNotifyRegistrant (null, exception); } /** * This makes a copy of @param ar */ public void notifyRegistrant(AsyncResult ar) { internalNotifyRegistrant (ar.result, ar.exception); } /*package*/ void internalNotifyRegistrant (Object result, Throwable exception) { Handler h = getHandler(); if (h == null) { clear(); } else { //利用handler机制异步更新 Message msg = Message.obtain(); msg.what = what; msg.obj = new AsyncResult(userObj, result, exception); h.sendMessage(msg); } } /** * NOTE: May return null if weak reference has been collected */ public Message messageForRegistrant() { Handler h = getHandler(); if (h == null) { clear(); return null; } else { Message msg = h.obtainMessage(); msg.what = what; msg.obj = userObj; return msg; } } public Handler getHandler() { if (refH == null) return null; return (Handler) refH.get(); } WeakReference refH; //注册handler int what; //注册的消息类型 Object userObj; }
2、被观察者类
/* * Copyright (C) 2006 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package android.os; import android.os.Handler; import android.os.Message; import java.util.ArrayList; import java.util.HashMap; /** @hide */ public class RegistrantList { ArrayList registrants = new ArrayList(); // of Registrant //观察者的容器 //添加观察者对象 public synchronized void add(Handler h, int what, Object obj) { add(new Registrant(h, what, obj)); } public synchronized void addUnique(Handler h, int what, Object obj) { // if the handler is already in the registrant list, remove it remove(h); add(new Registrant(h, what, obj)); } public synchronized void add(Registrant r) { removeCleared(); registrants.add(r); } public synchronized void removeCleared() { for (int i = registrants.size() - 1; i >= 0 ; i--) { Registrant r = (Registrant) registrants.get(i); if (r.refH == null) { registrants.remove(i); } } } public synchronized int size() { return registrants.size(); } public synchronized Object get(int index) { return registrants.get(index); } //更新观察者 private synchronized void internalNotifyRegistrants (Object result, Throwable exception) { for (int i = 0, s = registrants.size(); i < s ; i++) { Registrant r = (Registrant) registrants.get(i); r.internalNotifyRegistrant(result, exception); } } public /*synchronized*/ void notifyRegistrants() { internalNotifyRegistrants(null, null); } public /*synchronized*/ void notifyException(Throwable exception) { internalNotifyRegistrants (null, exception); } public /*synchronized*/ void notifyResult(Object result) { internalNotifyRegistrants (result, null); } public /*synchronized*/ void notifyRegistrants(AsyncResult ar) { internalNotifyRegistrants(ar.result, ar.exception); } //移除观察者 public synchronized void remove(Handler h) { for (int i = 0, s = registrants.size() ; i < s ; i++) { Registrant r = (Registrant) registrants.get(i); Handler rh; rh = r.getHandler(); /* Clean up both the requested registrant and * any now-collected registrants */ if (rh == null || rh == h) { r.clear(); } } removeCleared(); } }
相关文章推荐
- Android RIL java 详解---------command interface
- android RIL Java 详解 ---- 入口 Phone factory
- Android RIL Java 详解 -------- CDMALTEPhone
- android-WebView详解实例(JavaScript调用Java方法,Java调用JavaScript方法)
- Android RIL的java框架
- android froyo framework内RIL.java类分析
- Android RIL Java
- 【贪吃蛇—Java程序员写Android游戏】系列 3. 用J2ME实现Android的Snake Sample详解
- Win7环境下的Java和Android环境变量配置详解
- Win7环境下的Java和Android环境变量配置详解
- Android核心分析之(18)Android电话系统之RIL-Java
- Android RIL 本地代码(c/c++) 和 Java代码解析
- Android核心分析(18)-----Android电话系统之RIL-Java
- win7中java或android开发环境配置详解
- android4.0网络服务状态或者信号量强度上报过程(RIL Framework Java 部分)
- Android RIL的java框架
- 【贪吃蛇—Java程序员写Android游戏】系列 1.Android SDK Sample-Snake详解
- 【贪吃蛇—Java程序员写Android游戏】系列 1.Android SDK Sample-Snake详解
- Android资源和R.java文件详解《一》
- android 横屏竖屏设置的详解(最好不要用Java代码实现,用xml配置文件吧)