为了访问账户信息,在manifest文件中必须提供以下权限:
<uses-permission android:name=\"android.permission.GET_ACCOUNTS\" /> <uses-permission android:name=\"android.permission.READ_CONTACTS\" /> <uses-permission android:name=\"android.permission.WRITE_CONTACTS\" />
在Activity中,可以使用managedQuery方法查询ContactsContract.Contacts数据,并返回Cursor:
private Cursor getContacts { Uri uri = ContactsContract.Contacts.CONTENT_URI; String projection = new String { ContactsContract.Contacts._ID, ContactsContract.Contacts.LOOKUP_KEY, ContactsContract.Contacts.DISPLAY_NAME }; String selection = null; String selectionArgs = null; String sortOrder = ContactsContract.Contacts.DISPLAY_NAME + \" COLLATE LOCALIZED ASC\"; return managedQuery(uri, projection, selection, selectionArgs, sortOrder); }
关于ContactsContract.Contacts类中可用的字段和常数的完整信息,可参考开发者文档:http://developer.android.com/reference/android/provider/ContactsContract.Contacts.html。
有了Cursor,可以在SimpleCursorAdapter中加载它,并显示特定的数据字段,在这个例子中即联系信息“display name”:
String fields = new String { ContactsContract.Data.DISPLAY_NAME }; SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.contact, cursor, fields, new int {R.id.name}); // get the listview ListView contactlist = (ListView) findViewById(R.id.contactlist); // set the adapter and let it render contactlist.setAdapter(adapter);
以下是包含ListView的layout(通过R.id.contactlist引用):
<?xml version=\"1.0\" encoding=\"utf-8\"?> <LinearLayout xmlns:android=\"http://schemas.android.com/apk/res/android\" android:orientation=\"vertical\" android:layout_ android:layout_ android:background=\"#fff\" > <ListView android:id=\"@+id/contactlist\" android:layout_ android:layout_ /> </LinearLayout>
以下是SimpleCursorAdapter的通讯录layout(通过R.layout.contact引用):
<?xml version=\"1.0\" encoding=\"utf-8\"?> <LinearLayout xmlns:android=\"http://schemas.android.com/apk/res/android\" android:layout_ android:layout_ android:background=\"#fff\" > <TextView android:id=\"@+id/name\" android:layout_ android:layout_ android:textColor=\"#000\" android:textSize=\"25sp\" android:padding=\"5dp\" /> </LinearLayout>
这里,通过提供Cursor及Cursor内的位置来删除联系方式:
private void deleteContact(Cursor cursor, int position) { cursor.moveToPosition(position); long id = cursor.getLong(0); String lookupkey = cursor.getString(1); Uri uri = ContactsContract.Contacts.getLookupUri(id, lookupkey); String selectionArgs = null; String where = null; ContentResolver cr = getContentResolver; cr.delete(uri, where, selectionArgs); }
在这个例子中,为了添加联系方式,构建了一组ContentProviderOperations并对其进行了批量处理。注意,首先插入新的联系方式,然后如果有电话信息,就增加电话信息(正如这个例子所示)。为了执行插入操作,通过SimpleCursorContentProviderOperation.newInsert方法创建了ContentProviderOperation.Builder,生成专门执行insert的ContentProviderOperation,然后调用build方法执行构建:
String accountNameWeWant = \"SpecialAccount\"; String phone = \"8885551234\"; String name = \"Bob\"; String accountname = null; String accounttype = null; Account accounts = AccountManager.get(this).getAccounts; // find the account we want. if we don\'t find it we use \'null\' - the default for(Account account : accounts) { if(account.equals(accountNameWeWant)) { accountname = account.name; accounttype = account.type; break; } } ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>; ops.add(ContentProviderOperation.newInsert (ContactsContract.RawContacts.CONTENT_URI) .withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, accountname) .withValue(ContactsContract.RawContacts.ACCOUNT_NAME, accounttype) .build); // create the new contact 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, name) .build); // if there is a phone num we add it if(phone.getText != null && phone.getText.toString.trim.length > 0) { 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, phone) .withValue(ContactsContract.CommonDataKinds.Phone.TYPE, ContactsContract.CommonDataKinds.Phone.TYPE_HOME) .build); } try { getContentResolver.applyBatch(ContactsContract.AUTHORITY, ops); } catch (Exception e) { e.printStackTrace; }