Android Intent 和 startActivity 仍然需要调用静态方法吗?
我想我已经知道这个问题的答案了,因为这似乎有点牵强。但我正在认真寻找解决方案。
假设我有一个活动(称为蓝牙活动),它启动蓝牙后台线程,并且即使蓝牙活动消失,该线程也会永远运行 - 是的,它确实会永远运行。这个蓝牙后台线程是一个数据采集线程,持续实时收集要由活动(称为绘图活动)绘制的数据。我们可以将绘图方法称为静态方法 Plot.plotData();
我的问题是我无法判断 Plot 活动何时处于活动状态,因此我无法判断何时开始调用 Plot.data()。您可能会认为,由于 Plot.plotData() 是一个静态方法,我可以随时调用它。但事实并非如此。我必须等到 Plot 通过 Intent() 和 startActivity 实例化。否则 Plot 的 onCreate() 方法还没有被调用,并且 Plot 充满了空指针。
我该如何解决这个问题。添加一个静态 getter/setter,该静态 getter/setter 在 onCreate 运行之前最初为 false?
I think I already know the answer to this question because it seems, uh, will uh a bit far fetched. But I am seriously looking for a solution.
Suppose that I have an Activity, call it Bluetooth activity, that starts a bluetooth background thread and the thread runs forever even when the Bluetooth Activity goes away - yes it really does run forever. This bluetooth background thread is a data acquisition thread that continually collects data to be plotted by an Activity, call it Plot activity, in real-time. We can refer to the plotting method as a static method called Plot.plotData();
My problem is I can't tell when the Plot activity is active, so I cannot tell when to start calling Plot.data(). You might think that since Plot.plotData() is a static method I can call it anytime I want. But not so. I have to wait until Plot is instantiated through Intent() and startActivity. Otherwise Plot's onCreate() method has not been called and Plot is full of null pointers.
How do I solve this problem. Add a static getter/setter that is initially false until onCreate is run?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
它不会,除非您将其放入
Service
中。请不要从Activity
中泄漏线程。请注意,您有 已经被告知这一点了。
这不应该是静态方法,因为 我之前告诉过你。
这就是为什么
Plot
活动应该负责它自己的绘图。Service
中的后台线程应该单独处理数据收集。您可以安排通过Messenger
或通过bindService()
将数据从Service
获取到Plot
活动> 以及回调或其他方式。执行数据收集和显示结果的活动的服务设计在 Android 中很普遍。电子邮件客户端使用服务来收集新电子邮件。 Feed 阅读器使用服务来收集最新的 Feed 更新。等等。您只是碰巧通过蓝牙收集信息,而此概念的大多数示例将通过互联网收集信息。
因此,回顾一下:使用后台线程(或多个线程,如果需要的话)将蓝牙数据收集逻辑移动到
Service
中。安排Service
在数据到达时将其传送到Plot
活动。让Plot
活动显示结果。It will not, unless you put it in a
Service
. Do not leak threads from anActivity
, please.Please note that you have been told this already.
This should not be a static method, as I told you before.
Which is why the
Plot
activity should be responsible for its own drawing. Your background thread in yourService
should solely be handling data collection. You can arrange to get the data from theService
to thePlot
activity via aMessenger
, or viabindService()
and a callback, or other means.The design of a service doing the data collection and activities displaying the results is pervasive in Android. Email clients use services to collect new email messages. Feed readers use services to collect the latest feed updates. And so on. You just happen to be collecting information via Bluetooth, whereas most of the examples of this concept will collect information over the Internet.
So, to recap: move your Bluetooth data collection logic into a
Service
, using a background thread (or threads, plural, if needed). Arrange for theService
to deliver the data to thePlot
activity as it arrives. Have thePlot
activity display the results.