mardi 5 mai 2015

Android Contact Phone, Address and Name Intent

I'm using a basic Intent to grab a contact in Android Studio. Once a contact is selected, I want to extract address (home), phone and name. I'm currently only able to get name/address or name/phone. I'm not able to get all 3 because of the intent type I believe.

The intent:

private static int PICK_CONTACT = 0;
Intent intent = new Intent(Intent.ACTION_PICK);

//this intent type lets me pull name and phone number
intent.setType(ContactsContract.Contacts.CONTENT_TYPE);

//this intent type lets me pull name and address  
intent.setType(ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_TYPE);

startActivityForResult(intent, PICK_CONTACT);

Can I set the intent type to be able to retrieve all the data I need? I obviously don't want to have to select the contact again.

I'm very sorry if this is a simple question. I'm still new with Android development :).

Edit: here are my functions for retrieving contact phone, name and address given URI. They all work with different intent types.

public String getContactNameFromURI(Uri uri){
        String contactName = null;
        // querying contact data store
        Cursor cursor = getContentResolver().query(uri, null, null, null, null);
        if (cursor.moveToFirst()) {
            // DISPLAY_NAME = The display name for the contact.
            // HAS_PHONE_NUMBER =   An indicator of whether this contact has at least one phone number.
            contactName = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
        }
        cursor.close();
        return contactName;
    }

    public String getContactPhoneFromURI(Uri uri){
        String contactNumber = null;
        String contactID = getContactIdFromURI(uri);

        // Using the contact ID now we will get contact phone number
        Cursor cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                new String[]{ContactsContract.CommonDataKinds.Phone.NUMBER},

                ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ? AND " +
                        ContactsContract.CommonDataKinds.Phone.TYPE + " = " +
                        ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE,

                new String[]{contactID},
                null);

        if (cursor.moveToFirst()) {
            contactNumber = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
        }
        cursor.close();

        return contactNumber;
    }

    public String getContactAddressFromURI(Uri uri){
        String rAddress = null;
        Cursor cursor = getContentResolver().query(uri, null, null, null, null);
        if (cursor.moveToFirst()) {
            try {
                rAddress = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.FORMATTED_ADDRESS));//ContactsContract.CommonDataKinds.StructuredPostal.FORMATTED_ADDRESS));
            }catch(Exception e){
                return "nope";
            }
        }
        cursor.close();
        return rAddress;
    }

Aucun commentaire:

Enregistrer un commentaire