質問

I want to display the native android contacts list and then once the user picks a contact, i would like to move to the next native screen where it shows the details of the contact (like phone numbers, email, etc). Now once the user selects either an email address or a phone number, I would then like to get that data back to my original activity and do further processing.

I know that I could use

startActivityForResult(new Intent(Intent.ACTION_PICK, Contacts.CONTENT_URI), REQUEST_CODE_PICK_CONTACT);

and then myself parse the contact using getContentResolver().query() and then store each of the required fields. Then finally display on a separate activity as contact details and then once the user chooses any one of the phone numbers or emails, then i use that to do further processing.

I tried to do the following inside onActivityResult():

Uri contactData = data.getData();
                Intent intent = new Intent(Intent.ACTION_PICK, contactData);
                startActivityForResult(intent, REQUEST_CODE_PICK_CONTACT_DETAIL);

This displays the details of the contact but when the user selects a particular number, android calls it instead of passing it back to me in the previous activity.

But I would like to use the native screens if possible and it would require a lot less parsing and no extra activities/fragments.

Could someone suggest how to go about doing this?

Thanks

役に立ちましたか?

解決

You can get contact details from phone contact doing this way

 Uri uri = data.getData();
    String[] projection    = new String[] {ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
            ContactsContract.CommonDataKinds.Phone.NUMBER};

    Cursor people = getActivity().getContentResolver().query(uri, projection, null, null, null);

    int indexName = people.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
    int indexNumber = people.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
  int type = people.getColumnIndex(ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE);

    people.moveToFirst();
    do {
        String name   = people.getString(indexName);
        String number = people.getString(indexNumber);
        System.out.println(name+number);

    } while (people.moveToNext());

他のヒント

This might help:

 Uri contactData = data.getData();
            ContentResolver cr = getContentResolver();
            Cursor c = cr.query(contactData, null, null, null, null);
            if (c.moveToFirst()) {
                String id = c
                        .getString(c
                                .getColumnIndexOrThrow(ContactsContract.Contacts._ID));

                String hasPhone = c
                        .getString(c
                                .getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));

                if (hasPhone.equalsIgnoreCase("1")) {
                    Cursor phones = getContentResolver()
                            .query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                                    null,
                                    ContactsContract.CommonDataKinds.Phone.CONTACT_ID
                                            + " = " + id, null, null);
                    phones.moveToFirst();
                    String cNumber = phones
                            .getString(phones
                                    .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                    Toast.makeText(getApplicationContext(), cNumber,
                            Toast.LENGTH_SHORT).show();

                    String nameContact = c
                            .getString(c
                                    .getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME));

            // contact number = cNumber
            //contact name = nameContact
}
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top