2011-03-21 86 views
1

即時通訊使用下面的代碼段來獲取聯繫人姓名和號碼,它在模擬器上工作正常,但是當我在我的Froyo 2.2.1中安裝應用程序時,它只是返回我的名字,而不是返回我返回的數字'null',任何人都可以幫我解決這個問題嗎? 將非常感謝任何解決方案。謝謝從Android Froyo檢索聯繫人2.2

ContentResolver r = getContentResolver(); 
     Cursor cursor = r.query(People.CONTENT_URI, null, null, null, null); 

     // Let activity manage the cursor 
     // startManagingCursor(cursor); 
    // Log.d(TAG, "cursor.getCount()=" + cursor.getCount()); 

     // Get value from content provider 
     int nameIndex = cursor.getColumnIndex(People.NAME); 
     int numberIndex = cursor.getColumnIndex(People.NUMBER);//OrThrow(People.NUMBER); 

     cursor.moveToFirst(); 
     StringBuilder s = new StringBuilder(); 
     do { 
      String name = cursor.getString(nameIndex); 
      String number = cursor.getString(numberIndex); 
      s.append(name+ ": " + number + "\n"); 
     } while (cursor.moveToNext()); 

回答

2

People API已棄用嘗試使用已爲Android 2.0+引入的ContactsContract API。

你可以看一下this blog post about using the contactscontractapi documentation

+1

@arun如果你希望你的應用程序支持舊的API和新API的雙方,通過這個[鏈接](http://developer.android.com/resources/文章/ contacts.html)。請參閱**在同一應用程序中支持新舊API ** – Udayan 2011-03-21 12:41:58

+0

謝謝@senola!現在查看ContactsContract。 – arun 2011-03-22 03:57:26

+0

@Udayan謝謝,我會研究它! – arun 2011-03-22 03:57:48