1.AndroidManifest.xml中添加权限
<uses-permission android:name="android.permission.READ_CONTACTS"></uses-permission> <uses-permission android:name="android.permission.WRITE_CONTACTS"></uses-permission>
2.布局
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.lj.chapter9.MainActivity"> <EditText android:id="@+id/contactNameTxt" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:ems="10" android:inputType="textPersonName" android:text="姓名" /> <EditText android:id="@+id/phoneNumberTxt" android:layout_width="match_parent" android:layout_height="wrap_content" android:ems="10" android:inputType="phone" android:text="号码" android:layout_below="@+id/contactNameTxt" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" /> <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Click Button Below to Run a Query" android:textSize="17dip" android:layout_below="@+id/phoneNumberTxt" android:layout_alignRight="@+id/addButton" android:layout_alignEnd="@+id/addButton" android:layout_marginTop="34dp" /> <Button android:id="@+id/queryButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Click to Query Contacts Database" android:layout_below="@+id/phoneNumberTxt" android:layout_centerHorizontal="true" android:layout_marginTop="88dp" /> <Button android:id="@+id/addButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/queryButton" android:layout_alignStart="@+id/queryButton" android:layout_below="@+id/queryButton" android:layout_marginTop="35dp" android:text="Click to Add Contacts Database" /> <Button android:id="@+id/updateButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/queryButton" android:layout_alignStart="@+id/queryButton" android:layout_below="@+id/addButton" android:layout_marginTop="35dp" android:text="Click to Update Contacts Database" /> <Button android:id="@+id/deleteButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/queryButton" android:layout_alignStart="@+id/queryButton" android:layout_below="@+id/updateButton" android:layout_marginTop="35dp" android:text="Click to Delete Contacts Database" /> </RelativeLayout>
3.Activity活动
package com.example.lj.chapter9; import android.content.ContentResolver; import android.content.ContentUris; import android.content.ContentValues; import android.database.Cursor; import android.net.Uri; import android.os.Bundle; import android.provider.ContactsContract; import android.support.annotation.IdRes; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; public class MainActivity extends AppCompatActivity { private static final String TAG = "AndroidTest"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button queryBtn = (Button) findViewById(R.id.queryButton); queryBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { queryContactPhoneNumber(); } }); Button addBtn = (Button) findViewById(R.id.addButton); addBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { addContactPhoneNumber(getEditValue(R.id.contactNameTxt), getEditValue(R.id.phoneNumberTxt)); } }); Button updateBtn = (Button) findViewById(R.id.updateButton); updateBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { updateContactPhoneNumber(getEditValue(R.id.contactNameTxt), getEditValue(R.id.phoneNumberTxt)); } }); Button deleteBtn = (Button) findViewById(R.id.deleteButton); deleteBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { deleteContactPhoneNumber(getEditValue(R.id.contactNameTxt)); } }); } private String getEditValue(@IdRes int id) { EditText txt = (EditText) findViewById(id); return txt.getText().toString(); } private void deleteContactPhoneNumber(String contactName) { //根据姓名求id Uri uri = ContactsContract.RawContacts.CONTENT_URI; ContentResolver resolver = getContentResolver(); String where = ContactsContract.PhoneLookup.DISPLAY_NAME; Cursor cursor = resolver.query(uri, new String[]{ContactsContract.Data._ID}, where + "=?", new String[]{contactName}, null); if (cursor.moveToFirst()) { int id = cursor.getInt(0); //根据id删除data中的相应数据 resolver.delete(uri, where + "=?", new String[]{contactName}); uri = ContactsContract.Data.CONTENT_URI; resolver.delete(uri, ContactsContract.Data.RAW_CONTACT_ID + "=?", new String[]{id + ""}); } Toast.makeText(this, "Success", Toast.LENGTH_SHORT).show(); } private void updateContactPhoneNumber(String contactName, String phoneNumber) { Uri uri = ContactsContract.Data.CONTENT_URI;//对data表的所有数据操作 ContentResolver resolver = getContentResolver(); ContentValues values = new ContentValues(); values.put(ContactsContract.Data.DATA1, phoneNumber); int result = resolver.update(uri, values, ContactsContract.Data.MIMETYPE + "=? and " + ContactsContract.PhoneLookup.DISPLAY_NAME + "=?", new String[]{ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE, contactName}); if (result > 0) { Toast.makeText(this, "Success", Toast.LENGTH_SHORT).show(); } } private void addContactPhoneNumber(String contactName, String phoneNumber) { ContentValues values = new ContentValues(); Uri uri = getContentResolver().insert(ContactsContract.RawContacts.CONTENT_URI, values); long contact_id = ContentUris.parseId(uri); //插入data表 uri = ContactsContract.Data.CONTENT_URI; String raw_contact_id = ContactsContract.Data.RAW_CONTACT_ID; String data2 = ContactsContract.Data.DATA2; String data1 = ContactsContract.Data.DATA1; //add Name values.put(raw_contact_id, contact_id); values.put(ContactsContract.CommonDataKinds.Phone.MIMETYPE, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE); values.put(data2, contactName); values.put(data1, contactName); getContentResolver().insert(uri, values); values.clear(); //add Phone values.put(raw_contact_id, contact_id); values.put(ContactsContract.CommonDataKinds.Phone.MIMETYPE, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE); values.put(data2, phoneNumber); //手机 values.put(data1, phoneNumber); getContentResolver().insert(uri, values); values.clear(); Toast.makeText(this, "添加新的联系人:" + contactName + " " + phoneNumber, Toast.LENGTH_SHORT).show(); } private void queryContactPhoneNumber() { String[] cols = {ContactsContract.PhoneLookup.DISPLAY_NAME, ContactsContract.CommonDataKinds.Phone.NUMBER}; Cursor cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, cols, null, null, null); for (int i = 0; i < cursor.getCount(); i++) { cursor.moveToPosition(i); // 取得联系人名字 int nameFieldColumnIndex = cursor.getColumnIndex(ContactsContract.PhoneLookup.DISPLAY_NAME); int numberFieldColumnIndex = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER); String name = cursor.getString(nameFieldColumnIndex); String number = cursor.getString(numberFieldColumnIndex); Toast.makeText(this, name + " " + number, Toast.LENGTH_SHORT).show(); } } }
该活动主要是根据http://www.cnblogs.com/liaojie970/p/5744580.html这篇博客改编而来,将里面如:
Uri uri = Uri.parse("content://com.android.contacts/raw_contacts");
方式改为了
uri = ContactsContract.Data.CONTENT_URI;
其基本思路是一样的