I need a good way to get data from a thread to another activity
I've asked various forms of this question before and have gotten differin开发者_如何学Pythong 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.
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.
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
- SQLite Database - probably fairly applicable here
- Service - also applicable
- Intents - not very applicable
- Content Provider - more work than a database and of limited additional value in this case
- Files - probably not as good as a database
- Shared Preferences - not applicable
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.
精彩评论