网络服务需要返回回调
我有一个网络服务,我想将其用作服务。它是一个本地服务,因为一旦应用程序进程终止并且没有其他应用程序需要访问它,它就不再有效。(或者应该......)。
我正在考虑是否使用带有对该类的本地引用的 IBinder 接口,但暂时决定不这样做。
我有以下问题:
- 如果服务终止,并且我知道它可以在请求处理期间终止,这对我来说是一个问题,首先我看到它,进程不会终止,直到网络请求返回(然后线程优雅地终止),除非在进程中使用kill -9...那么我不确定android如何处理连接。我不确定我应该在这里采取什么方法。(即使这是本地线程而不是服务,这也是事实......)
- 如果我希望服务监听回调并在网络连接后调用它处理已完成,我遇到了问题,无法使用意图传递任何实例。所以我需要一些其他的解决方案,所有我认为听起来很糟糕的解决方案:A.使用 IBinder 来获取网络服务类的实例,然后我可以调用它的方法之一并传递一个实例,这将起作用,因为它们都可以在同一个进程中运行,但是要求我使用异步方式来获取不太适合我的网络实例。 B. 在我可以访问的服务中使用静态成员,那么我需要该服务做什么? C. 使用 Intent 仅向服务发送参数,服务将从中组成一个请求并将其放入队列中,然后完成后将使用 Intent 发送响应,该响应将封装响应(可能很长!)还将包含作为字符串的调用类的名称,以便所有接收器都知道它是否适合它们 - 将数据封装在 Intent 中并在所有接收器中搜索正确的接收器以获得响应的开销很大。
我不想将该服务用作本地运行的简单线程,因为我担心如果我在根活动中运行它,我将不得不使用静态容器,这样它将在每个活动中可见,并且如果根活动由于某种原因被破坏,即使我开始新任务并且该过程仍然存在,它也会带走所有服务......
有人对如何处理这个事情有一些好主意吗?
I have a Networking service that i want to use as a Service. It's a local Service since it's not valid anymore once the application process is dead and no Other applications need to access it.(or should...).
I was pondering if to use the IBinder interface with local reference to the class and decided not to, for the moment.
I have the following issues:
- if the service dies, and i know it can during request processing, it's an issue for me, first i've seen it, the process wont die until the net request returns (then the thread dies gracefully), unless kill -9 is used on the process... then i'm not sure what android does with the connections. I'm not sure what's the approach i should take here.(it will be true though even if this was a local thread and not a service...)
- if i want the service to listen on a callback and call it once the network processing is done, i'm in a problem, no instances can be passed on using Intents. So i need some other solutions, all the ones i though of sounds bad to me: A. use IBinder to get instance of the network service class then i can call one of it's methods and pass on an instance, this will work since they all run in the same process, BUT requires me to use Async way to get a Network instance which is not so suitable for me. B. Use static member in the Service i can access, then what to i need the service for ? C. use intent to send parameters only to the service, the service will compose a Request out of it and put it in the queue, then once done will send a response using intent which will encapsulate the response (which might be long!) and will also contain the name of the calling class as a string so all the Receivers will know if it's for them or not - BIG overhead of encapsulating data in Intent and search in all the receivers for the right one to get the response.
I don't want to use the service as a local running simple thread since i'm afraid if i'll run it in the root activity i will have to use static container so it will be visible in each activity and if the root will be destroyed for some reason it will take all the service with it, even if i start new task and the process is still alive...
Anyone got some nice ideas on how to approach this thing ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
最终我放弃了这项服务。
不使用该服务的原因是扩展应用程序对象并将网络类保留为该应用程序对象的成员,它在创建应用程序时启动,在创建任何活动之前,并在应用程序绘制它之前关闭最后一口气。我知道应用程序 onTerminate 可能不会始终被调用,但是如果有人在我的应用程序上调用kill -9 或等效函数,并且进程将用它杀死应用程序,那么我已经准备好了,因为我的服务无论如何都会被销毁。
我放弃服务的原因是:
因此,无需使用绑定并使我的代码变得复杂,无需考虑如何启动或结束服务,如果我创建了一个服务,那么我很可能会从应用程序(或根活动)启动它),所以我认为对于我和我的应用程序来说这是最好的选择。
Eventually i gave up on the service.
The reason to not use the service But to extend Application object and keep the networking class as a member of that Application object, it is started when the application is created, before any activity is created,and it is shut down before the application draws it's last breath. I know application onTerminate might not be called at all times, but if someone will call kill -9 or equivalent on my Application and the process will die killing the application with it, i'm all set as my Service will be destroyed anyway.
The reasons i gave up a service were:
So no need to use binding and complicate my code with it, no need to think of how to start or end the service, if i'd made a service most chances i'll be starting it from the Application anyway (or the root activity), so i think for me and my app this is the best option.