Android 简单联系人操作
openkk1
12年前
Android中的Contacts联系人通过ContentProvider对外提供联系人数据及操作接口。
下面通过简单例子,来看看有关联系人的基本操作。
主Activity:public class MainActivity extends ListActivity { private ArrayList<String> list = new ArrayList<String>(); private ArrayAdapter adapter; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); insert(); queryByName(); queryAll(); adapter = new ArrayAdapter(this,R.layout.main,list); this.setListAdapter(adapter); } //查询所有联系人 public void queryAll() { ContentResolver resolver = this.getContentResolver(); Uri uri = Uri.parse("content://com.android.contacts/contacts"); Cursor idCursor = resolver.query(uri, new String[] { "_id" }, null, null, null); while (idCursor.moveToNext()) { //获取到raw_contacts表中的id int id = idCursor.getInt(0); //根据获取到的ID查询data表中的数据 uri = Uri.parse("content://com.android.contacts/contacts/" + id + "/data"); Cursor dataCursor = resolver.query(uri, new String[] { "data1", "mimetype" }, null, null, null); StringBuilder sb = new StringBuilder(); sb.append("id=" + id); //查询联系人表中的 while (dataCursor.moveToNext()) { String data = dataCursor.getString(0); String type = dataCursor.getString(1); if ("vnd.android.cursor.item/name".equals(type)) sb.append(", name=" + data); else if ("vnd.android.cursor.item/phone_v2".equals(type)) sb.append(", phone=" + data); else if ("vnd.android.cursor.item/email_v2".equals(type)) sb.append(", email=" + data); } list.add(sb.toString()); } } //根据电话号码查询联系人名称 private void queryByName() { ContentResolver resolver = this.getContentResolver(); Uri uri = Uri.parse("content://com.android.contacts/data/phones/filter/1234567"); Cursor c = resolver.query(uri, new String[] { "display_name" }, null, null, null); while (c.moveToNext()) { System.out.println(c.getString(0)); } } //添加联系人 private void insert() { ContentResolver resolver = this.getContentResolver(); Uri uri = Uri.parse("content://com.android.contacts/raw_contacts"); ContentValues values = new ContentValues(); //向raw_contacts插入一条除了ID之外, 其他全部为NULL的记录, ID是自动生成的 long id = ContentUris.parseId(resolver.insert(uri, values)); //添加联系人姓名 uri = Uri.parse("content://com.android.contacts/data"); values.put("raw_contact_id", id); values.put("data2", "zhang xiangdong"); values.put("mimetype", "vnd.android.cursor.item/name"); resolver.insert(uri, values); //添加联系人电话 values.clear(); // 清空上次的数据 values.put("raw_contact_id", id); values.put("data1", "1392315698"); values.put("data2", "2"); values.put("mimetype", "vnd.android.cursor.item/phone_v2"); resolver.insert(uri, values); //添加联系人邮箱 values.clear(); values.put("raw_contact_id", id); values.put("data1", "345672@qq.com"); values.put("data2", "1"); values.put("mimetype", "vnd.android.cursor.item/email_v2"); resolver.insert(uri, values); } //使用事务添加联系人 private void insertWithBatch() throws Exception { ContentResolver resolver = this.getContentResolver(); ArrayList<ContentProviderOperation> operations = new ArrayList<ContentProviderOperation>(); //插入id ContentProviderOperation operation1 = ContentProviderOperation .newInsert(Uri.parse("content://com.android.contacts/raw_contacts")) .withValue("_id", null) .build(); operations.add(operation1); //插入姓名 ContentProviderOperation operation2 = ContentProviderOperation .newInsert(Uri.parse("content://com.android.contacts/data")) .withValueBackReference("raw_contact_id", 0) .withValue("data2", "JOhn") .withValue("mimetype", "vnd.android.cursor.item/name") .build(); operations.add(operation2); //插入联系人电话 ContentProviderOperation operation3 = ContentProviderOperation .newInsert(Uri.parse("content://com.android.contacts/data")) .withValueBackReference("raw_contact_id", 0) .withValue("data1", "135124689745") .withValue("data2", "2") .withValue("mimetype", "vnd.android.cursor.item/phone_v2") .build(); operations.add(operation3); //插入邮箱 ContentProviderOperation operation4 = ContentProviderOperation .newInsert(Uri.parse("content://com.android.contacts/data")) .withValueBackReference("raw_contact_id", 0) .withValue("data1", "123568974@qq.com") .withValue("data2", "2") .withValue("mimetype", "vnd.android.cursor.item/email_v2") .build(); operations.add(operation4); //执行批量操作 resolver.applyBatch("com.android.contacts", operations); } }布局文件:
<?xml version="1.0" encoding="utf-8"?> <TextView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" />描述文件:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.test.contacts" android:versionCode="1" android:versionName="1.0"> <uses-sdk android:minSdkVersion="10" /> <strong><uses-permission android:name="android.permission.READ_CONTACTS"></uses-permission> <uses-permission android:name="android.permission.WRITE_CONTACTS"></uses-permission></strong> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".MainActivity" 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>运行结果: