簡體   English   中英

Android - 無法獲取某些聯系人的電話號碼

[英]Android - cant get phone number of some contacts

我在提取聯系人列表中某些人的電話號碼時遇到問題。

首先,我在列表視圖中顯示所有聯系人:

String[] projection = new String[] {
            ContactsContract.Contacts._ID,
            ContactsContract.CommonDataKinds.Phone.CONTACT_ID,
            ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
            ContactsContract.CommonDataKinds.Phone.NUMBER
    };

mCursor = mContext.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
            projection, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "=?", new String[] {mContactId}, null);

單擊某個項目時,這就是我獲取 contact_id 的方式:

@Override
public void onListItemClick(ListView l, View v, int position, long id) {
        Cursor currentCursor = mContactsAdapter.getCursor();

    if (currentCursor != null) {
        notifyOnContactSelectedListeners(String.valueOf(id));
    }
}

然后我創建一個新片段,並在加載它時查詢聯系人的電話和顯示名稱:

if (cursor != null && cursor.getCount() > 0) {

        cursor.moveToFirst();

        String firstName = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
        String number = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
    }

所以對於一些有電話的人來說,我通過這種方式獲得電話號碼,這沒關系。 但是對於某些人來說,我無法通過這種方式獲取電話號碼 - 但他們在默認的電話通訊錄中確實有電話號碼。

什么地方出了錯?

我也有類似的困難。 我發現我收不到的號碼都是從我關聯的 Facebook 帳戶導入的。 您將能夠檢測到該聯系人是否存在,並且他們確實有電話號碼。 但是,當您嘗試使用 SQL 查詢檢索所述數字時,返回的結果將為null

據了解,Facebook 出於安全原因限制了對其聯系人的訪問。 我還沒有找到另一個隱藏電話號碼的提供商(例如 LinkedIn、Google)。

進一步閱讀: 在 RawContacts 中找不到 Facebook 聯系人

試試這個可能對你有用

公共類 Contact 擴展 Activity 實現 OnItemClickListener{

private static final int PICK_CONTACT = 0;
Cursor c;
Cursor cursor,phones,emails,address;
String id,phoneNo,name;
String[] from;
int[] to;
ListView lv;
Cursor cur,pCur;
List<String> list1 = new ArrayList<String>();

@Override
public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.contact);
  lv = (ListView)findViewById(R.id.contactlist);
  displayContacts();
  lv.setAdapter(new ArrayAdapter<String>(this,
          android.R.layout.simple_list_item_1, list1)); 
  lv.setOnItemClickListener(this);
}

private void displayContacts() {

    ContentResolver cr = getContentResolver();
   cur = cr.query(ContactsContract.Contacts.CONTENT_URI,
            null, null, null, null);
    if (cur.getCount() > 0) {
        while (cur.moveToNext()) {
            id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
             name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
            if (Integer.parseInt(cur.getString(
                    cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
                  pCur = cr.query(
                         ContactsContract.CommonDataKinds.Phone.CONTENT_URI, 
                         null, 
                         ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?", 
                         new String[]{id}, null);
                 while (pCur.moveToNext()) {
                     phoneNo = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                 // setContact(name,phoneNo);
                     System.out.println("name"+name+"ph no"+phoneNo);
                     list1.add(name+"\n"+phoneNo);
            //       Toast.makeText(this, "Name: " + name + ", Phone No: " + phoneNo, Toast.LENGTH_SHORT).show();


                 } 

            pCur.close();
          }
        }
    }




}

@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
    // TODO Auto-generated method stub
    String s = lv.getItemAtPosition(arg2).toString();

    Log.i("my msg", s.substring(0, s.indexOf("\n")));

    Toast.makeText(this, s.substring(s.indexOf("\n")+1,s.length() ),1 ).show();
}  

}

我在一些聯系人中收到了null

我驗證了我的代碼,發現在查詢電話號碼時,我使用的是ContactsContract.CommonDataKinds.Phone.NORMALIZED_NUMBER然后我更改為ContactsContract.CommonDataKinds.Phone.NUMBER ,根據它所說的 android 文檔

用戶輸入的電話號碼。

並且代碼運行良好。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM