我需要一种好方法将数据从线程获取到另一个活动
我之前问过这个问题的各种形式,并得到了不同的意见。主要是,我被建议做一些阅读和实验。但无济于事。我仍然不明白所建议的任何替代解决方案。同样,我正在开发一个绘制蓝牙数据的 Android 应用程序。
我有一个蓝牙活动,显然创建了一个后台线程,该线程使用通过蓝牙连接接收的数据更新屏幕。当我启动不同/新的活动来绘制数据时,我可以看到蓝牙后台线程继续写入 Logcat。 所以我知道当我启动 Plot 活动时蓝牙后台线程仍在运行。
我的目标是绘制蓝牙后台线程不断提供的蓝牙数据。 我已经成功如下:由于这个蓝牙后台线程仍在运行,我决定使用它的 update() 方法来调用静态方法 Plot.plotData() 来绘制数据。 这有效,但我不明白为什么。 它将无限地运行而不会出现问题 - 接收和绘制蓝牙数据。
但有人告诉我,这可能不是一个好的解决方案,因为可能存在:线程安全、内存泄漏、主 UI 线程阻塞等嗯>。尽管我没有遇到任何这些问题,但由于我收到的反馈,我不愿意得出结论我的解决方案是好的。
有人建议我尝试使用 Service、AsyncTask 等,而不是从蓝牙后台线程的 update() 方法调用静态方法(我的 Plot.plotData())。但从我所做的研究来看,我不明白如何将我的解决方案更改为更合适的解决方案。
如果有人看到更合适的解决方案,请大声说出并描述它。 如果我当前的解决方案有问题,我不想继续下一步。
如果您想查看源代码,我很乐意发布。
I've asked various forms of this question before and have gotten differing opinions. Mainly, I've been advised to do some reading and experimentation. But to no avail. I still don't understand any of the alternative solutions that have been suggested. Again, I am developing an Android app that plots bluetooth data.
I've got a Bluetooth activity that apparently creates a background thread which updates the screen with data it receives over a bluetooth connection. When I launch a different/new activity to plot the data, I can see the bluetooth background thread continue to write to Logcat. So I know for a fact the bluetooth background thread is still running when I launch my Plot activity.
My goal is to plot the bluetooth data that is continually provided by the Bluetooth background thread. I have succeeded as follows: since this bluetooth background thread is still running, I decided to use its update() method to call a static method Plot.plotData() to plot the data. And this works, but I don't understand why. It will will run endlessly with out a problem - receiving and plotting bluetooth data.
But I have been advised that this is probably not a good solution due to possible: thread safety, memory leak, blocking of the main UI thread, etc. Although I have not encountered any of these problems, I am reluctant to conclude my solution is a good because of the feedback I have received.
It's been suggested that I try a Service, AsyncTask, etc instead of calling a static method, my Plot.plotData(), from the bluetooth background thread's update() method. But from the research I have done, I don't understand how to change my solution to a more appropriate one.
If anyone sees a more appropriate solution please speak up and describe it. I don't want to move forward if there is a problem with my current solution.
If you'd like to see source code I would be happy to post it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我要做的就是将 BT 数据(bkg 线程中的数据)放入 Android 服务中,该服务也管理 bkg 线程。从该服务中,您可以通过意图广播更新或让活动绑定到该服务。这样,任何知道要监听什么的活动都可以监听您的数据。
看看 http://developer.android.com/guide/topics/fundamentals /services.html 了解服务。了解基础知识后,您可以更深入地了解。
另请阅读以下内容: http://developer.android.com/resources/ articles/multitasking-android-way.html
我还想补充一点,您可能找不到“完美”或“正确”的答案,您需要查看文档并获得帮助小脏看看什么最适合你的具体问题。
What I would do, is put the BT data(what you have in a bkg thread) in a Android Service that also manages a bkg thread. From that service you could Broadcast updates though intents or let the activity bind to that service. This way any activity that knows what to listen for can listen in on your data.
Take a look at http://developer.android.com/guide/topics/fundamentals/services.html to get an idea of services. You can dive in deeper once you understand the basics.
Also read this one: http://developer.android.com/resources/articles/multitasking-android-way.html
I would also like to add, you probably won't find a "perfect" or "correct" answer, you need to take a look at the docs and get your hands a little dirty to see what best fits your particular problem.
在android中,在活动之间共享数据的常见方法(或者在您的情况下,一个活动在不同的活动中启动了一个线程)是
听起来你正在寻找一种内存中的方式来共享数据,而这根本不是 Android 的方式活动模型有效。你问你的方法有没有问题,答案肯定是有。
我建议查看 Service 或 数据库 来解决此问题并摆脱静态方法。
In android, the common ways to share data between activities (or in your case, an activity with a thread started in a different activity) is
It sounds like you're looking for an in-memory way to share data, and that's simply not the way the Android activity model works. You ask if there's a problem with your approach, and the answer is definitely yes.
I suggest looking into Service or Databases to fix this problem and get rid of your static methods.