2012-03-09 141 views
0

在我的應用程序中,我想獲取並列出聯繫人姓名的號碼,並且我嘗試了電子郵件地址,但ContactsContract.CommonDataKinds.EmailContactsContract.Contacts.DISPLAY_NAME發生衝突,因此無法儘可能地提取給我一些代碼,我試着下面的代碼無法獲取本地電話聯繫人姓名,手機號碼和電子郵件地址

ContentResolver cr = getContentResolver(); 
    Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, 
      null, null, null, null); 
    if (cur.getCount() > 0) { 
     while (cur.moveToNext()) { 
      String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID)); 
      String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)); 
      if (Integer.parseInt(cur.getString(
        cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) { 
       Cursor pCur = cr.query(
         ContactsContract.CommonDataKinds.Phone.CONTENT_URI, 
         null, 
         ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?", 
         new String[]{id}, null); 
       while (pCur.moveToNext()) { 
        String phoneNo = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); 
        Toast.makeText(NativeContentProvider.this, "Name: " + name + ", Phone No: " + phoneNo, Toast.LENGTH_SHORT).show(); 
       } 
      pCur.close(); 
     } 
     } 
    } 
    String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID)); 
    Cursor emailCur = cr.query(
      ContactsContract.CommonDataKinds.Email.CONTENT_URI, 
      null, 
      ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?", 
      new String[]{id}, null); 
    while (emailCur.moveToNext()) { 
     // This would allow you get several email addresses 
      // if the email addresses were stored in an array 
     String email = emailCur.getString(
         emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA)); 
     String emailType = emailCur.getString(
         emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.TYPE)); 

     System.out.println("Email " + email + " Email Type : " + emailType); 
    } 
    emailCur.close(); 
+0

HI請參考以下鏈接: http://stackoverflow.com/questions/6152442/how-to-get-contact-email-id – Mohanish 2012-03-09 14:05:56

回答

0

它看起來像你的代碼的問題可能是你想要應該在兩個單獨的查詢。

查詢電子郵件帳戶或電話號碼列表使用的常量類似ContactsContract.CommonDataKinds,URI:ContactsContract.CommonDataKinds.Email.CONTENT_URI中的常量。

查詢實際的非名稱列表的聯繫人屬性將使用與ContactsContract.Contacts,URI中的常量類似的常量:基於從Contacts.CONTENT_URI返回的數據。

信息並非全部存儲在同一個地方,這可能是爲什麼可能存在衝突。

相關問題