安卓:当活动被关闭服务站 [英] Android: Service stops when Activity is closed

查看:123
本文介绍了安卓:当活动被关闭服务站的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我理解正确的话, bindService()与BIND_AUTO_CREATE将启动一个服务,并不会死,直到所有绑定都unbinded。

If I understand it correctly, bindService() with BIND_AUTO_CREATE will start a service and will not die until all bindings are all unbinded.

但是,如果我bindService(BIND_AUTO_CREATE)在的onCreate()和回击按钮关闭 活动时,服务调用的onDestroy()和也死亡。

But if I bindService(BIND_AUTO_CREATE) at onCreate() and hit back button to close the activity, the service calls onDestroy() and dies also.

我不叫解除绑定()在任何时候。 那么,这意味着当活动得到了破坏,结合得到了也毁坏和服务也得到破坏?

I don't call unbind() at anytime. So is that mean when the Activity got destroyed, the binding got destroyed also and the service gets destroyed also?

如果我想的服务将始终运行,同时在活动开始时, 我想绑定,这样我可以访问服务?

What if I want the service to be always running, at the same time when the activity starts I want to bind it so that I can access the service?

如果我调用StartService(),然后bindService()在的onCreate(),它会重新启动在每次发射活动的服务。 (我不想要的)。

If I call StartService() and then bindService() at onCreate(), it will restart the service at every launch of Activity. (Which I don't want).

所以,我可以,我开始服一次,然后下一次我绑定推出的活动?

So I could I start service once and then bind next time I launch the activity?

推荐答案

您需要使用 startService 这样的服务没有停止时,结合它的活动破坏。

You need to use startService so that the service is not stopped when the activity that binds to it is destroyed.

startService 将确保即使你的活动被破坏(由于内存的限制,除非它被停止)服务正在运行。当你调用 startService ONSTART 方法被调用的服务。它不会重新启动(停止并重新启动)的服务。因此,这取决于你如何实现对服务的 ONSTART 方法。该服务的的onCreate ,然而,这只是调用一次,所以你可能要初始化的东西在那里。

startService will make sure that the service is running even if your activity is destroyed (unless it is stopped because of memory constraints). When you call startService, the onStart method is called on the service. It doesn't restart (terminate and start again) the service. So it depends on how you implement the onStart method on the Service. The Service's onCreate, however, is only called once, so you may want to initialize things there.

这篇关于安卓:当活动被关闭服务站的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆