安卓;使用服务订阅/取消订阅 Observable 对象,好主意吗?

发布于 2024-10-09 14:35:13 字数 340 浏览 2 评论 0原文

我有一个要求,当用户单击按钮时,需要观察一个对象。当用户再次单击该按钮时,观察就会停止。

我最初的想法是使用布局文件中的 onClick="myMethod" 将按钮绑定到活动中的方法。

调用此方法时,它将调用 startService() 来开始观察对象。我的意思是它将服务注册为观察者。

当第二次单击该按钮时,它会调用 stopService() 方法,该方法会取消将该服务注册为观察者。

我对使用服务的想法就是观察;并且后续操作将从 UI 线程中取消。这是一个合理的方法吗?或者 Android SDK 中是否有一些东西可以更轻松地做到这一点?

I have a requirement, whereby when a user clicks a button, then an object needs to be observed. When the user clicks that button again, the observation stops.

My initial thought is to have the button bound to a method in the activity using the onClick="myMethod" in the layout file.

When this method is invoke it will call startService() which starts observing the object. By this I mean it registers the service as an observer.

When the button is clicked for a second time, it calls the stopService() method which un-registers the service as an observer.

My thoughts for using a service is so the observation; and subsequent actioning is taken off the UI thread. Is this a reasonable approach or is there something in the Android SDK that could do this easier?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

恰似旧人归 2024-10-16 14:35:13

我对使用服务的想法就是观察;并且后续操作将从 UI 线程中取消。

仅当您分叉自己的线程,并且仅当“观察”支持替代线程时。从 UI 的角度来看,服务位于“后台”(它们不会直接在屏幕上绘制),但默认情况下,从线程的角度来看,它们并不位于“后台”。

这是一种合理的方法吗?Android SDK 中是否有某些东西可以更轻松地做到这一点?

鉴于您上面所写的内容,这是不可能回答的。您似乎认为该按钮很重要 - 可能并非如此。可能重要的是你正在“观察”的这个“物体”是什么……而你没有说它是什么。

如果您的服务能够在 stopService() 中可靠地注销自身,那么您不应使用此方法遇到垃圾收集问题。然而,与观察者/可观察模式相关的线程通常是可观察者的责任——在本例中是神秘的“对象”。

My thoughts for using a service is so the observation; and subsequent actioning is taken off the UI thread.

Only if you fork your own thread, and only if the "observation" supports alternative threads. Services are in the "background" from a UI standpoint (they do not draw on the screen directly), but they are not in the "background" from a threading standpoint by default.

Is this a reasonable approach or is there something in the Android SDK that could do this easier?

That is impossible to answer given what you have written above. You seem to think the button is important -- probably, it's not. What probably is important is what this "object" is that you are "observing"...and you didn't say what it is.

If your service will reliably unregister itself in stopService(), you should not run into garbage collection issues with this approach. However, threading with respect to the observer/observable pattern usually is the responsibility of the observable -- in this case, the mysterious "object".

·深蓝 2024-10-16 14:35:13

我同意墨菲的观点,但我想我应该给你一个小观点:
1. Activity被销毁时你在做什么?暂停?
2.如果你正在观察的对象正在生成你需要观察的事件,你必须考虑在所有情况下它们会发生什么,如果观察者对象随着活动而死亡,我认为你最好使用异步任务或本地线程,在任何情况下,您还必须发现该 Activity 在您的服务中被销毁,以便 GC 观察到的对象或在 Activity onDestroy 中也终止该服务。

I agree with Murphy however a small point i think i should give you:
1. what are you doing when the activity is destroyed ? paused ?
2. if the object u are observing is generating events that you need to observe you have to think what happens to them in all situations, if the observer object dies with the activity, i think u are better off with async task or local thread, in any case you will also have to discover that the activity was destroyed in your service in order to GC the observed object or kill the service too in the Activities onDestroy.

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