无法在Android 2.3.3中获取电话号码

时间:2012-02-16 05:26:52

标签: android phone-number

我写一个简单的应用程序来获取联系人中的电话号码。但是,电话号码返回" null"。

这是我的代码:

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());
    }
}

1 个答案:

答案 0 :(得分: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());
        }

我认为这会对你有帮助。