Android IntentService providing data back to Activity
I have a activity (ListActivity) in which if a user click on a button, I kick off an intent to a "IntentService" which basically makes a REST call to a web service and gathers data. My problem is that once the service is done, i populate list of items in the Activity which is just a static var. However, I am having tr开发者_高级运维ouble kicking off listadaptor to ask it to refresh the view as the data has changed. Can someone tell me how can i "notify" the activity to refresh as the IntentService has completed it task?
If you're making a REST call, I would suggest you use IntentService instead of an AsyncTask. AsyncTask looks nice and useful, but it needs much care not to leak the context and return the result to an Activity that might be as well long gone, which makes it not that easy to properly use.
Please watch this presentation for very useful guidelines:
http://www.youtube.com/watch?v=xHXn3Kg2IQE
EDIT:
As for sending back the result, you can include a parcelable ResultReceiver
in your Intent that you start the IntentService with, and notify the Activity (and handle the adapter refresh there) using .send()
As for refreshing the ListAdapter, it really depends on which kind you are using. If it's a simple adapter, you could swap it using
getListView().setAdapter(adapter)
but you'd be much better with CursorAdapter
and simply calling
context.getContentResolver().notifyChange(uri, null);
Where context is the Context of your IntentService.
Hope that helps!
精彩评论