/** * Create an {@link EntitySet} based on {@link Contacts} specified by the given query parameters. * This closes the {@link EntityIterator} when finished, so it doesn't subscribe to updates. */ public static EntitySet fromQuery( ContentResolver resolver, String selection, String[] selectionArgs, String sortOrder) { EntityIterator iterator = null; final EntitySet state = new EntitySet(); try { // Perform background query to pull contact details iterator = resolver.queryEntities(RawContacts.CONTENT_URI, selection, selectionArgs, sortOrder); while (iterator.hasNext()) { // Read all contacts into local deltas to prepare for edits final Entity before = iterator.next(); final EntityDelta entity = EntityDelta.fromBefore(before); state.add(entity); } } catch (RemoteException e) { throw new IllegalStateException("Problem querying contact details", e); } finally { if (iterator != null) { iterator.close(); } } return state; }