如何将联系人信息保存到电话簿?

时间:2012-04-18 07:03:41

标签: android

嘿伙计们,我想保存人名细节,就像它的名字姓氏电话号码一样 将用户网站用户组织解析为电话簿,请提出建议 解决这个问题的一些方法。

1 个答案:

答案 0 :(得分:5)

您可以执行此代码

将用户信息保存到电话簿中
  package com.contacts.in;

import java.util.ArrayList;

import android.app.Activity;
import android.content.ContentProviderOperation;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.ContextWrapper;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.provider.ContactsContract.Data;
import android.widget.Toast;

public class CONTACTSAVEDActivity extends Activity {
    String displayName = "XYZ";
    String mobileNumber = "123456";
    String homeNumber = "1111";
    String workNumber = "2222";
    String emailID = "email@nomail.com";
    String company = "bad";
    String jobTitle = "abcd";


    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        ArrayList<ContentProviderOperation> ops = 
            new ArrayList<ContentProviderOperation>();

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

        //------------------------------------------------------ Names
        if(displayName != null)
        {           
            ops.add(ContentProviderOperation.newInsert(
                ContactsContract.Data.CONTENT_URI)              
                .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
                .withValue(ContactsContract.Data.MIMETYPE,
                    ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE)
                .withValue(
                    ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME,     
                    displayName).build()
            );
        } 

        //------------------------------------------------------ Mobile Number                      
        if(mobileNumber != null)
        {
            ops.add(ContentProviderOperation.
                newInsert(ContactsContract.Data.CONTENT_URI)
                .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
                .withValue(ContactsContract.Data.MIMETYPE,
                ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
                .withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, mobileNumber)
                .withValue(ContactsContract.CommonDataKinds.Phone.TYPE, 
                ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE)
                .build()
            );
        }

                            //------------------------------------------------------ Home Numbers
                            if(homeNumber != null)
                            {
                                ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
                                        .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
                                        .withValue(ContactsContract.Data.MIMETYPE,
                                                ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
                                        .withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, homeNumber)
                                        .withValue(ContactsContract.CommonDataKinds.Phone.TYPE, 
                                                ContactsContract.CommonDataKinds.Phone.TYPE_HOME)
                                        .build());
                            }

                            //------------------------------------------------------ Work Numbers
                            if(workNumber != null)
                            {
                                ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
                                        .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
                                        .withValue(ContactsContract.Data.MIMETYPE,
                                                ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
                                        .withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, workNumber)
                                        .withValue(ContactsContract.CommonDataKinds.Phone.TYPE, 
                                                ContactsContract.CommonDataKinds.Phone.TYPE_WORK)
                                        .build());
                            }

                            //------------------------------------------------------ Email
                            if(emailID != null)
                            {
                                 ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
                                            .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
                                            .withValue(ContactsContract.Data.MIMETYPE,
                                                    ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE)
                                            .withValue(ContactsContract.CommonDataKinds.Email.DATA, emailID)
                                            .withValue(ContactsContract.CommonDataKinds.Email.TYPE, ContactsContract.CommonDataKinds.Email.TYPE_WORK)
                                            .build());
                            }

                            //------------------------------------------------------ Organization
                            if(!company.equals("") && !jobTitle.equals(""))
                            {
                                ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
                                        .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
                                        .withValue(ContactsContract.Data.MIMETYPE,
                                                ContactsContract.CommonDataKinds.Organization.CONTENT_ITEM_TYPE)
                                        .withValue(ContactsContract.CommonDataKinds.Organization.COMPANY, company)
                                        .withValue(ContactsContract.CommonDataKinds.Organization.TYPE, ContactsContract.CommonDataKinds.Organization.TYPE_WORK)
                                        .withValue(ContactsContract.CommonDataKinds.Organization.TITLE, jobTitle)
                                        .withValue(ContactsContract.CommonDataKinds.Organization.TYPE, ContactsContract.CommonDataKinds.Organization.TYPE_WORK)
                                        .build());
                            }

                            // Asking the Contact provider to create a new contact                  
                            try 
                            {
                                getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
                            } 
                            catch (Exception e) 
                            {               
                                e.printStackTrace();
                              //  Toast.makeText(myContext, "Exception: " + e.getMessage(), Toast.LENGTH_SHORT).show();
                            }

    }
}

还在清单

中添加权限
<uses-permission android:name="android.permission.READ_CONTACTS"></uses-permission>
<uses-permission android:name="android.permission.WRITE_CONTACTS"></uses-permission>