设为首页 加入收藏

TOP

Android4.0 输入法框架分析(二)
2014-11-24 11:47:47 来源: 作者: 【 】 浏览:3
Tags:Android4.0 输入法 框架 分析
ice中的回调方法IInputMethod加载进来,这似乎风马牛不相及,慢着:


由IInputMethodWrapper 声明可以看出玄机:


class IInputMethodWrapper extends IInputMethod.Stub {



}



现在,我们可以确定,InputMethodManagerService中的mCurMethod是由InputMethodService实现并传过来的。虽然,InputMethodService也是继承来的。



回到原来的问题:谁发送MSG_CREATE_SESSION的Message


有两个位置会发送:


startInputUncheckedLocked


和onServiceConnected



先分析startInputUncheckedLocked这个函数:


InputBindResult startInputUncheckedLocked(ClientState cs,


IInputContext inputContext, EditorInfo attribute, int controlFlags) {



//没有选中任何输入法,返回


if (mCurMethodId == null) {


return mNoBinding;


}



//输入法需要切换了


if (mCurClient != cs) {


//将当前的Client输入法解除绑定


unbindCurrentClientLocked();


}



//如果屏幕是亮着的


if (mScreenOn) {


try {


//将需要切换的输入法设置为活动的


cs.client.setActive(mScreenOn);


} catch (RemoteException e) {



}


}



//我们开启一个输入法,在数据库中会记录这个输入法的名称,mCurId 是从数据库中读取的名称


if (mCurId != null && mCurId.equals(mCurMethodId)) {


if (cs.curSession != null) {


//将当前的client端和InputMethodService绑定,并返回包含id、IInputMethodSession等信//息的InputBindResult


return attachNewInputLocked(


(controlFlags&InputMethodManager.CONTROL_START_INITIAL) != 0);


}



//若是已经绑定


if (mHaveConnection) {


if (mCurMethod != null) {


if (!cs.sessionRequested) {


cs.sessionRequested = true;


//发送创建 MSG_CREATE_SESSION 消息


executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(


MSG_CREATE_SESSION, mCurMethod,


new MethodCallback(mCurMethod, this)));


}


return new InputBindResult(null, mCurId, mCurSeq);


}


}



return startInputInnerLocked();


}



我们到handleMessage中看看如何创建Session的


case MSG_CREATE_SESSION:


args = (HandlerCaller.SomeArgs)msg.obj;


try {


//这里将MethodCallback的实例传到IInputMethodWrapper中去了


((IInputMethod)args.arg1).createSession(


(IInputMethodCallback)args.arg2);


} catch (RemoteException e) {


}


return true;


然后调用InputMethodWrapper中的createSession来创建Session



IInputMethodWrapper.java


public void createSession(IInputMethodCallback callback) {


mCaller.executeOrSendMessage(mCaller.obtainMessageO(DO_CREATE_SESSION, callback));


}



public void executeMessage(Message msg) {


case DO_CREATE_SESSION: {


//msg.obj是MethodCallback的实例


inputMethod.createSession(new InputMethodSessionCallbackWrapper(


mCaller.mContext, (IInputMethodCallback)msg.obj));


return;


}


}


我们知道,InputMethodService继承自AbstractInputMethodService,但是忽略了这个父类中所用到的类:


public abstract class AbstractInputMethodImpl implements InputMethod {


public void createSession(SessionCallback callback) {


//开始调用MethodCallbacks中中的sessionCreated了,那么,传入参数是什么呢?


callback.sessionCreated(onCreateInputMethodSessionInterface());


}


}


它实现了我们需要的方法:createSession



继续向下:


InputMethodService.java


//这里InputMethodSessionImpl才是真正的InputMethodService的回调方法类


public AbstractInputMethodImpl onCreateInputMethodInterface() {


return new InputMethodSessionImpl();


}



//这里实现InputMethodSession中定义的接口如下所示


public class InputMethodSessionImpl extends AbstractInputMethodSessionImpl {


public void finishInput() {}



public vo

首页 上一页 1 2 3 下一页 尾页 2/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇shell的startup文件 下一篇Android PopUp window的使用

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·“我用Java 8”已成 (2025-12-26 11:19:54)
·下载 IntelliJ IDEA (2025-12-26 11:19:52)
·Java是什么?(通俗 (2025-12-26 11:19:49)
·雾里看花:真正意义 (2025-12-26 10:54:36)
·C++——模板(超详细 (2025-12-26 10:54:34)