aidl ( 四) 各部分代码的执行进程和线程

首先看bind service时,onServiceConnected是在前台进程的主线程内,但是bindService可以不在主线程里执行

myAIDLInterface.setCallback(callback);这句话会跨进程,调用service内的setCallback,执行service的setCallback是在service的非主线程内(service的oncreate为主线程,setCallback其实是在binder线程中调用的).换句话说,myAIDLInterface.setCallback(callback);会导致后台进程执行setCallback,后台进程会开一个子线程(就是binder线程)来执行setCallback。

线程id如下:

service mainthread id1

set callbackthread id5852


接着,再看mCallback.showTime(count);  这也是跨进程的,会导致前台进程执行showTime(),这是在前台进程的非主线程里执行。也就是说,mCallback.showTime(count);  会导致前台进程启动一个子线程(binder线程)来执行showTime().日志如下,可见每次aidl调用会让前台进程启动一个线程去执行showTime,而且这个线程不是固定的,有时候是5830,有时候是5831.

05-28 22:22:52.319 13421-13432/com.example.fish.aidlclient1 D/FishLog﹕ callback3
05-28 22:22:52.319 13421-13432/com.example.fish.aidlclient1 D/FishLog﹕ process namecom.example.fish.aidlclient1
05-28 22:22:52.319 13421-13432/com.example.fish.aidlclient1 D/FishLog﹕ thread id5830
05-28 22:22:52.319 13421-13421/com.example.fish.aidlclient1 D/FishLog﹕ handleMessage
05-28 22:22:54.319 12514-13435/com.myservice D/FishLog﹕ callback
05-28 22:22:54.319 13421-13433/com.example.fish.aidlclient1 D/FishLog﹕ callback4
05-28 22:22:54.329 13421-13433/com.example.fish.aidlclient1 D/FishLog﹕ process namecom.example.fish.aidlclient1
05-28 22:22:54.329 13421-13433/com.example.fish.aidlclient1 D/FishLog﹕ thread id5831
05-28 22:22:54.339 13421-13421/com.example.fish.aidlclient1 D/FishLog﹕ handleMessage
05-28 22:22:56.349 12514-13435/com.myservice D/FishLog﹕ callback
05-28 22:22:56.349 13421-13432/com.example.fish.aidlclient1 D/FishLog﹕ callback5
05-28 22:22:56.349 13421-13432/com.example.fish.aidlclient1 D/FishLog﹕ process namecom.example.fish.aidlclient1
05-28 22:22:56.359 13421-13432/com.example.fish.aidlclient1 D/FishLog﹕ thread id5830
05-28 22:22:56.359 13421-13421/com.example.fish.aidlclient1 D/FishLog﹕ handleMessage

总结下:

aidl调用会导致被调用进程启动一个子线程来执行实际代码。

因此后台service通过aidl调用前台进程的话,要注意无法在调用函数体内执行ui更新操作,因为此函数是在非主线程中执行的

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值