PinyinIME的onCreate函数实现调用startPinyinDecoderService的原理

PinyinIME(也在Android源码的packags/inputmethods下)的onCreate()函数,省略掉与LatinIME类似的代码。
       @Override
       public void onCreate() {
             ...
             startPinyinDecoderServic e();
             ...
       }
函数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成员,以供其后的调用:
       public class PinyinDecoderServiceConn ection implements ServiceConnection {
             public void onServiceConnected(ComponentName name, IBinder service) {
                   mDecInfo.mIPinyinDecoderService = IPinyinDecoderService.Stub
                               .asInterface(service);
             }
             ....
       }

mDecInfo.mIPinyinDecoderService的声明如下:
       public class DecodingInfo {
             ......
            
             private IPinyinDecoderService mIPinyinDecoderService;
             ......
       }

现在再来看在输入法(客户端进程)调用bindService时,服务端进程启动的详细过程:
因为第1个参数Intent对象的类名在调用前被设成PinyinDecoderService.class,所以系统进程响应bindService时,如果服务未运行时首先调用PinyinDecoderService的onCreate()
       @Override
       public void onCreate() {
             super.onCreate();
             mUsr_dict_file = getFileStreamPath("usr_dict.dat").getPath();
             ......
             initPinyinEngine();
       }
接下来调用PinyinDecoderService的onBind()函数,并把返回的binder对象传给前面说过的ServiceConnection.onServiceConnected():
       @Override
       public IBinder onBind(Intent intent) {
             return mBinder;
       }

辅助的初始化函数initPinyinEngine()首先打算系统静态字典资源文件res/raw/dict_pinyin.dat,然后把用户字典名写到一个byte数组里,最后把静态字典文件信息与用户字典名作为参数,调用本地c/c++函数nativeImOpenDecoderFd()。

onBind函数返回的binder对象,就是
private final IPinyinDecoderService.Stub mBinder = new IPinyinDecoderService.Stub() {
       ....
};

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值