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

    由于之前people过时,就不能使用这样的方法了,就采用下面的方法:

     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;

    }



  • 相关阅读:
    shell脚本while read line的使用
    shell 一次性赋值多个变量
    Docker Volume
    nginx反向代理与负载均衡
    lua中 table.getn(t) 、#t、 table.maxn(t) 这三个什么区别?
    pv绑定pvc
    使用brew services管理服务
    Kubernetes中强制删除Pod、namespace
    shell 中的${},##, %% , :- ,:+, ? 的使用
    petalinux中fsbl源码
  • 原文地址:https://www.cnblogs.com/slider/p/2290174.html
Copyright © 2011-2022 走看看