我遇到了远程服务问题。在我启动并绑定(bind)我的服务后,我想要/需要访问它的功能。如果我使用按钮和 onclick Controller ,就像 http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/app/RemoteServiceBinding.html 中那样,那么效果很好。
但实际上,我需要在绑定(bind)服务后直接访问服务功能,例如:
Intent Intent = new Intent(IwlService.class.getName()); bindService( Intent ,serviceConnection,Context.BIND_AUTO_CREATE);
String.valueOf(wlService.getDataFromService());//这将使应用程序崩溃!!!
如果我在 onClickListener 中访问服务函数“getDataFromService()”,它就可以正常工作。
绑定(bind)/启动服务后如何直接访问服务功能?
最佳答案
but actually, i need to access the services functions DIRECTLY after binding the service, like:
这是不可能的。例如,该服务可能需要启动。对于远程服务,这将需要几百毫秒。
how do i access the services functions directly after binding/starting the service?
你不知道。您重写代码以从 ServiceConnection
对象中的 onServiceConnected()
方法开始访问您的服务。在此之前的任何时间,您的服务都尚未准备好。
关于java - 访问远程服务功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2719720/