2012-02-16 65 views
1

我編寫了一個簡單的應用程序來獲取聯繫人中的電話號碼。但是,電話號碼返回「null」。無法在Android 2.3.3中獲取電話號碼

這裏是我的代碼:

private void queryContactPhoneNumber() { 
    // TODO Auto-generated method stub 
    String[] cols = new String[] {People.NAME, People.NUMBER}; 
    Uri myContacts = People.CONTENT_URI; 
    Cursor mqCur = managedQuery(myContacts, cols, null, null, null); 
    if(mqCur.moveToFirst()) 
    { 
     String myname = null; 
     String mynumber = null; 
     do 
     { 
      myname = mqCur.getString(mqCur.getColumnIndex(People.NAME)); 
      mynumber = mqCur.getString(mqCur.getColumnIndex(People.NUMBER)); 
      Toast.makeText(this, myname + " " + mynumber, Toast.LENGTH_SHORT).show(); 
     } 
     while(mqCur.moveToNext()); 
    } 
} 

回答

2

試試這個,

Uri myContacts = ContactsContract.CommonDataKinds.Phone.CONTENT_URI ;//People.CONTENT_URI; 
     Cursor mqCur = managedQuery(myContacts, null, null, null, null); 
     if(mqCur.moveToFirst()) 
     { 
      String myname = null; 
      String mynumber = null; 
      do 
      { 
       myname = mqCur.getString(mqCur.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME)); 
       mynumber = mqCur.getString(mqCur 
         .getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.NUMBER)); 
       Toast.makeText(this, myname + " " + mynumber, Toast.LENGTH_SHORT).show(); 
      } 
      while(mqCur.moveToNext()); 
     } 

我認爲這會幫助你。

+0

它工作:),非常感謝。但是,你想爲我解釋我的代碼有什麼問題嗎? – 2012-02-16 08:23:58

+0

是的, 這裏您使用了People.CONTENT_URI(請參閱[這裏](http://developer.android.com/reference/android/provider/Contacts.People.html#CONTENT_URI))已被棄用。所以它不會工作。替換爲ContactsContract(請參閱[這裏](http://developer.android.com/reference/android/provider/ContactsContract.html))。 – Satheesh 2012-02-16 09:01:11

+0

非常感謝。現在我明白了 :) – 2012-02-16 17:01:21