2011-03-21 63 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