B05_b
基於軟硬整合觀點
IPC的Proxy-Stub设计模式(b)
By 高煥堂
2 、IBinder接口的一般用途
基於軟硬整合觀點
boolean onTransact( int code,
ib.transact(1, Parcel data, Parcel reply, int
data, reply, 0); flags) {
// …….
switch( code ){
ib.transact(2, case 1 : // call Play()
data, reply, 0); case 2 : // call Stop()
}}
IBinder接口
的一般用途
一般用途
• Android的IPC框架仰赖单一的IBinder接口。
此时Client端调用IBinder接口的transact()
函数,透过IPC机制而调用到远方(Remote)
的onTransact()函数。
• 在J a层框架里,IBinder接口实现于
Binder基类,如下图:
• myActivity调用IBinder接口,执行
myBinder的onTransact()函数,可送信息
给myService去播放mp3音乐,如下图:
• myService也能送Broadcast信息给
myActivity ,将字符串显示于画面上:
// myActivity.j a
// ………
public class myActivity extends Activity implements OnClickListener {
private final int WC = LinearLayout.LayoutParams. WRAP_CONTENT;
private final int FP = LinearLayout.LayoutParams.FILL_PARENT;
private Button btn, btn2, btn3;
public TextView tv;
private IBinder ib = null;
private final String MY_S_EVENT =
new String("com.misoo.pk01.myService.MY_S_EVENT");
protected final IntentFi r fi r new IntentFi r(MY_S_EVENT);
private BroadcastReceiver receiver new myIntentReceiver();
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout. VERTICAL);
btn = new Button(this); btn.setId(101); btn.setText("play");
btn.setBackgroundResource(R.drawable.heart);
btn.setOnClickListener(this);
LinearLayout.LayoutParams par