Android Contacts的使用(二) – Android移動開發技術文章_手機開發 Android移動開發教學課程

Granting Access 授予權限
<uses-permission android:name="android.permission.READ_CONTACTS" />

Querying the contact database 聯系人數據庫查詢


Retrieving Contact Details 獲取聯系方式

本的聯系人信息存儲在聯系人表中,而詳細信息存儲在個人表中。在 Android1.x 中查詢的聯系人記錄數據庫的URI是People.CONTENT_URI。

Java代碼 
package com.test;import android.app.Activity; 
import android.content.ContentResolver; 
import android.database.Cursor;import android.os.Bundle; 
import android.provider.Contacts.People; 
public class TestContacts extends Activity {     
@Override    public void onCreate(Bundle savedInstanceState) {  
       super.onCreate(savedInstanceState);         
       setContentView(R.layout.main);        
       ContentResolver cr = getContentResolver();         
       Cursor cur = cr.query(People.CONTENT_URI, null, null, null, null);         
        if(cur.getCount()>0){            
          while (cur.moveToNext()) {                 
             String id = cur.getString(cur.getColumnIndex(People._ID));                 
             String name = cur.getString(cur.getColumnIndex(People.DISPLAY_NAME));             
          }         
}          


Phone Numbers 電話號碼
查詢的URI換成Contacts.Phones.CONTENT_URI。
Java代碼 
if (cur.getInt(cur.getColumnIndex(People.PRIMARY_PHONE_ID)) > 0) {                     
        Cursor pCur = cr.query(Contacts.Phones.CONTENT_URI,null,                            Contacts.Phones.PERSON_ID +" = ?",new String[]{id}, null); 
         int i=0;                     
         int pCount = pCur.getCount();                     
         String[] phoneNum = new String[pCount];                      String[] phoneType = new String[pCount];                     
 while (pCur.moveToNext()) {    
          phoneNum[i] = pCur.getString(pCur.getColumnIndex(Contacts.Phones.NUMBER));          
           phoneType[i] = pCur.getString(pCur.getColumnIndex(Contacts.Phones.TYPE));                                                         i++;                     
}     

以存儲多個電話號碼,我們需要遍歷返回的結果。查詢除瞭返回電話號碼外還返回瞭其他(傢庭,工作,手機等)類型。

Email Addresses 郵件地址  
Java代碼 
Cursor emailCur = cr.query(Contacts.ContactMethods.CONTENT_EMAIL_URI,null,                        Contacts.ContactMethods.PERSON_ID + " = ?",new String[]{id}, null);                 
   while (emailCur.moveToNext()) {                     
 // This would allow you get several email addresses                }                  
    emailCur.close();   
   

Notes 註釋  
為每個聯系人附加自定義註釋。註釋存儲在聯系人記錄中,並通過People.NOTES存儲的數據進行訪問。

String notes=cur.getString(cur.getColumnIndex(People.NOTES));

Postal Addresses 郵政地址
每個聯系人都可以存儲多個郵政地址。地址存儲在聯系方式表中,要獲取數據,需要有次要條件。添加適配Contacts.ContactMethods.CONTENT_POSTAL_ITEM_TYPE的條件Contacts.ContactMethods.KIND來訪問Contacts.ContactMethods.CONTENT_URI傳遞過來的地址。

Java代碼 
String addrWhere = Contacts.ContactMethods.PERSON_ID                 + " = ? AND " + Contacts.ContactMethods.KIND + " = ?";                 
 String[] addrWhereParams = new String[]{id,                     Contacts.ContactMethods.CONTENT_POSTAL_ITEM_TYPE};                         Cursor addrCur = cr.query(Contacts.ContactMethods.CONTENT_URI,                             null, addrWhere, addrWhereParams, null);                 while(addrCur.moveToNext()) {                    String addr = addrCur.getString(                               addrCur.getColumnIndex(Contacts.ContactMethodsColumns.DATA));                    String type = addrCur.getString(                               addrCur.getColumnIndex(Contacts.ContactMethodsColumns.TYPE));                }                 addrCur.close(); 

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。