發現對系統的聯繫人進行操做的api很亂,感受邏輯有點不清楚......android
主要用到這4個類:api
android.provider.ContactsContract.CommonDataKinds.Email;
android.provider.ContactsContract.CommonDataKinds.Phone;
android.provider.ContactsContract.CommonDataKinds.StructuredName;
android.provider.ContactsContract.Data;app
1 import android.content.ContentResolver; 2 import android.content.ContentUris; 3 import android.content.ContentValues; 4 import android.net.Uri; 5 import android.os.Bundle; 6 import android.provider.ContactsContract.CommonDataKinds.Email; 7 import android.provider.ContactsContract.CommonDataKinds.Phone; 8 import android.provider.ContactsContract.CommonDataKinds.StructuredName; 9 import android.provider.ContactsContract.Data; 10 import android.provider.ContactsContract.RawContacts; 11 import android.support.v7.app.ActionBarActivity; 12 import android.util.Log; 13 import android.view.Menu; 14 import android.view.MenuItem; 15 import android.view.View; 16 import android.widget.Button; 17 import android.widget.EditText; 18 19 public class MainActivity extends ActionBarActivity { 20 ContentResolver contentResolver; 21 EditText name; 22 EditText phone; 23 EditText emil; 24 25 @Override 26 protected void onCreate(Bundle savedInstanceState) { 27 super.onCreate(savedInstanceState); 28 setContentView(R.layout.activity_main); 29 name = (EditText) findViewById(R.id.editText_name); 30 phone = (EditText) findViewById(R.id.EditText_phone); 31 emil = (EditText) findViewById(R.id.editText_emil); 32 contentResolver = getContentResolver(); 33 Button saveButton = (Button) findViewById(R.id.button_insert); 34 saveButton.setOnClickListener(new MyOnclickListener()); 35 36 } 37 38 class MyOnclickListener implements View.OnClickListener { 39 40 @Override 41 public void onClick(View v) { 42 ContentValues values = new ContentValues(); 43 Uri rawContactUri = contentResolver.insert(RawContacts.CONTENT_URI, values);//插入一個空值,目的是增長一條記錄,返回該記錄的id 44 long rawContactId = ContentUris.parseId(rawContactUri); 45 Log.i("csx", "rawContactId:" + rawContactId); 46 // 插入姓名 47 values.clear(); 48 values.put(Data.RAW_CONTACT_ID, rawContactId); 49 values.put(Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE); 50 values.put(StructuredName.GIVEN_NAME, name.getText().toString()); 51 contentResolver.insert(Data.CONTENT_URI, values); 52 // 插入電話 53 values.clear(); 54 values.put(Data.RAW_CONTACT_ID, rawContactId); 55 values.put(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE); 56 values.put(Phone.NUMBER, phone.getText().toString()); 57 values.put(Phone.TYPE, Phone.TYPE_MOBILE); 58 contentResolver.insert(Data.CONTENT_URI, values); 59 // 插入郵箱 60 values.clear(); 61 values.put(Data.RAW_CONTACT_ID, rawContactId); 62 values.put(Data.MIMETYPE, Email.CONTENT_ITEM_TYPE); 63 values.put(Email.DATA, emil.getText().toString()); 64 values.put(Email.TYPE, Email.TYPE_WORK); 65 contentResolver.insert(Data.CONTENT_URI, values); 66 67 } 68 }