开发者

Add city and street to personal details in Android

I want to make an application to add personal details to the contacts in Android. I am able to add the name, phone number, etc. using the following code:

intent addContactIntent = new Intent(Cont开发者_如何转开发acts.Intents.Insert.ACTION, Contacts.People.CONTENT_URI);
addContactIntent.putExtra(Contacts.Intents.Insert.NAME, "myname"); 
addContactIntent.putExtra(Contacts.Intents.Insert.PHONE, "232323222"); 
addContactIntent.putExtra(Contacts.Intents.Insert.PHONE_TYPE, Contacts.Phones.TYPE_MOBILE); 

But the problem is that in address field there is city, street, etc. and here I'm not able to add data.


maybe a bit late, but hopefully still helpfull for anyone who has the same problem:

Even though Contacts.Intents.Inserts is depreciated, the ContactsContract.Intents.Insert API still doesn't provide adding city, street etc. via Intent. To solve the problem you have to do it programmatically by using the ContactsContract API. Here is my solution:

First I response to some "addContactEvent" - in my case a click on a button - with a Toast. My addContactToAddressBook Method returns the Id of a String Ressource which describes the outcome of the action.

int resultId = addContactToAddressBook(myContactObject, this);
Toast.makeText(this, getString(resultId), Toast.LENGTH_SHORT).show();

Next I add a lot of information to the contact address book like street, city, name... The information is stored in a contactobject.

public int addContactToAddressBook(MyContactObject contact, Context context){

    // is contact already in contacts??
    if(contact != null && !numberAlreadyInContacts(contact.getPhoneNumber(), context)){

        ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
        int rawContactInsertIndex = ops.size();

        // prepare Contact
        ops.add(ContentProviderOperation.newInsert(RawContacts.CONTENT_URI)
            .withValue(RawContacts.ACCOUNT_TYPE, null)
            .withValue(RawContacts.ACCOUNT_NAME, null)
            .build());

        // Insert First- and LastName
        if(contact.getFirstName() != null && contact.getLastName() != null){
            ops.add(ContentProviderOperation.newInsert(Data.CONTENT_URI)
                .withValueBackReference(Data.RAW_CONTACT_ID, rawContactInsertIndex)
                .withValue(Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE)
                .withValue(StructuredName.DISPLAY_NAME, "Lost Symbol Characters")
                .withValue(StructuredName.GIVEN_NAME, contact.getFirstName())
                .withValue(StructuredName.FAMILY_NAME, contact.getLastName())
                .build());  
        }

        // Insert Organistion and JobTitle
        if(contact.getOrganisation() != null && contact.getJobTitle() != null){
            ops.add(ContentProviderOperation.newInsert(Data.CONTENT_URI)
                .withValueBackReference(Data.RAW_CONTACT_ID, rawContactInsertIndex)
                .withValue(Data.MIMETYPE, Organization.CONTENT_ITEM_TYPE)
                .withValue(Organization.COMPANY, contact.getOrganisation())
                .withValue(Organization.TYPE, Organization.TYPE_WORK)
                .withValue(Organization.TITLE, contact.getJobTitle())
                .withValue(Organization.TYPE, Organization.TYPE_WORK)
                .build());              
        }

        // Insert Email
        if(contact.getEmailAddress() != null){
            ops.add(ContentProviderOperation.newInsert(Data.CONTENT_URI)
                .withValueBackReference(Data.RAW_CONTACT_ID, rawContactInsertIndex)
                .withValue(Data.MIMETYPE, Email.CONTENT_ITEM_TYPE)
                .withValue(Email.DATA, contact.getEmailAddress())
                .withValue(Email.TYPE, Email.TYPE_WORK)
                .build());              
        }

        // Insert PhoneNumber
        if(contact.getPhoneNumber() != null){
            ops.add(ContentProviderOperation.newInsert(Data.CONTENT_URI)
                .withValueBackReference(Data.RAW_CONTACT_ID, rawContactInsertIndex)
                .withValue(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE)
                .withValue(Phone.NUMBER, contact.getPhoneNumber())
                .withValue(Phone.TYPE, Phone.TYPE_WORK)                
                .build());              
        }

        // Insert MobileNumber
        if(contact.getMobileNumber() != null){
            ops.add(ContentProviderOperation.newInsert(Data.CONTENT_URI)
                .withValueBackReference(Data.RAW_CONTACT_ID, rawContactInsertIndex)
                .withValue(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE)
                .withValue(Phone.NUMBER, contact.getMobileNumber())
                .withValue(Phone.TYPE, Phone.TYPE_MOBILE)                
                .build());              
        }

        // Insert Address
        if(contact.getStreetAddress() != null && contact.getPostalCode() != null && contact.getLocation() != null && contact.getCountry() != null){
            ops.add(ContentProviderOperation.newInsert(Data.CONTENT_URI)
                .withValueBackReference(Data.RAW_CONTACT_ID, rawContactInsertIndex)
                .withValue(Data.MIMETYPE, StructuredPostal.CONTENT_ITEM_TYPE)
                .withValue(StructuredPostal.COUNTRY, contact.getCountry())
                .withValue(StructuredPostal.POSTCODE, contact.getPostalCode())
                .withValue(StructuredPostal.CITY, contact.getLocation())
                .withValue(StructuredPostal.STREET, contact.getStreetAddress())
                .build());              
        }

        // add to DB
        try {
            context.getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
            return R.string.contact_added;

        } catch (RemoteException e) {

            return R.string.adding_contact_failed; 

        } catch (OperationApplicationException e) {

            return R.string.adding_contact_failed;
        }

    } else if(contact != null && numberAlreadyInContacts(contact.getPhoneNumber(), context)){

        return R.string.contact_exists;

    } else {

        return R.string.adding_contact_failed;
    }
}

In this method I try to find out whether the contact is already in my address book. Therefore I look for the phone number, because it should be unique over all contacts.

private boolean numberAlreadyInContacts(String number, Context context) {

    if(number == null && number.length()<1){
        return false;
    }

    Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(number));

    ContentResolver contentResolver = context.getContentResolver();
    Cursor contactLookup = contentResolver.query(uri, new String[] {BaseColumns._ID,
            ContactsContract.PhoneLookup.DISPLAY_NAME }, null, null, null);

    if (contactLookup != null && contactLookup.getCount() > 0) {
        contactLookup.close();
        return true;
    }

    contactLookup.close();
    return false;
}

Have fun with this code!

Chris

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜