????3.3 ???-Service
????ServiceA.class??A???????????????????????????????????????????(??????????????????)????????????????????ServiceB.class??ServiceC.class????????????????????????????????????????????????????BaseService?????????????????????????????????????????AndroidManifest.xml????????????????Service??
???????????£?
public class BaseService extends Service {
@Nullable
@Override
public IBinder onBind(Intent intent) {
return new InstanceTransferImp();
}
}
public class ServiceA extends BaseService {}
public class ServiceB extends BaseService {}
public class ServiceC extends BaseService {}
????3.4 ???-InstanceReceiver
????InstanceReceiver.class?????ServiceConnection?????????????????Binder?????????InstanceTransfer????????????AIDL????????????????????????????????????
????????
@Override
public void onServiceConnected(ComponentName name?? IBinder service){
Log.i(TAG?? "[onServiceConnected]" + name);
try {
/** ????????????(????)???????????? */
InstanceTransfer.Stub.asInterface(service).transfer();
} catch (Exception e) {
Log.e(TAG?? "[onServiceConnected][exception when transfer instance]" + name?? e);
}
}
@Override
public void onServiceDisconnected(ComponentName name) {
/** ????????????????????????д????????? */
Log.e(TAG?? "[onServiceDisconnected][exception when service disconnected]" + name);
}
InstanceTransfer????壺
interface InstanceTransfer {
InstanceCarrier transfer();
}
????3.5 InstanceCarrier
????????e???????InstanceCarrier?????InstanceCarrier??????????????????Parcelable????????????飬???????????????л?(Stub)??????л?(Proxy)????????
???????????????????
private static final String TAG = "InstanceCarrier";
private static final int PROCESS_A = 1;
private static final int PROCESS_B = 2;
private static final int PROCESS_C = 3;
/**
* ?????????????IBinder????????????
* @param dest
* @param flags
*/
@Override
public void writeToParcel(Parcel dest?? int flags) {
if (ProcessUtils.isProcessA()) {
dest.writeInt(PROCESS_A);
dest.writeStrongInterface(SingletonAImp.getInstance());
Log.i(TAG?? String.format(
"[write][PROCESS_A][processCode=%s]"?? PROCESS_A));
}else if (ProcessUtils.isProcessB()) {
dest.writeInt(PROCESS_B);
dest.writeStrongInterface(SingletonBImp.getInstance());
Log.i(TAG?? String.format(
"[write][PROCESS_B][processCode=%s]"?? PROCESS_B));
}else if (ProcessUtils.isProcessC()) {
dest.writeInt(PROCESS_C);
dest.writeStrongInterface(SingletonCImp.getInstance());
Log.i(TAG?? String.format(
"[write][PROCESS_C][processCode=%s]"?? PROCESS_C));
}
}
/**
* ???????????????????IBinder?????????????
* @param in
*/
protected InstanceCarrier(Parcel in) {
int processCode = in.readInt();
switch (processCode) {
case PROCESS_A:
SingletonAImp.INSTANCE =
SingletonA.Stub.asInterface(in.readStrongBinder());
Log.i(TAG?? String.format(
"[read][PROCESS_A][processCode=%s]"?? processCode));
break;
case PROCESS_B:
SingletonBImp.INSTANCE =
SingletonB.Stub.asInterface(in.readStrongBinder());
Log.i(TAG?? String.format(
"[read][PROCESS_B][processCode=%s]"?? processCode));
break;
case PROCESS_C:
SingletonCImp.INSTANCE =
SingletonC.Stub.asInterface(in.readStrongBinder());
Log.i(TAG?? String.format(
"[read][PROCESS_C][processCode=%s]"?? processCode));
break;
default:
Log.w(TAG?? String.format(
"[unknown][processCode=%s]"?? processCode));
}
}
public InstanceCarrier() {}
@Override
public int describeContents() {
return 0;
}
public static final Creator<InstanceCarrier> CREATOR = new Creator<InstanceCarrier>() {
@Override
public InstanceCarrier createFromParcel(Parcel in) {
return new InstanceCarrier(in);
}
@Override
public InstanceCarrier[] newArray(int size) {
return new InstanceCarrier[size];
}
};
???????????????????????????á?
????????????μ????????????
???????嵥????AIDL
??????????
??????InstanceCarrier????????л???????л??????д???
??????????????????????????????????BaseService????????
??????????????????????????????AIDL????????????μ????
????????????????????
????????????????????????????????AIDL(Android IPC???????)?????????????????????????Bundle????
??????????÷??????????????????е?????????????????(??????????????????Binder???)????????????????????????????????????????????????е?????????????????????????????
?????????????????д????????????????????????κ??????????????????????????????????????????????????????
????Android??IPC???????????????Android??IPC??????1M????棬?????????????????????????????????????????(?????????????????????????????????????????????????????????1M?????)????
????AIDL????????????(??????...)