Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid Service updates table; How to get corresponding ListView to update?
    text
    copied!<p>Let's say I have a long-running service that monitors a network of external devices. Whenever it discovers a new device in the network, it adds a row to a table in a local SQLite database, and whenever it detects a change of state on a known device, it updates the corresponding row in the table. (BTW, there's no way to predict how frequently or infrequently these discoveries and updates will occur -- the external network marches to its own drummer!)</p> <p>Now, let's also say I have an Activity (in the same process) that includes a ListView backed by the above-mentioned table. At the precise moment that the ListView is initially populated, it's of course in sync with the backing table -- but a moment (or two, or three) afterwards, because of changes in the external device network, the table changes, courtesy of the Service.</p> <p>What's the best way for the Service to inform the Activity that it needs to refresh the ListView, i.e., call requery() on the Cursor, and then call notifyDataSetChanged() on the adapter?</p> <p>I've seen a couple of posts here on SO in which the approach seemed to be "register a broadcastReceiver on the Activity, and send a broadcast Intent from the Service" -- but that seems a bit overkill for a local Service. Isn't there some simpler way? </p> <p>For instance, I'm thinking that since I can get a handle to the local Service object via a Binder object, I might be able to register some kind of callback to the Activity, to be called by the service when needed. Does this sound like it would work? (Or would I be entering blind alley?)</p> <p>Looking forward to your insights.</p> <p>M</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload