在单例等中使用 BroadcastReceiver 的最灵活方法
我有一个单例,它存储一些有关我的应用程序用户的谨慎信息。目前,它存储用户的登录信息和用户的位置。
1) 该位置是通过服务找到的。目前,该服务直接引用我的单例以将经度和纬度填充到其中。我想使用 BroadcastReceiver 来发送单例听到并用于更新值的广播。
但是,要注册 BroadcastReceiver,我的单例中需要一个上下文。实现我想要的最巧妙的方法是什么。 BroadcastReceiver 可能不是合适的对象吗?
2) 另外,使用单例时我会遇到哪些问题?我假设 Android 可能会在任何给定时间回收该内存(这显然是不好的);那么我怎样才能防止这种情况发生呢?传递应用程序的上下文并将其存储在成员变量中会阻止这种情况吗?
Android 文档指出:“但是,静态对象的生命周期并不在您的控制之下;因此,为了遵守生命周期模型,应用程序类应该在 onCreate() 和 onTerminate( ) 应用程序类的方法”,但我不完全确定如何实现这一点。
I have a singleton which stores some prudent information about the user of my application. At the moment, it stores the user's login and the user's location.
1)
The location is found via a Service. At the moment, the Service references my singleton directly to stuff the longitude and latitude into it. I would like to use a BroadcastReceiver to send a broadcast that the singleton hears and uses to update the values, instead.
However, to register the BroadcastReceiver, I need a Context in my singleton. What is the slickest way to achieve what I'm wanting. Is BroadcastReceiver possibly not the appropriate object?
2)
Also, what problems am I looking at with using a singleton? I assume that Android will possibly reclaim this memory at any given time (which would obviously be bad); so how can I prevent that? Would passing in the application's Context and storing it in a member variable thwart this?
The Android documentation states: "But, the life cycle of a static is not well under your control; so to abide by the life-cycle model, the application class should initiate and tear down these static objects in the onCreate() and onTerminate() methods of the Application Class," but I'm not entirely sure how to accomplish this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
“最狡猾的方法”就是不做你正在做的事情。请仅从
Activity
、Service
或也许
Application
注册BroadcastReceiver
。当Activity
、Service
或Application
被销毁时,您必须取消注册此BroadcastReceiver
。你不知道。 Android 保留随时终止您的进程的权利(例如,回收内存)。 Android 2.1 及更早版本上的任务杀手将随时终止您的进程。一旦应用程序的所有组件都被销毁,Android 可能会随时回收您的进程,同时清除您的堆。等等。
只把那些你不介意失去的东西放进记忆里。
最好将您的“应用程序”视为一篮子松散耦合的组件,而不是一个整体实体。
不。
创建
Application
的子类,并通过android:name
在清单中指示 Android 应该使用它
元素上的 > 属性。The "slickest way" is to not do what you are doing. Please only register a
BroadcastReceiver
from anActivity
,Service
, or maybe anApplication
. You must unregister thisBroadcastReceiver
when theActivity
,Service
, orApplication
is destroyed.You don't. Android reserves the right to terminate your process at any time (e.g., to reclaim memory). Task killers on Android 2.1 and previous will terminate your process at any time. Once all components of your application are destroyed, Android may recycle your process at any time, clearing out your heap at the same time. And so on.
Only put things in memory that you don't mind losing.
It is best to think of your "application" as a basket of loosely-coupled components, not as a monolithic entity.
No.
Create a subclass of
Application
and indicate in the manifest that Android should use it, via theandroid:name
attribute on the<application>
element.