6

che sto cercando di:Prendi contatti Android con funzionalità tipo-to-filtro, limitate a un account specifico

  • visualizzare un elenco dei contatti
  • Facciamo la ricerca degli utenti attraverso di loro inserendo i termini di
  • Limita i risultati della ricerca solo a un account Google/Gmail specifico.

Questo è come costruisco l'URI per il cursore:

// User is searching for 'jo' 
String query = "jo"; 
Uri uri = Uri.withAppendedPath(Contacts.CONTENT_FILTER_URI, Uri.encode(query)); 

// Restrict the query to contacts from '[email protected]' 
Uri.Builder builder = uri.buildUpon(); 
builder.appendQueryParameter(
    ContactsContract.DIRECTORY_PARAM_KEY, String.valueOf(ContactsContract.Directory.DEFAULT)); 
builder.appendQueryParameter(ContactsContract.RawContacts.ACCOUNT_NAME, "[email protected]"); 
builder.appendQueryParameter(ContactsContract.RawContacts.ACCOUNT_TYPE, "com.google"); 

uri = builder.build(); 

Questa è l'URI finale:

content://com.android.contacts/contacts/filter/jo?directory=0&account_name=example%40gmail.com&account_type=com.google 

Attualmente, questa mostra i risultati di ricerca da tutto account al telefono.


NOTA: Se uso Contacts.CONTENT_URI invece di Contacts.CONTENT_FILTER_URI, quindi specificando le opere directory/conto come previsto, ma non posso più usare 'type-to-filtro' di ricerca di stile.

Il documentation fa Stato:

Il più importante caso d'uso per directory è ricerca. Un provider di Directory supporterà almeno Contacts.CONTENT_FILTER_URI.

Qualcuno potrebbe aiutare a far notare cosa potrei fare di sbagliato?

risposta

2

ho aggiunto il codice in Google's example for contact retrieving, e con un paio di modifiche ha funzionato perfettamente con il mio account Google for Work.

I cambiamenti che ho fatto sono state:

  • rimuovere la riga con DIRECTORY_PARAM_KEY, come io non ho trovato fare alcuna differenza
  • rimosso ContactsQuery.SELECTION dalla dichiarazione di ritorno, a causa che impedisce costanti "invisibile" contatti dalla visualizzazione.

Le modifiche sono state fatte per ContactsListFragment.java

@Override 
public Loader<Cursor> onCreateLoader(int id, Bundle args) { 
    // If this is the loader for finding contacts in the Contacts Provider 
    // (the only one supported) 
    if (id == ContactsQuery.QUERY_ID) { 
     Uri contentUri; 

     // There are two types of searches, one which displays all contacts and 
     // one which filters contacts by a search query. If mSearchTerm is set 
     // then a search query has been entered and the latter should be used. 

     if (mSearchTerm == null) { 
      // Since there's no search string, use the content URI that searches the entire 
      // Contacts table 
      contentUri = ContactsQuery.CONTENT_URI; 
     } else { 
      // Since there's a search string, use the special content Uri that searches the 
      // Contacts table. The URI consists of a base Uri and the search string. 
      contentUri = Uri.withAppendedPath(ContactsQuery.FILTER_URI, Uri.encode(mSearchTerm)); 
     } 

     // HERE COMES YOUR CODE (except the DIRECTORY_PARAM_KEY line) 
     Uri.Builder builder = contentUri.buildUpon(); 
     builder.appendQueryParameter(ContactsContract.RawContacts.ACCOUNT_NAME, "[email protected]"); 
     builder.appendQueryParameter(ContactsContract.RawContacts.ACCOUNT_TYPE, "com.google"); 
     contentUri = builder.build(); 

     // Returns a new CursorLoader for querying the Contacts table. No arguments are used 
     // for the selection clause. The search string is either encoded onto the content URI, 
     // or no contacts search string is used. The other search criteria are constants. See 
     // the ContactsQuery interface. 

     return new CursorLoader(getActivity(), 
       contentUri, 
       ContactsQuery.PROJECTION, 
       null, // I REMOVED SELECTION HERE 
       null, 
       ContactsQuery.SORT_ORDER); 
    } 

    Log.e(TAG, "onCreateLoader - incorrect ID provided (" + id + ")"); 
    return null; 
} 
+0

Grazie per la risposta, ma questo sembra avere lo stesso problema. Inizialmente, * fa * limita i contatti all'account specificato. Ma non appena inizi a digitare una query di ricerca, mostra i contatti di tutti gli account. – Gautam

+0

allora dovrei pensare che è un Android <5.0 bug, perché il molto stesso codice funziona perfettamente sul mio Lollipop Nexus, ma non sul mio Galaxy 4.2 tablet. –

+0

In realtà lo stavo eseguendo su un Moto X (testato su Android 5.0 e con il recente aggiornamento 5.1). Stesso problema. Hai funzionato correttamente anche la ricerca? – Gautam