他很可能使用的是Android接口定义语言(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类活动:
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();
}
};
谢谢我会给它一个镜头。但正如我上面提到的那样,有一个[Local Service Sample](http://developer.android.com/reference/android/app/Service.html#LocalServiceSample)将ServiceConnection()和IBinder传递给bindService()和我不认为它使用AIDL。也许@mnish正在使用AIDL,正如你所说,存根的原因。 – Marie
添加样品做魔术的部分 – Tosa
对不起,我不明白。你补充了什么?本地服务示例的LocalBinder?不,已经有了。 mBoundService?没有了。对不起,因为太钝了,但我不明白你的意思“添加了样本做魔术的部分。”你有没有编辑你的答案,我只是没有看到它? – Marie