彼はおそらく Android Interface Definition Language (AIDL)
http://developer.android.com/guide/components/aidl.htmlを使用しています。
したがって、文書化されているようなサーバー側実装のスタブを使用する必要があります。
// This is called when the connection with the service has been
// established, giving us the service object we can use to
// interact with the service. We are communicating with our
// service through an IDL interface, so get a client-side
// representation of that from the raw service object.
mService = IRemoteService.Stub.asInterface(service);
iservice 参照は、サービスをアクティビティにバインドした後に呼び出される onServiceConnected メソッドから取得されます。bindService の呼び出しには、onServiceConnected メソッドを実装する ServiceConnection が渡されます。
サービスの実装がローカルの場合、「IRemoteService.Stub.asInterface(service)」は必要ありません。サービスをローカル サービスにキャストするだけです。
ローカル サービス サンプルは、サービスでこれを行います。
public class LocalService extends Service {
private NotificationManager mNM;
// Unique Identification Number for the Notification.
// We use it on Notification start, and to cancel it.
private int NOTIFICATION = R.string.local_service_started;
/**
* Class for clients to access. Because we know this service always
* runs in the same process as its clients, we don't need to deal with
* IPC.
*/
public class LocalBinder extends Binder {
LocalService getService() {
return LocalService.this;
}
}
...
}
ServiceConnection クラスの Activity では、次のようになります。
private ServiceConnection mConnection = new ServiceConnection() {
public void onServiceConnected(ComponentName className, IBinder service) {
// This is called when the connection with the service has been
// established, giving us the service object we can use to
// interact with the service. Because we have bound to a explicit
// service that we know is running in our own process, we can
// cast its IBinder to a concrete class and directly access it.
mBoundService = ((LocalService.LocalBinder)service).getService();
// Tell the user about this for our demo.
Toast.makeText(Binding.this, R.string.local_service_connected,
Toast.LENGTH_SHORT).show();
}
public void onServiceDisconnected(ComponentName className) {
// This is called when the connection with the service has been
// unexpectedly disconnected -- that is, its process crashed.
// Because it is running in our same process, we should never
// see this happen.
mBoundService = null;
Toast.makeText(Binding.this, R.string.local_service_disconnected,
Toast.LENGTH_SHORT).show();
}
};