AIDL単純インスタンス
アプリケーションごとに独自のプロセスがあり、プロセス間で相互にアクセスできない.これをアプリケーション砂時計(application sandboxing)といい、アプリケーションが相互にアクセスできるようにAndroidがIPC(interprocess communication protocol)を提供するIPC protocolはパッケージ(marshaling/un marshalingデータ)を整理する必要があるため複雑になる.したがって.AndroidはAIDL(Android Interface Definition Language)を提供し、javaのような文法を使用してIPC軽量級を実現し、AIDLツールがstubの作成を自動的に実現する手順:1.the AIDL interface 2を定義する.RemoteサービスのためにStub 3を実現する.remoteサービスをローカルlocal clientに提供する第一歩:the AIDL interfaceを定義する
package com.marakana;//
]AIDLinterface IAdditionService { // in, out, or inout. // Primitive ( int, boolean, etc.) in.
int add(in int value1, in int value2);}
ステップ2:remoteサービスのためのStubの実装
/**
* remote service Local client
*/
public class AdditionService extends Service {
private static final String TAG = "AdditionService";
@Override
public void onCreate() {
super.onCreate();
Log.d(TAG, "onCreate()");
}
// remote
@Override
public IBinder onBind(Intent intent) {
return new IAdditionService.Stub() {
/**
* Implementation of the add() method
*/
public int add(int value1, int value2) throws RemoteException {
Log.d(TAG, String.format("AdditionService.add(%d, %d)",value1, value2));
return value1 + value2;
}
};
}
@Override
public void onDestroy() {
super.onDestroy();
Log.d(TAG, "onDestroy()");
}
}
ステップ3:remoteサービスをローカルlocal clientに提供servcieでonBind()を実装すると、remoteサービスに接続できます.
public class AIDLDemo extends Activity {
private static final String TAG = "AIDLDemo";
IAdditionService service;
AdditionServiceConnection connection;
/**
* remote service . IBinder , Stub
* AIDL . asInterface aild
*/
class AdditionServiceConnection implements ServiceConnection {
public void onServiceConnected(ComponentName name, IBinder boundService) {
service = IAdditionService.Stub.asInterface((IBinder) boundService);
Log.d(AIDLDemo.TAG, "onServiceConnected() connected");
Toast.makeText(AIDLDemo.this, "Service connected", Toast.LENGTH_LONG)
.show();
}
public void onServiceDisconnected(ComponentName name) {
service = null;
Log.d(AIDLDemo.TAG, "onServiceDisconnected() disconnected");
Toast.makeText(AIDLDemo.this, "Service connected", Toast.LENGTH_LONG)
.show();
}
}
/** Activity Service. */
private void initService() {
connection = new AdditionServiceConnection();
Intent i = new Intent();
i.setClassName("com.marakana", com.marakana.AdditionService.class.getName());
boolean ret = bindService(i, connection, Context.BIND_AUTO_CREATE);
Log.d(TAG, "initService() bound with " + ret);
}
/** . */
private void releaseService() {
unbindService(connection);
connection = null;
Log.d(TAG, "releaseService() unbound.");
}
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
initService();
// Setup the UI
Button buttonCalc = (Button) findViewById(R.id.buttonCalc);
buttonCalc.setOnClickListener(new OnClickListener() {
TextView result = (TextView) findViewById(R.id.result);
EditText value1 = (EditText) findViewById(R.id.value1);
EditText value2 = (EditText) findViewById(R.id.value2);
public void onClick(View v) {
int v1, v2, res = -1;
v1 = Integer.parseInt(value1.getText().toString());
v2 = Integer.parseInt(value2.getText().toString());
try {
res = service.add(v1, v2);
} catch (RemoteException e) {
Log.d(AIDLDemo.TAG, "onClick failed with: " + e);
e.printStackTrace();
}
result.setText(new Integer(res).toString());
}
});
}
/** Called when the activity is about to be destroyed. */
@Override
protected void onDestroy() {
releaseService();
}
}
ソース:http://marakana.com/forums/android/examples/48.html
ソース:http://marakana.com/static/tutorials/AIDLDemo.zip