zoukankan      html  css  js  c++  java
  • android:获取联系人信息(姓名和电话)

    public void getUserInfo(){
           Cursor cursor = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
           while(cursor.moveToNext()){
               String id = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
               String name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
               Log.d(TAG    , "Name is : "+name);
               int isHas = Integer.parseInt(cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER)));
               if(isHas>0){
                   Cursor c = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,
                            ContactsContract.CommonDataKinds.Phone.CONTACT_ID+ " = " + id,null,null);
                   while(c.moveToNext()){
                       String number = c.getString(c.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                       Log.d(TAG    , "Number is : "+number);
                   }
                   c.close();
               }
           }
           cursor.close();
    }
    有时候也会遇到根据号码找联系人的情况,及时提示用户。
    private String getNameFromPhone(String number) {
            String name = null;
            String[] projection = { ContactsContract.PhoneLookup.DISPLAY_NAME,
                    ContactsContract.CommonDataKinds.Phone.NUMBER };
    
            Cursor cursor = this.getContentResolver().query(
                    ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                    projection, // Which columns to return.
                    ContactsContract.CommonDataKinds.Phone.NUMBER + " = '"
                            + number + "'", // WHERE clause.
                    null, // WHERE clause value substitution
                    null); // Sort order.
    
            if (cursor == null) {
                Log.d(TAG, "getPeople null");
                return null;
            }
            Log.d(TAG, "getPeople cursor.getCount() = " + cursor.getCount());
            for (int i = 0; i < cursor.getCount(); i++) {
                cursor.moveToPosition(i);
    
                int nameFieldColumnIndex = cursor
                        .getColumnIndex(ContactsContract.PhoneLookup.DISPLAY_NAME);
                name = cursor.getString(nameFieldColumnIndex);
                Log.i(TAG, "" + name + " .... " + nameFieldColumnIndex);
                
                
            }
            cursor.close();
            return name;
            
        }
  • 相关阅读:
    UVA748 Exponentiation 解题报告
    uva465 解题报告
    UVA 10494 If We Were a Child Again 解题报告
    【今日所得】1.29。。。
    题型总结之高精度
    cf 163 div2 c题 YY 思路
    寒假计划
    Facebook Hacker Cup 2013 Qualification Round 解题报告
    戴戒指的含义(以后要结婚的必看)
    男人心疼女人的13种方式
  • 原文地址:https://www.cnblogs.com/pengqinping/p/2599503.html
Copyright © 2011-2022 走看看