Android 获取手机联系人

来源:互联网 发布:notes是什么软件 编辑:程序博客网 时间:2024/06/10 15:09
private void getContent() {        ContentResolver cr = getContentResolver();        Cursor c1 = cr.query(Contacts.CONTENT_URI, new String[] { Contacts._ID, Contacts.DISPLAY_NAME }, null, null, null);        if (c1 != null) {            while (c1.moveToNext()) {                int id = c1.getInt(c1.getColumnIndex("_id"));                Log.i("info", "name:" + c1.getString(c1.getColumnIndex("display_name")));                Cursor c2 = cr.query(Phone.CONTENT_URI, new String[] { Phone.NUMBER, Phone.TYPE }, Phone.CONTACT_ID + "=" + id, null, null);                if (c2 != null) {                    while (c2.moveToNext()) {                        int type2 = c2.getInt(c2.getColumnIndex(Phone.TYPE));                        if (type2 == Phone.TYPE_HOME)                            Log.i("info", "家庭电话:" + c2.getString(c2.getColumnIndex(Phone.NUMBER)));                        else if (type2 == Phone.TYPE_MOBILE)                            Log.i("info", "手机号码:" + c2.getString(c2.getColumnIndex(Phone.NUMBER)));                    }                    c2.close();                }                Cursor c3 = cr.query(Email.CONTENT_URI, new String[] { Email.DATA, Email.TYPE }, Email.CONTACT_ID + "=" + id, null, null);                if (c3 != null) {                    while (c3.moveToNext()) {                        int type3 = c3.getInt(c3.getColumnIndex(Email.TYPE));                        if (type3 == Email.TYPE_WORK)                            Log.i("info", "Email:" + c3.getString(c3.getColumnIndex(Email.DATA)));                    }                    c3.close();                }            }            c1.close();        }    }

输出的Log为:

name:张三 手机号码:1820281****
name:李四 手机号码:1518443****

0 0
原创粉丝点击