Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I'm not 100% sure what your sample code is supposed to do, but the following snippet should help you 'call the contacts list function, pick a contact, then return to [your] app with the contact's name'.</p> <p>There are three steps to this process.</p> <h1>1. Permissions</h1> <p>Add a permission to read contacts data to your application manifest.</p> <pre><code>&lt;uses-permission android:name="android.permission.READ_CONTACTS"/&gt; </code></pre> <h1>2. Calling the Contact Picker</h1> <p>Within your Activity, create an Intent that asks the system to find an Activity that can perform a PICK action from the items in the Contacts URI.</p> <pre><code>Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI); </code></pre> <p>Call <code>startActivityForResult</code>, passing in this Intent (and a request code integer, <code>PICK_CONTACT</code> in this example). This will cause Android to launch an Activity that's registered to support <code>ACTION_PICK</code> on the <code>People.CONTENT_URI</code>, then return to this Activity when the selection is made (or canceled).</p> <pre><code>startActivityForResult(intent, PICK_CONTACT); </code></pre> <h1>3. Listening for the Result</h1> <p>Also in your Activity, override the <code>onActivityResult</code> method to listen for the return from the 'select a contact' Activity you launched in step 2. You should check that the returned request code matches the value you're expecting, and that the result code is <code>RESULT_OK</code>.</p> <p>You can get the URI of the selected contact by calling <code>getData()</code> on the <em>data</em> Intent parameter. To get the name of the selected contact you need to use that URI to create a new query and extract the name from the returned cursor.</p> <pre><code>@Override public void onActivityResult(int reqCode, int resultCode, Intent data) { super.onActivityResult(reqCode, resultCode, data); switch (reqCode) { case (PICK_CONTACT) : if (resultCode == Activity.RESULT_OK) { Uri contactData = data.getData(); Cursor c = getContentResolver().query(contactData, null, null, null, null); if (c.moveToFirst()) { String name = c.getString(c.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)); // TODO Whatever you want to do with the selected contact name. } } break; } } </code></pre> <p><sup>Full source code: <a href="http://tutorials-android.blogspot.com/2011/11/how-to-call-android-contacts-list.html" rel="noreferrer">tutorials-android.blogspot.com (how to call android contacts list)</a>.</sup></p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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