下面样例包含新增通讯录和删除通讯录操作
1.Activity文件(ContactOPActivity.java)
<pre class="java" name="code">package com.example.contacttest; import android.app.Activity; 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.Data; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; public class ContactOPActivity extends Activity implements OnClickListener { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.contact_op); Button addContactButton = (Button)findViewById(R.id.delContactButton); Button delContactButton = (Button)findViewById(R.id.addContactButton); addContactButton.setOnClickListener(this); delContactButton.setOnClickListener(this); } @Override public void onClick(View v) { EditText nameEditText = (EditText)findViewById(R.id.nameEditText); EditText phoneNumEditText = (EditText)findViewById(R.id.phoneNumEditText); EditText emailEditText = (EditText)findViewById(R.id.emailEditText); String name = nameEditText.getText().toString(); String phoneNum = phoneNumEditText.getText().toString(); String email = emailEditText.getText().toString(); switch (v.getId()) { case R.id.delContactButton: delContact(name); Toast.makeText(this, "Deleted "+name, Toast.LENGTH_LONG).show(); break; case R.id.addContactButton: addContact(name,phoneNum,email); Toast.makeText(this, "Added "+name, Toast.LENGTH_LONG).show(); break; } } private void delContact(String name) { Uri uri = Uri.parse("content://com.android.contacts/raw_contacts"); ContentResolver resolver = this.getApplicationContext() .getContentResolver(); Cursor cursor = resolver.query(uri, new String[] { Data._ID }, "display_name=?", new String[] { name }, null); if (cursor.moveToFirst()) { int id = cursor.getInt(0); resolver.delete(uri, "display_name=?", new String[] { name }); uri = Uri.parse("content://com.android.contacts/data"); resolver.delete(uri, "raw_contact_id=?", new String[] { id + "" }); } } private void addContact(String name,String phoneNum,String email) { Uri uri = Uri.parse("content://com.android.contacts/raw_contacts"); ContentResolver resolver = this.getApplicationContext() .getContentResolver(); ContentValues values = new ContentValues(); long contac_id = ContentUris.parseId(resolver.insert(uri, values)); uri = Uri.parse("content://com.android.contacts/data"); values.put("raw_contact_id", contac_id); values.put(Data.MIMETYPE, "vnd.android.cursor.item/name"); values.put("data2", name); values.put("data1", name); resolver.insert(uri, values); values.clear(); values.put("raw_contact_id", contac_id); values.put(Data.MIMETYPE, "vnd.android.cursor.item/phone_v2"); values.put("data2", "2"); values.put("data1", phoneNum); resolver.insert(uri, values); values.clear(); values.put("raw_contact_id", contac_id); values.put(Data.MIMETYPE, "vnd.android.cursor.item/email_v2"); values.put("data2", "2"); values.put("data1", email); resolver.insert(uri, values); values.clear(); } }
2.layout文件:contact_op.xml
<?xml version="1.0" encoding="utf-8"?> <TableLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <TableRow android:id="@+id/tableRow1" android:layout_width="wrap_content" android:layout_height="wrap_content" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Name:" /> <EditText android:id="@+id/nameEditText" android:layout_width="wrap_content" android:layout_height="wrap_content" android:ems="10" android:text="jerry"> </EditText> </TableRow> <TableRow android:id="@+id/tableRow2" android:layout_width="wrap_content" android:layout_height="wrap_content" > <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Phone:" /> <EditText android:id="@+id/phoneNumEditText" android:layout_width="wrap_content" android:layout_height="wrap_content" android:ems="10" android:text="13500000000"> </EditText> </TableRow> <TableRow android:id="@+id/tableRow3" android:layout_width="wrap_content" android:layout_height="wrap_content" > <TextView android:id="@+id/textView3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Email:" /> <EditText android:id="@+id/emailEditText" android:layout_width="wrap_content" android:layout_height="wrap_content" android:ems="10" android:text="test@xx.com"> </EditText> </TableRow> <TableRow android:id="@+id/tableRow4" android:layout_width="wrap_content" android:layout_height="wrap_content" > <Button android:id="@+id/addContactButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Add" /> <Button android:id="@+id/delContactButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Delete" /> </TableRow> </TableLayout>
3.manifest配置文件
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.contacttest" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="21" /> <!-- 读联系人权限 --> <uses-permission android:name="android.permission.READ_CONTACTS" /> <!-- 写联系人权限 --> <uses-permission android:name="android.permission.WRITE_CONTACTS" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".ContactOPActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>