為了訪問賬戶信息,在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; }