編輯:關於Android編程
通訊錄是Android手機自帶的一個應用,它是一個ContentProvider應用,其它應用可以對通訊錄進行訪問,進行對聯系人的CRUD操作。
首先,我們可以在File Explorer視圖下找到contacts2.db文件,這是通訊錄的文件
然後,我們用SQLite打開,分析下它的數據庫結構:
這三張表的結構介紹完了,接下來我們說下它們之間的聯系:<喎?/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()); } }
//根據號碼獲取聯系人的姓名 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(); }
//添加聯系人 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); }
//批量添加 public void testAddContact2() throws Exception{ Uri uri = Uri.parse("content://com.android.contacts/raw_contacts"); ContentResolver resolver = getContext().getContentResolver(); ArrayListoperations = 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); }
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+""}); } }
一. 接入須知:(JPush基本概念)1. 消息形式 JPush提供四種消息形式:通知,自定義消息,富媒體和本地通知。a. 通知 或者說 Push Notificatio
一、能做什麼你只需要傳url,JavaBean就可以在回調方法裡面得到想要的結果,你會發現你的代碼裡面沒有了子線程、沒有了handle,鏈式的變成使得代碼更加清晰。1.1
Android 調用WCF實例1. 構建服務端程序using System.ServiceModel;namespace yournamespace{ [Service
本文實例講述了Android編程實現自定義進度條顏色的方法。分享給大家供大家參考,具體如下:android 自定義進度條顏色先看圖基於產品經理各種自定義需求,經過查閱了解