Skip to content Skip to sidebar Skip to footer

Android Static Method Plots Background Thread Data Nicely In Real-time, But Is It A Good Solution?

I've been asking a series of evolving questions regarding my Android project that continually plots Bluetooth data in real-time. And I haven't done a very good job of asking questi

Solution 1:

I found the method in the background thread that writes BluetoothData to the Logcat. So I am leveraging this method to call a static method, plotData(BluetoothData), in the Plotting Activity. It works nicely plotting the incoming BluetoothData in real-time.

This story does not add up, or BluetoothData is misnamed.

In Android, to plot to the screen, you need an Activity instance, with whatever widget(s) you are plotting on. A plotData() method that does the plotting can be static, but somehow it needs the Activityinstance. So, one of the following must be true:

  • BluetoothData contains an Activity instance (and hence is misnamed), or
  • plotData() takes more than the one parameter you have indicated, or
  • you are holding onto an Activity instance in a static data member (BAD BAD BAD BAD BAD), or
  • plotData() is not a static method, so you are actually calling it on an Activity instance

But, since you repeatedly decline to provide the source code, despite having asked several questions about the code, it is impossible for us to say which of these is really your problem.

Is it not thread safe? Do I have a deadlock issue? Is my solution a fragile one? Am I circumventing the Android OS? Am I lucky that it's working at all? Or is there a proper way to extend the existing design?

The first five of these have different answers depending upon which of the four bullets above reflects reality, and I really do not feel like writing out a 20-cell grid of answers. Your last question assumes that you have actually explained your "design", which you have not.


UPDATE

Some comments based upon the substantial revision to the question:

I would appreciate an explanation as to why plotData() seemingly must be static in order to work.

It probably doesn't have to be static.

Is my current solution an OK one?

Probably not.

Static methods are rarely a problem on their own. Static data is frequently a problem, due to lack of thread safety, memory leaks, and the like. Hence, your objective is to minimize or eliminate all static data members.

There are at least four static data members at play here, perhaps more. One is strData. This isn't too bad, in that you reset the static data member to a fresh StringBuffer on each plotData() call, so your memory leak is modest. However, if plotData() somehow were to be called on multiple threads simultaneously -- and, since I don't know your threading model, that's at least possible -- you will have problems, since you have no synchronization.

However, the far bigger problem is represented by the plotHistory, plotHistorySeries, and plotHistoryPlot static data members are. I have no idea what these objects are. However, by their name and your overall objective, it would appear that redraw() actually draws to the screen, which means that plotHistoryPlot is or holds some subclass of View that is the thing being plotted upon. This means you violated a cardinal rule of Android development:

Never put something that references a transient Context in a static data member

Here, an Activity represents a transient Context, "transient" because activities do go away, Context because it inherits from Context. Your statically-referenced View holds a reference back to its Activity. Hence, this Activity can never be garbage collected, which is bad for business, let alone any possible thread-safety issues.

Again, this is an educated guess, since I don't know what those static data members really are.

Post a Comment for "Android Static Method Plots Background Thread Data Nicely In Real-time, But Is It A Good Solution?"