在单例等中使用 BroadcastReceiver 的最灵活方法

发布于 2024-09-24 12:23:08 字数 476 浏览 5 评论 0原文

我有一个单例,它存储一些有关我的应用程序用户的谨慎信息。目前,它存储用户的登录信息和用户的位置。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

墨落画卷 2024-10-01 12:23:08

但是,要注册 BroadcastReceiver,我的单例中需要一个上下文。什么是
实现我想要的最巧妙的方法。 BroadcastReceiver 可能不合适吗 >对象?

“最狡猾的方法”就是不做你正在做的事情。请仅从 ActivityService也许Application 注册 BroadcastReceiver 。当 ActivityServiceApplication 被销毁时,您必须取消注册此 BroadcastReceiver

我假设 Android 可能会在任何给定时间回收该内存(这将
显然很糟糕);那么我该如何防止这种情况发生呢?

你不知道。 Android 保留随时终止您的进程的权利(例如,回收内存)。 Android 2.1 及更早版本上的任务杀手将随时终止您的进程。一旦应用程序的所有组件都被销毁,Android 可能会随时回收您的进程,同时清除您的堆。等等。

只把那些你不介意失去的东西放进记忆里。

最好将您的“应用程序”视为一篮子松散耦合的组件,而不是一个整体实体。

会传入应用程序的上下文并将其存储在成员变量中
这个?

不。

Android 文档指出:“但是,静态的生命周期并不适合您的
控制;因此,为了遵守生命周期模型,应用程序类应该启动并
在 onCreate() 和 onTerminate() 方法中拆除这些静态对象
应用程序类”,但我不完全确定如何实现这一点。

创建 Application 的子类,并通过 android:name 在清单中指示 Android 应该使用它 元素上的 > 属性。

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?

The "slickest way" is to not do what you are doing. Please only register a BroadcastReceiver from an Activity, Service, or maybe an Application. You must unregister this BroadcastReceiver when the Activity, Service, or Application is destroyed.

I assume that Android will possibly reclaim this memory at any given time (which would
obviously be bad); so how can I prevent that?

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.

Would passing in the application's Context and storing it in a member variable thwart
this?

No.

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.

Create a subclass of Application and indicate in the manifest that Android should use it, via the android:name attribute on the <application> element.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文