停止一切开始服务于应用程序关闭/退出 [英] Stop All Started Services on App Close / Exit

查看:127
本文介绍了停止一切开始服务于应用程序关闭/退出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能当用户点击Home键停止所有启动服务?

我使用的:

  startService(新意图(ClassName.this,ClassName2.class));
stopService(新意图(ClassName.this,ClassName2.class));
 

这意味着我将不得不以某种方式加入'stopService()我的应用程序类7+ 我研究过这个话题,我觉得有onTerminate,但仍不能确定应如何执行。

任何帮助或暗示将AP preciated! 谢谢!

解决方案
  

是否可以停止全部开工   当用户点击主页服务   按钮?

不直接。对于初学者来说,你有没有办法知道他们pressed家。

如果您只想业务正常运行,同时活动都使用它,请考虑摆脱 startService的()。相反,使用 bindService()在需要的服务,并调用<$活动的 ONSTART()方法C $ C> unbindService()在其相应的的onStop()的方法。您可以使用 BIND_AUTO_CREATE 有需要时服务偷懒启动的,并且Android将自动停止该服务后,所有连接绑定。

Is it possible to stop all started services when the user hits the Home Button?

I use:

startService(new Intent(ClassName.this, ClassName2.class));
stopService(new Intent(ClassName.this, ClassName2.class));

This means I will have to somehow add the 'stopService()' for 7+ of my app classes I've researched this topic and I think there's 'onTerminate' but still not sure how this should be implemented.

Any help or hints would be appreciated! Thanks!

解决方案

Is it possible to stop all started services when the user hits the Home Button?

Not directly. For starters, you have no way of knowing they pressed HOME.

If you only want the service running while activities are using it, consider getting rid of startService(). Instead, use bindService() in the onStart() methods of the activities that need the service, and call unbindService() in their corresponding onStop() methods. You can use BIND_AUTO_CREATE to have the service be lazy-started when needed, and Android will automatically stop the service after all connections have been unbound.

这篇关于停止一切开始服务于应用程序关闭/退出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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