ssummeraw 2013-07-02 08:20 采纳率: 10%
浏览 2508
已采纳

如何获取联系人的电话号码?

代码如下:

String[] columns = {ContactsContract.Contacts._ID, ContactsContract.Contacts.DISPLAY_NAME, ContactsContract.Contacts.HAS_PHONE_NUMBER};
Cursor cursor = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, columns, null, null, null);
int ColumeIndex_ID = cursor.getColumnIndex(ContactsContract.Contacts._ID);
int ColumeIndex_DISPLAY_NAME = cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);
int ColumeIndex_HAS_PHONE_NUMBER = cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER);
while(cursor.moveToNext()) 
{   
    String id = cursor.getString(ColumeIndex_ID);
    String name = cursor.getString(ColumeIndex_DISPLAY_NAME);
    String has_phone = cursor.getString(ColumeIndex_HAS_PHONE_NUMBER);
    if(!has_phone.endsWith("0")) 
    {
        System.out.println(name);
        GetPhoneNumber(id);
    }           
}
 cursor.close();
public String GetPhoneNumber(String id) 
{
    String number = "";
    Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone._ID + " = " + id, null, null);
     if(phones.getCount() > 0) 
    {
        while(phones.moveToNext()) 
        {
            number = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
        }
        System.out.println(number);
    }
    phones.close();
    return number;
}

我能获取联系人的姓名,但是不能在GetPhoneNumber()中获取电话号码。
phones.getCount() 总是等于0,如何修改?

  • 写回答

2条回答 默认 最新

  • myc_100 2013-07-03 03:03
    关注

    Android Contact API 2.0

    ContentResolver cr = getContentResolver();
    Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null,
        "DISPLAY_NAME = '" + NAME + "'", null, null);
    if (cursor.moveToFirst()) {
        String contactId =
            cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
        //
        //  Get all phone numbers.
        //
        Cursor phones = cr.query(Phone.CONTENT_URI, null,
            Phone.CONTACT_ID + " = " + contactId, null, null);
        while (phones.moveToNext()) {
            String number = phones.getString(phones.getColumnIndex(Phone.NUMBER));
            int type = phones.getInt(phones.getColumnIndex(Phone.TYPE));
            switch (type) {
                case Phone.TYPE_HOME:
                    // do something with the Home number here...
                    break;
                case Phone.TYPE_MOBILE:
                    // do something with the Mobile number here...
                    break;
                case Phone.TYPE_WORK:
                    // do something with the Work number here...
                    break;
                }
        }
        phones.close();
     }
    

    cursor.close();

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥30 python代码,帮调试,帮帮忙吧
  • ¥15 #MATLAB仿真#车辆换道路径规划