This is how I went about handling this problem. the content provider itself never accesses the network in the main request thread. in stead, it,
- looks for a cached result to the query
- if there is a cached result, return that immediately
- if there is no cached result, start a worker thread to find the result, and return empty results
when the worker thread from #3 is complete, it broadcasts an intent. this is received by the activity hosting the search, triggering it to re-submit the same search. this time when the search query gets to the content provider, the result is cached, and returned immediately.
the only complexity to this was how to force the activity to re-submit the search without user interaction. the solution was different depending on the version of android,
public void onReceive(Context context, Intent intent) {
String query = intent.getStringExtra(SearchManager.QUERY);
if (query == null) {
return;
}
// if user has typed something new, ignore
if (!query.equals(searchQuery)) {
return;
}
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.HONEYCOMB) {
SearchView searchView = (SearchView) searchMenuItem.getActionView();
searchView.setQuery(query, false);
} else {
SearchManager sm = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
ComponentName cm = FolderActivity.this.getComponentName();
sm.startSearch(query, false, cm, null, false);
}
}