0

I want to read all contacts and use those in my application. i read all related topics but my implementation is very ِdifferent...

i want to Achieve this form :

see Image

1-user can choose specific contacts

2- Theirs information must add in application listview

3-and user can Choose silent/Normal status for contacts that he choosen.

How i can get contact based on this scenario ?

Hossein
  • 77
  • 1
  • 1
  • 11

2 Answers2

0

use explicit intent and startActivityForResult() for getting contacts

static final int PICK_CONTACT_REQUEST = 1;  // The request code
...
private void pickContact() {
    Intent pickContactIntent = new Intent(Intent.ACTION_PICK, Uri.parse("content://contacts"));
    pickContactIntent.setType(Phone.CONTENT_TYPE); // Show user only contacts w/ phone numbers
    startActivityForResult(pickContactIntent, PICK_CONTACT_REQUEST);
}

===============================

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // Check which request it is that we're responding to
    if (requestCode == PICK_CONTACT_REQUEST) {
        // Make sure the request was successful
        if (resultCode == RESULT_OK) {
            // Get the URI that points to the selected contact
            Uri contactUri = data.getData();
            // We only need the NUMBER column, because there will be only one row in the result
            String[] projection = {Phone.NUMBER};

            // Perform the query on the contact to get the NUMBER column
            // We don't need a selection or sort order (there's only one result for the given URI)
            // CAUTION: The query() method should be called from a separate thread to avoid blocking
            // your app's UI thread. (For simplicity of the sample, this code doesn't do that.)
            // Consider using CursorLoader to perform the query.
            Cursor cursor = getContentResolver()
                    .query(contactUri, projection, null, null, null);
            cursor.moveToFirst();

            // Retrieve the phone number from the NUMBER column
            int column = cursor.getColumnIndex(Phone.NUMBER);
            String number = cursor.getString(column);

            // Do something with the phone number...
        }
    }
}

Then add details to your applications listview.do as you need to

Akash kv
  • 429
  • 4
  • 13
0

Don't know if you figure this out after a year but here's step 1:

private static final int CONTACT_PICKER_RESULT = 1001;
...
    public void methodname() {

    // By using ContactsContract.CommonDataKinds.Phone.CONTENT_URI, the user is
    // presented with a list of contacts, with one entry per phone number.
    // The selected contact is guaranteed to have a name and phone number.

    Intent contactPickerIntent = new Intent(Intent.ACTION_PICK, ContactsContract.CommonDataKinds.Phone.CONTENT_URI);
    startActivityForResult(contactPickerIntent, CONTACT_PICKER_RESULT);
    return true;
    }
Martin Sing
  • 1,247
  • 10
  • 15