0

我正在使用以下代码使用联系人 ID 在 android 中获取联系人详细信息。但是,该代码仅适用于 android 设备 2.3.5 版而不是 2.3.3 或 3 以上版本。以下是我的代码,其中contactId 是该特定联系人的 id。执行不移动到if (phone.moveToFirst()) {}部分是不是查询中有问题?如果是这样,那么为什么它在 2.3.5 版上运行良好?任何帮助将不胜感激..谢谢

public String queryAllPhoneNumbersForContact(int contactId) 
{
    String[] projection = new String[] { Phone.NUMBER, Phone.TYPE };

    @SuppressWarnings("deprecation")
    Cursor phone = managedQuery(Phone.CONTENT_URI, 
            projection,
            Data.CONTACT_ID + "=?",
            new String[] { String.valueOf(contactId) }, null);

    if (phone.moveToFirst())
    {
        int contactNumberColumnIndex = phone.getColumnIndex(Phone.NUMBER);
        int contactTypeColumnIndex = phone.getColumnIndex(Phone.TYPE);

        while (!phone.isAfterLast()) 
        {
            String number = phone.getString(contactNumberColumnIndex).trim();
            int type = phone.getInt(contactTypeColumnIndex);

            System.out.println("number" + number);
            System.out.println("Type"   + getString(Phone.getTypeLabelResource(type)));

            phone.moveToNext();
            if ((getString(Phone.getTypeLabelResource(type)).equals("Mobile"))) 
            {
                Log.e("WorkNo. : ", number);
                return number;
            }
        }
    }
    phone.close();
    return null;
}
4

1 回答 1

0

哪些设备完全不起作用?我建议您使用适用于主要版本和设备的 2.2 版本

于 2013-01-09T16:33:39.710 回答