Aug 23 | 6:25 PM |
Mark M. | has entered the room |
Mark M. | turned on guest access |
Frank S. | has entered the room |
Frank S. |
Hi Mark
|
Aug 23 | 6:30 PM |
Mark M. |
howdy, Frank
|
Frank S. |
I need some help understanding how to get UI
updates from a service to the UI Thread
|
Frank S. |
I have a service that needs to send out updates
say every second
|
Mark M. |
why?
|
Frank S. |
it is monitoring "real-time" data.
|
Frank S. |
like heart-rate
|
Mark M. |
ok
|
Mark M. |
is the issue getting the data to an activity?
|
Frank S. |
I had it broadcasting, but I think that was
overkill. And then I tried Handlers, but I still don't like that
|
Mark M. |
you can use callbacks
|
Mark M. |
(a.k.a., listener objects)
|
Frank S. |
ok. What if there were several activities (ie in a
tabhost) that needed to do the same exact thing
|
Frank S. |
they all have a common "hud" on each tab
|
Mark M. |
well, I wouldn't have several activities in a
TabHost, but that's a separate issue
|
Mark M. |
either have the TabHost register the listener, or
have all interested tabs register listeners
|
Aug 23 | 6:35 PM |
Frank S. |
I would like to use the TabHost, but was having
issues including other views in the same XML file as the tabhost. It was
ignoring all of them
|
Frank S. |
it would ignore everything besides the "tabs" and
the "content frame"
|
Mark M. |
you would need to have the TabHost be inside some
other container (e.g., RelativeLayout) and have the non-tab-widgets be
part of the outer container
|
Frank S. |
ok. I will make sure to try that again
|
Frank S. |
so when the listener gets an update say
sharedpreferenceupdate. I can just schedule for the UI thread to redraw
my HudView using the new sharedpreference values
|
Mark M. |
well, I wouldn't use shared preferences as the
conduit of the data
|
Mark M. |
Step #1: Have the service implement
registerSomethingListener() and unregisterSomethingListener()
|
Frank S. |
so bundle them up in an intent
|
Mark M. |
Step #2: Have the service hold onto a collection
of those listeners
|
Frank S. |
ok
|
Mark M. |
Step #3: have the service iterate over the
collection calling a method on the listeners when the event occurs
|
Mark M. |
Step #4: have the activities call register...()
and unregister...() when appropriate
|
Mark M. |
you'll see an example of this pattern in _Android
Programming Tutorials_
|
Aug 23 | 6:40 PM |
Mark M. |
specifically tutorials 17 and 18
|
Frank S. |
Ok. Thanks I will look over them. What type of
application would you want to use Handlers or something like AIDL
|
Mark M. |
Handlers are orthogonal to your problem
|
Mark M. |
Handlers are one way to get control from a
background thread to the main application thread
|
Mark M. |
your activity might use a Handler to have the
listener update the UI
|
Mark M. |
I usually only use AIDL for remote services, where
the service and its clients are in separate applications
|
Frank S. |
ahhh. That helps me make sense of things. I was
trying to use RemoteViews to update and it was messing me all up.
|
Mark M. |
RemoteViews are mostly for app widgets, and
secondarily for customized entries in the notification drawer
|
Aug 23 | 6:45 PM |
Frank S. |
Yes. I was trying to design this thing where the
"hud" in the activities could be used an app widget
|
Frank S. |
but I think that's a whole different problem that I
will have to face later after I get the activities in the tabs working
|
Frank S. |
So we have been teaching a class on Android
Programming at Florida State University. Would you mind if I used some
of your examples in class and in exchange we will put your ware
subscription as the "required text"
|
Aug 23 | 6:50 PM |
Mark M. |
if the course is listed in the FSU course catalog
|
Frank S. |
here is a link to the page I created last semester
ww2.cs.fsu.edu/~sposaro/mobile
|
Mark M. |
then I can give you Warescriptions for your
students for free
|
Frank S. |
yes. It is in the course catalog
|
Frank S. |
it it listed as CIS4930: Mobile Programming
|
Frank S. |
typically we only have 1 class in the spring with
around 30 computer science students
|
Mark M. |
I'm confused -- I see lots of CIS4930 classes
(multi-core programming, systems & network administration)
|
Frank S. |
yeah. That's a generic title
|
Mark M. |
oh, wait, some of those are past years
|
Frank S. |
I suppose if the class sticks around for another
year then we will get our unique listing
|
Mark M. |
so are you running the CIS4930 Mobile Programming
one this fall?
|
Mark M. |
or in the spring?
|
Frank S. |
no. It will be in the spring
|
Mark M. |
OK
|
Mark M. |
when enrollment settles out, send me an email
reminding me of all of this, along with the count of students, and I can
send you Warescription coupons for them
|
Frank S. |
So when the time comes. I will just contact you
with more information then
|
Frank S. |
thank you
|
Mark M. |
in terms of the sample code, that's all Apache 2
licensed, so you can do with it what you want
|
Aug 23 | 7:15 PM |
Frank S. |
Hey Mark. 1 more question before you go. I just
looked at those examples. How would your store the data that the service
generates for the View update
|
Aug 23 | 7:20 PM |
Mark M. |
what do you mean by "store"?
|
Frank S. |
getting the data values to the UI to update
|
Mark M. |
ummm...an object?
|
Frank S. |
not in a shared preference because of the
frequency
|
Mark M. |
oh, you mean you want to persist the data?
|
Mark M. |
if the data is updated every second, I don't know
why you need to persist it
|
Mark M. |
just put it in an object
|
Mark M. |
pass the object to the listener
|
Mark M. |
listener passes the object to the activity
|
Mark M. |
activity does really cool stuff
|
Mark M. |
:-)
|
Frank S. |
lol. Will I have to have a handler in the activity
to actually scheduel the UI update
|
Mark M. |
I usually use post(), available on View (so, any
widget or container), to schedule a Runnable to do the work
|
Mark M. |
or, runOnUiThread(), available on Activity
|
Mark M. |
but you can use a Handler -- there's an example of
that in _The Busy Coder's Guide to Android Development_
|
Frank S. |
I have runonuithread now, but I think using post
will be better since I'm creating a custom view for the hud
|
Mark M. |
all runOnUiThread() does is see if you're
presently running on the main application thread, and if not, it does
the same stuff as post()
|
Aug 23 | 7:25 PM |
Mark M. |
Hence, I don't know that it's worth changing
|
Frank S. |
I am collecting HeartRate data and wanted to offer
it along with calories burned etc. So that was the reason for
persistence. But I want to log a history of it so I'll worry about
filling in a content provider later on
|
Frank S. |
well thanks for the help. I have been very happy
with me Commonsware subscription :)
|
Mark M. |
glad you like it
|
Mark M. |
have a pleasant day/evening/whatever!
|
Frank S. |
lol. I'm on the east cost too
|
Frank S. |
goodbye
|
Frank S. | has left the room |
Mark M. | turned off guest access |