将服务绑定到 android.app.Activity 与将其绑定到 android.app.Application [英] Binding a Service to an android.app.Activity vs Binding it to an android.app.Application

查看:24
本文介绍了将服务绑定到 android.app.Activity 与将其绑定到 android.app.Application的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

将服务绑定到 android.app.Activity 与将其绑定到 android.app.Application 是否有任何根本区别.我想绑定服务到应用程序 因为我想在应用程序中保留一些全局状态/数据,而不是在我的所有活动中复制它.

Is there any fundamental difference in binding a service to an android.app.Activity vs binding it to an android.app.Application. I want to bind the service to an Application because I want to keep some global state/data in the Application instead of duplicating it in all my activities.

谢谢.

推荐答案

没有.没有根本区别.

也就是说,子类化 android.app.Application 是一个非常存储全局/状态数据的好地方.只有一个实例,所有派生自 Context 的东西都可以访问它.

That said, subclassing android.app.Application is a very good place to store global/state data. There is only one instance and everything that derives from Context has access to it.

我也确信,如果您不小心,将服务绑定到应用程序会导致一些奇怪的生命周期.我的意思是,即使您的应用程序不在视线范围内并且没有活动的活动,您的应用程序仍然可以存在,因为您的服务仍然存在.您的服务仍然存在,因为您的应用程序仍然存在.您必须根据 onDestroy 以外的其他事件手动关闭服务.

I'm also sure that binding a service to an application will result in some odd lifetimes if you aren't careful. What I mean is that even though your app is out of sight and has no activities alive, your application could still exist because your service still exists. Your service still exists because your application still exists. You would have to manually shut down the service based on some event other than onDestroy.

这篇关于将服务绑定到 android.app.Activity 与将其绑定到 android.app.Application的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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