Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android開發系列(十一):對手機通訊錄的讀取、添加、刪除、查找

Android開發系列(十一):對手機通訊錄的讀取、添加、刪除、查找

編輯:關於Android編程

一、通訊錄介紹

通訊錄是Android手機自帶的一個應用,它是一個ContentProvider應用,其它應用可以對通訊錄進行訪問,進行對聯系人的CRUD操作。


二、通訊錄數據庫結構的介紹

首先,我們可以在File Explorer視圖下找到contacts2.db文件,這是通訊錄的文件

\

然後,我們用SQLite打開,分析下它的數據庫結構:

raw_contacts表:

\

data表:

\

mimetypes表:

\

這三張表的結構介紹完了,接下來我們說下它們之間的聯系:<喎?/kf/ware/vc/" target="_blank" class="keylink">vcD4KPHA+cmF3X2NvbnRhY3Rzse205rfFwarPtcjLtcS8x8K8SUSjutfWts7D+7PGysdfaWQ8L3A+CjxwPmRhdGGx7bTmt8XBqs+1yMu1xNDFz6Kjul9pZMrH1ve8/KOscmF3X2NvbnRhY3RzX2lkttTTpnJhd19jb250YWN0c7HttcRfaWSjrG1pbWV0eXBlX2lk19a2zrbU06a1xMrHbWltZXR5cGVzse21xF9pZDwvcD4KPHA+bWltZXR5cGVzse205rfFZGF0YbHttcTDv8z1vMfCvLXEyvTQ1KO6X2lkysfW97z8o6zOqjG1xMqxuvLKx2VtYWlswODQzTwvcD4KPHA+PGJyPgo8L3A+CjxoMT7I/aGiz9TKvqGizO2806Giyb6z/aGisunV0sGqz7XIy6O6PC9oMT4KPGgyPjGhorvxyKHL+dPQtcTBqs+1yMujujwvaDI+CjxwPjwvcD4KPHByZSBjbGFzcz0="brush:java;">public void testContacts() throws Exception{ Uri uri = Uri.parse("content://com.android.contacts/contacts"); //獲得一個ContentResolver數據共享的對象 ContentResolver reslover = getContext().getContentResolver(); //取得聯系人中開始的游標,通過content://com.android.contacts/contacts這個路徑獲得 Cursor cursor = reslover.query(uri, null, null, null, null); //上邊的所有代碼可以由這句話代替:Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null); //Uri.parse("content://com.android.contacts/contacts") == ContactsContract.Contacts.CONTENT_URI while(cursor.moveToNext()){ //獲得聯系人ID String id = cursor.getString(cursor.getColumnIndex(android.provider.ContactsContract.Contacts._ID)); //獲得聯系人姓名 String name = cursor.getString(cursor.getColumnIndex(android.provider.ContactsContract.Contacts.DISPLAY_NAME)); //獲得聯系人手機號碼 Cursor phone = reslover.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "=" + id, null, null); StringBuilder sb = new StringBuilder("contactid=").append(id).append(name); while(phone.moveToNext()){ //取得電話號碼(可能存在多個號碼) int phoneFieldColumnIndex = phone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER); String phoneNumber = phone.getString(phoneFieldColumnIndex); sb.append(phoneNumber+"www"); } //建立一個Log,使得可以在LogCat視圖查看結果 Log.i(TAG, sb.toString()); } }

2、查找聯系人:

//根據號碼獲取聯系人的姓名
	public void testContactNameByNumber() throws Exception{
		String number = "110";
		Uri uri = Uri.parse("content://com.android.contacts/data/phones/filter/"+number);
		ContentResolver resolver = getContext().getContentResolver();
		Cursor cursor = resolver.query(uri, new String[]{android.provider.ContactsContract.Data.DISPLAY_NAME}, null, null, null);	
		if(cursor.moveToFirst()){
			String name = cursor.getString(0);
			Log.i(TAG, name);
		}
		cursor.close();
	}

3、添加聯系人:

//添加聯系人
	public void testAddContact() throws Exception{
		Uri uri = Uri.parse("content://com.android.contacts/raw_contacts");
		ContentResolver resolver = getContext().getContentResolver();
		ContentValues values = new ContentValues();
		long contactid = ContentUris.parseId(resolver.insert(uri, values));
		
		uri = Uri.parse("content://com.android.contacts/data");
		
		//添加姓名
		values.put("raw_contact_id", contactid);
		values.put(Data.MIMETYPE, "vnd.android.cursor.item/name");
		values.put("data1", "xiaoming");
		resolver.insert(uri, values);
		values.clear();
		
		//添加電話
		values.put("raw_contact_id", contactid);
		values.put(Data.MIMETYPE, "vnd.android.cursor.item/phone_v2");
		values.put("data1", "1234120155");
		resolver.insert(uri, values);
		values.clear();
		
		//添加Email
		values.put("raw_contact_id", contactid);
		values.put(Data.MIMETYPE, "vnd.android.cursor.item/email_v2");
		values.put("data1", "[email protected]");
		resolver.insert(uri, values);
	}

4、批量添加聯系人(因為如果按照姓名、電話、Email的方式添加的話,其中一個環節出錯,聯系人也可以添加上去。但是批量的話就是一下子添加所有的)

//批量添加
	public void testAddContact2() throws Exception{
		Uri uri = Uri.parse("content://com.android.contacts/raw_contacts");
		ContentResolver resolver = getContext().getContentResolver();
		ArrayList operations = new ArrayList();
		ContentProviderOperation op1 = ContentProviderOperation.newInsert(uri)
			.withValue("account_name", null)
			.build();
		operations.add(op1);
		
		uri = Uri.parse("content://com.android.contacts/data");
		//添加姓名
		ContentProviderOperation op2 = ContentProviderOperation.newInsert(uri)
			.withValueBackReference("raw_contact_id", 0)
			.withValue("mimetype", "vnd.android.cursor.item/name")
			.withValue("data2", "李小龍")
			.build();
		operations.add(op2);
		//添加電話號碼
		ContentProviderOperation op3 = ContentProviderOperation.newInsert(uri)
			.withValueBackReference("raw_contact_id", 0)
			.withValue("mimetype", "vnd.android.cursor.item/phone_v2")
			.withValue("data1", "1234120155")
			.withValue("data2", "2")
			.build();
		operations.add(op3);
		
		resolver.applyBatch("com.android.contacts", operations);
	}

5、刪除聯系人:

public void testDelete()throws Exception{  
	    String name = "李小龍";  
	    //根據姓名求id  
	    Uri uri = Uri.parse("content://com.android.contacts/raw_contacts");  
	    ContentResolver resolver = this.getContext().getContentResolver();  
	    Cursor cursor = resolver.query(uri, new String[]{Data._ID},"display_name=?", new String[]{name}, null);  
	    if(cursor.moveToFirst()){  
	        int id = cursor.getInt(0);  
	        //根據id刪除data中的相應數據  
	        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+""});  
	    }  
	}  






  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved