我需要一个很好的方式来获得一个线程的数据到另一个活动 [英] I need a good way to get data from a thread to another activity

查看:140
本文介绍了我需要一个很好的方式来获得一个线程的数据到另一个活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我问过各种形式的前这个问题,并已经得到了不同的观点。主要是,我已经建议做一些阅读和实验。但无济于事。我还是不明白,任何已提出的备选方案。同样,我开发一个的Andr​​oid应用程序,绘制蓝牙数据

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.

我有,显然会创建一个后台线程,更新屏幕数据接收通过蓝牙连接蓝牙的活动。当我启动一个不同的/新的活动,以绘制数据,我可以看到蓝牙后台线程继续写的logcat 的。 所以,我知道一个事实,蓝牙后台线程仍在运行,当我启动我的剧情活性

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.

我的目标是绘制了不断的蓝牙后台线程提供的蓝牙数据。 我已经成功了,如下所示:,因为这蓝牙后台线程仍在运行,我决定用它的update()方法来调用一个静态方法Plot.plotData()来绘制数据。 这工作,但我不明白为什么。 它将会无休止地奔跑着出了问题 - 接收并绘制蓝牙数据

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.

但我被告知,这可能不是一个很好的解决方案由于可能的:线程安全内存泄漏阻塞主UI线程,等的。虽然我还没有遇到这些问题,我不愿意来结束我的解决方案是一个很好的反馈,因为我已经收到了。

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.

它已经建议我尝试了服务,AsyncTask的,等等,而不是调用一个静态方法,我Plot.plotData(),从蓝牙后台线程的update()方法。但是,从研究,我都做了,我不明白怎么我的解决方案更改为一个更合适的。

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.

如果你想看到源$ C ​​$ C我会很乐意将它张贴。

If you'd like to see source code I would be happy to post it.

推荐答案

我会做的,就是把BT的数据(你有什么在BKG线程)在Android的服务,还管理着一个BKG线程。从该服务可以播出,虽然意图更新或让活动绑定到该服务。这种方式知道什么监听可以对数据监听任何活动。

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.

看看 http://developer.android.com/guide/主题/基本面/ services.html 获得服务的想法。您可以在更深的潜水,一旦你了解的基础知识。

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.

也读这个: 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.

这篇关于我需要一个很好的方式来获得一个线程的数据到另一个活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆