函数startPinyinDecoderServic
e()检测PinyinDecoderService服务如果未运行,则通过系统函数bindService()来启动它。bindService的第2个参数对象有2个成员函数会在PinyinDecoderService服务启动过程中被调用:
onServiceConnected()
-- PinyinDecoderService建立,PinyinDecoderService.onBind()返回的binder对象作为函数的第2个参数传入。
onServiceDisconnected() --
PinyinDecoderService结束。
关于bindService()的第2个参数以及其在进程间调用的作用可参见
http://developer.android.com/guide/developing/tools/aidl.html,在PinyinIME中,它是ServiceConnection接口的一个实现,onServiceDisconnected()什么都不用做,onServiceConnected()在PinyinDecoderService建立时被调用,远程service进程的binder对象作为函数的第2个参数传入。输入法作为客户端进程,需要借助以下辅助函数把传入的binder对象转化成可用的接口:
IPinyinDecoderService.Stub.asInterface()
PinyinIME输入法把转化后的接口对象保存在类DecodingInfo对象mDecInfo的mIPinyinDecoderService成员,以供其后的调用:
mDecInfo.mIPinyinDecoderService的声明如下:
现在再来看在输入法(客户端进程)调用bindService时,服务端进程启动的详细过程:
因为第1个参数Intent对象的类名在调用前被设成PinyinDecoderService.class,所以系统进程响应bindService时,如果服务未运行时首先调用PinyinDecoderService的onCreate()
接下来调用PinyinDecoderService的onBind()函数,并把返回的binder对象传给前面说过的ServiceConnection.onServiceConnected():
辅助的初始化函数initPinyinEngine()首先打算系统静态字典资源文件res/raw/dict_pinyin.dat,然后把用户字典名写到一个byte数组里,最后把静态字典文件信息与用户字典名作为参数,调用本地c/c++函数nativeImOpenDecoderFd()。
onBind函数返回的binder对象,就是
private final IPinyinDecoderService.Stub mBinder = new IPinyinDecoderService.Stub() {
};