By 高煥堂
返回:Android底層結構系列文章
*** To Bind 到Service的不同Interface
---- 如果從Binder衍生了myBinder1、myBinder2和myBinder3等子類時,如何替myService選擇適當的myBinder介面類別呢?
---- 如果連續呼叫bindService()兩次,會bind到同一個myBinder物件。---- 如果想Bind到另一個myBinder介面類別之物件,可先unbind(),就會呼叫到onBind()函數,來決定bind到哪一個物件。
茲寫個範例:

按下<bind-1>會呼叫onBind()而綁到myBinder1的物件。再按下<IPC call>,就出現:

由於Connection1已經綁定了myBinder1的物件。繼續按下<bind-2>就還是Bind到myBinder1的物件(不會呼叫onBind()函數),按下<IPC call>,就出現:

繼續按下<bind-3>,先unbind掉myBinder1的物件,再按下<bind-3>,會呼叫onBind()而綁到myBinder3的物件。再按下<IPC call>,就出現:

茲列出程式碼
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.misoo.kx02c">
<application android:icon="@drawable/icon">
<activity android:name=".ac01" android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".myService" android:process=":remote">
<intent-filter>
<action android:name="com.misoo.kx02c.REMOTE_SERVICE" />
</intent-filter>
</service>
</application>
</manifest>
//ac01
package com.misoo.kx02c;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.graphics.Color;
import android.os.Bundle;
import android.os.IBinder;
import android.os.Parcel;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
public class ac01 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, btn4, btn5;
public TextView tv;
private IBinder ib = null;
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("bind-1");
btn.setBackgroundResource(R.drawable.heart);
btn.setOnClickListener(this);
LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(120, 50);
param.topMargin = 10;
layout.addView(btn, param);
btn2 = new Button(this);
btn2.setId(102);
btn2.setText("bind-2");
btn2.setBackgroundResource(R.drawable.heart);
btn2.setOnClickListener(this);
layout.addView(btn2, param);
btn3 = new Button(this);
btn3.setId(103);
btn3.setText("bind-3");
btn3.setBackgroundResource(R.drawable.heart);
btn3.setOnClickListener(this);
layout.addView(btn3, param);
btn4 = new Button(this);
btn4.setId(104);
btn4.setText("IPC call");
btn4.setBackgroundResource(R.drawable.heart);
btn4.setOnClickListener(this);
layout.addView(btn4, param);
btn5 = new Button(this);
btn5.setId(105);
btn5.setText("exit");
btn5.setBackgroundResource(R.drawable.heart);
btn5.setOnClickListener(this);
layout.addView(btn5, param);
tv = new TextView(this);
tv.setTextColor(Color.WHITE);
tv.setText("");
LinearLayout.LayoutParams param2 = new LinearLayout.LayoutParams(FP, WC);
param2.topMargin = 10;
layout.addView(tv, param2);
setContentView(layout);
}
private ServiceConnection mConnection = new ServiceConnection() {
public void onServiceConnected(ComponentName className, IBinder ibinder) {
ib = ibinder;
}
public void onServiceDisconnected(ComponentName className) {}
};
private ServiceConnection mConnection2 = new ServiceConnection() {
public void onServiceConnected(ComponentName className, IBinder ibinder) {
ib = ibinder;
}
public void onServiceDisconnected(ComponentName className) {}
};
private ServiceConnection mConnection3 = new ServiceConnection() {
public void onServiceConnected(ComponentName className, IBinder ibinder) {
ib = ibinder;
}
public void onServiceDisconnected(ComponentName className) {}
};
public void onClick(View v) {
switch (v.getId()) {
case 101:
tv.setText("press <IPC Call>");
Intent in = new Intent("com.misoo.kx02c.REMOTE_SERVICE");
in.putExtra("key", 1);
bindService(in, mConnection, Context.BIND_AUTO_CREATE);
break;
case 102:
tv.setText("press <IPC Call>");
in = new Intent("com.misoo.kx02c.REMOTE_SERVICE");
in.putExtra("key", 2);
bindService(in, mConnection2, Context.BIND_AUTO_CREATE);
break;
case 103:
tv.setText("press <IPC Call>");
this.unbindService(mConnection);
this.unbindService(mConnection2);
in = new Intent("com.misoo.kx02c.REMOTE_SERVICE");
in.putExtra("key", 3);
bindService(in, mConnection3, Context.BIND_AUTO_CREATE);
break;
case 104:
Parcel p = Parcel.obtain();
Parcel p_reply = Parcel.obtain();
try {
ib.transact(0, p, p_reply, 0);
tv.setText(p_reply.readString());
} catch (Exception e) {
e.printStackTrace();
}
break;
case 105:
finish();
break;
}
}
}
//myService
package com.misoo.kx02c;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.os.Parcel;
public class myService extends Service {
private IBinder mBinder1 = null;
private IBinder mBinder2 = null;
private IBinder mBinder3 = null;
@Override
public void onCreate() {
mBinder1 = new myBinder1();
mBinder2 = new myBinder2();
mBinder3 = new myBinder3();
}
@Override
public IBinder onBind(Intent intent) {
// Intent in = new Intent(this.getApplicationContext(), myActivity.class);
// in.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
// startActivity(in);
int k = intent.getIntExtra("key", 0);
if(k == 1 )
return mBinder1;
else if(k == 2)
return mBinder2;
else if(k == 3)
return mBinder3;
else return null;
}
public class myBinder1 extends Binder{
@Override
public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
throws android.os.RemoteException {
reply.writeString("myBinder1");
return true;
}
}
public class myBinder2 extends Binder{
@Override
public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
throws android.os.RemoteException{
reply.writeString("myBinder2");
return true;
}
}
public class myBinder3 extends Binder{
@Override
public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
throws android.os.RemoteException{
reply.writeString("myBinder3");
return true;
}
}
}
返回:Android底層結構系列文章
~~ end ~~