Android Java 检查联系人是否有姓名

问题描述

我正在尝试通过 ContentResolver 查询在电话中取得联系。我可以获取联系人,但我无法检查联系人是否有姓名。

我的情况是当联系人没有姓名时,我会输入一个空字符串,但 ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME 返回其号码而不是空。

这是我获取联系人的代码

Cursor managedCursor = activity.getContentResolver()
                .query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,new String[]{ContactsContract.CommonDataKinds.Phone._ID,ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,ContactsContract.CommonDataKinds.Phone.NUMBER},"(" + ContactsContract.CommonDataKinds.Phone.NUMBER + " LIKE ? OR " + ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " LIKE ?)",new String[]{"%" + keyword + "%","%" + keyword + "%"},ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " COLLATE NOCASE");
        String firstChar = "";
        contacts.clear();
        while (managedCursor != null && managedCursor.moveToNext()) {
            String name = managedCursor.getString(managedCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
            String phoneNumber = managedCursor.getString(managedCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));

            // Cleanup the phone number
            phoneNumber = phoneNumber.replaceAll("[()\\s-]+","");
            if (name.length() > 0) {
                String first = name.charAt(0) + "";
                if (!first.equalsIgnoreCase(firstChar)) {
                    firstChar = first + "";
                    contacts.add(new Contact(Contact.TYPE_HEADER,firstChar));
                }
            } else {

                //when name length is 0 or name is null,I want to replace it with empty string
                name = "";
                contacts.add(new Contact(Contact.TYPE_HEADER,"#"));
            }
            Contact contact = new Contact(phoneNumber,name);
            if (!contacts.contains(contact)) {
                contacts.add(contact);
            }
        }
        setupAdapter(contacts,keyword);
        if(managedCursor != null){
            managedCursor.close();
        }

我以前将它替换为空字符串的方法是将它的 phoneNumber 与 name 进行比较。如果它们相同,我将其替换为空字符串。但是当无名联系人有多个号码时,它无法检测。

有没有人知道如何检测联系人是否有姓名?

谢谢

解决方法

我曾与它合作过。让我与你分享it(git repo)

如果要检查用户名是否可用。比,你写了 http://localhost:8000/complaints-input/72/72067183

if(name.isEmpty){}

通过这种方式我获取数据并将其发送到适配器。以上源代码用于保存联系人列表。我提供了一些 Cursor c; ArrayList<HashMap<String,String>> list = new ArrayList<HashMap<String,String>>(); c=getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,ContactsContract.Contacts.DISPLAY_NAME+" ASC "); while (c.moveToNext()){ //get contact list String name=c.getString(c.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME)); String number=c.getString(c.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); //put value into Hashmap HashMap<String,String> user_data = new HashMap<>(); user_data.put(Constant.NAME,name); user_data.put(Constant.PHONE_NUMBER,number); list.add(user_data); } // call the constructor of CustomAdapter to send the reference and data to Adapter CustomAdapterOfReadContacts customAdapter = new CustomAdapterOfReadContacts(HomeActivity.this,list); recyclerView.setAdapter(customAdapter); // set the Adapter to RecyclerView c.close(); 的源代码。

call list
,

您需要同时查询 PhoneStructuredName

您可以通过直接在 Data 表上查询来获取这两种数据类型。

Map<Long,List<String>> contacts = new HashMap<Long,List<String>>();

String[] projection = {Data.CONTACT_ID,Data.DISPLAY_NAME,Data.MIMETYPE,Data.DATA1,Data.DATA2,Data.DATA3};
String selection = Data.MIMETYPE + " IN ('" + Phone.CONTENT_ITEM_TYPE + "','" + StructuredName.CONTENT_ITEM_TYPE + "')";
Cursor cur = cr.query(Data.CONTENT_URI,projection,selection,null);

while (cur != null && cur.moveToNext()) {
    long id = cur.getLong(0);
    String name = cur.getString(1); // display name (can be phone or email if name is missing)
    String mime = cur.getString(2); // phone / structured-name
    String data1 = cur.getString(3); // will contain phone number if this is a phone row
    String data2 = cur.getString(4); // will contain first name if this is a name row
    String data3 = cur.getString(5); // will contain family name if this is a name row

    String kind = "unknown";

    // get an existing "contact" from our HashMap,or create a new one
    List<String> infos;
    if (contacts.containsKey(id)) {
        infos = contacts.get(id);
    } else {
        infos = new ArrayList<String>();
        infos.add("display-name = " + name);
        contacts.put(id,infos);
    }

    switch (mime) {
        case Phone.CONTENT_ITEM_TYPE: 
            kind = "phone"; 
            // add the phone to the contact
            infos.add(kind + " = " + data1);
            break;
        case StructuredName.CONTENT_ITEM_TYPE: 
            kind = "name";
            if (!TextUtils.isEmpty(data2) || !TextUtils.isEmpty(data3)) {
                // found a real name! add it to the "contact"
                infos.add(kind + " = " + data2 + " " + data3);
            }
            break;
    }

    Log.d(TAG,"got " + id + "," + name + "," + kind + " - " + data1 + " - " + data2 + " - " + data3 + " - " + hasName);
}

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...