如何从所选联系人中检索公司名称

时间:2019-05-28 03:52:45

标签: android android-contacts

使用ContactsContract,我可以检索并显示所选的手机号码和相关的联系人姓名。

但是没有返回公司名称,而是再次返回了手机号码。

当有多个号码时我用来选择特定电话号码的意图

Intent calContctPickerIntent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
                    calContctPickerIntent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE);
                    startActivityForResult(calContctPickerIntent, 1);

这是主要代码

 @Override
public void onActivityResult(int reqCode, int resultCode, Intent data) {
    super.onActivityResult(reqCode, resultCode, data);

    switch (reqCode) {
        case (1):
            if (resultCode == Activity.RESULT_OK) {
                Uri contctDataVar = data.getData();

                Cursor contctCursorVar = getContentResolver().query(contctDataVar, null, null, null, null);
                if (contctCursorVar.getCount() > 0) {
                    while (contctCursorVar.moveToNext()) {
                        String ContctUidVar = contctCursorVar.getString(contctCursorVar.getColumnIndex(ContactsContract.Contacts._ID));

                        String ContctNamVar = contctCursorVar.getString(contctCursorVar.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
                        String Companyname = contctCursorVar.getString(contctCursorVar.getColumnIndex(ContactsContract.CommonDataKinds.Organization.DATA));


                        Log.i("Names", ContctNamVar);

                        if (Integer.parseInt(contctCursorVar.getString(contctCursorVar.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
                            // Query phone here. Covered next
                            String ContctMobVar = contctCursorVar.getString(contctCursorVar.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                            String Companyname2 = contctCursorVar.getString(contctCursorVar.getColumnIndex(ContactsContract.CommonDataKinds.Organization.COMPANY));

                            mobile.setText(ContctMobVar);
                            custname.setText(ContctNamVar);
                            companyname.setText(Companyname2);

                            Log.i("Number", ContctMobVar);

                        }

                    }
                }
            }
            break;
    }
}

我需要找到一种方法来检索所选联系人下保存的公司名称。

2 个答案:

答案 0 :(得分:0)

您可以尝试以下方法:

ContentResolver mContentResolver = this.getContentResolver();
 Cursor contacts = cr.query(ContactsContract.Contacts.CONTENT_URI,
                        null, null, null, null);

String mContactId = contacts.getString(contacts.getColumnIndex(ContactsContract.Contacts._ID));
String mRawContactId = getRawContactID(mContactId);
String mCompanyName = getCompanyName(mRawContactId);


private String getRawContactID(String contactId) {
        String[] projection = new String[]{ContactsContract.RawContacts._ID};
        String selection = ContactsContract.RawContacts.CONTACT_ID + "=?";
        String[] selectionArgs = new String[]{contactId};
        Cursor c = mContentResolver.query(ContactsContract.RawContacts.CONTENT_URI, projection, selection, selectionArgs, null);
        if (c == null) return null;
        int rawContactId = -1;
        if (c.moveToFirst()) {
            rawContactId = c.getInt(c.getColumnIndex(ContactsContract.RawContacts._ID));
        }
        c.close();
        return String.valueOf(rawContactId);

    }


private String getCompanyName(String rawContactId) {
        try {
            String orgWhere = ContactsContract.Data.RAW_CONTACT_ID + " = ? AND " + ContactsContract.Data.MIMETYPE + " = ?";
            String[] orgWhereParams = new String[]{rawContactId,
                    ContactsContract.CommonDataKinds.Organization.CONTENT_ITEM_TYPE};
            Cursor cursor = mContentResolver.query(ContactsContract.Data.CONTENT_URI,
                    null, orgWhere, orgWhereParams, null);

            if (cursor == null) return null;
            String name = null;
            if (cursor.moveToFirst()) {
                name = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Organization.COMPANY));
            }
            cursor.close();
            return name;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }

    }

答案 1 :(得分:0)

借助@Shiva Snape提供的解决方案,我能够解决问题。

按钮代码以调用联系人应用并选择相关号码

        Intent intent = new Intent(Intent.ACTION_PICK);
        intent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE);
            startActivityForResult(intent, 1);

用于收集联系电话,姓名和公司名称的代码。

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (data != null) {
        Uri uri = data.getData();

        if (uri != null) {
            Cursor c = null;


            try {
                c = getContentResolver().query(uri, new String[]{
                                ContactsContract.CommonDataKinds.Phone.CONTACT_ID,
                                ContactsContract.CommonDataKinds.Phone.TYPE,
                                ContactsContract.CommonDataKinds.Phone.NUMBER,
                                ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME},
                        null, null, null);
                ContentResolver mContentResolver = this.getContentResolver();


                if (c != null && c.moveToFirst()) {
                    String number = c.getString(c.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                    String name = c.getString(c.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
                    String contactId = c.getString(c.getColumnIndex(ContactsContract.CommonDataKinds.Phone.CONTACT_ID));
                    String rawContactId = getRawContactId(contactId);
                    String companyName = getCompanyName(rawContactId);
                    int type = c.getInt(1);
                    showSelectedNumber(type, number);

                }
            } finally {
                if (c != null) {
                    c.close();
                }
            }
        }
    }
}

private String getCompanyName(String rawContactId) {
    try {
        String orgWhere = ContactsContract.Data.RAW_CONTACT_ID + " = ? AND " + ContactsContract.Data.MIMETYPE + " = ?";
        String[] orgWhereParams = new String[]{rawContactId,
                ContactsContract.CommonDataKinds.Organization.CONTENT_ITEM_TYPE};
        Cursor cursor = getContentResolver().query(ContactsContract.Data.CONTENT_URI,
                null, orgWhere, orgWhereParams, null);

        if (cursor == null) return null;
        String Vname = null;
        if (cursor.moveToFirst()) {
            Vname = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Organization.COMPANY));

        }
        cursor.close();
        return Vname;
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}

private String getRawContactId(String contactId) {
    String[] projection = new String[]{ContactsContract.RawContacts._ID};
    String selection = ContactsContract.RawContacts.CONTACT_ID + "=?";
    String[] selectionArgs = new String[]{contactId};
    Cursor c = getContentResolver().query(ContactsContract.RawContacts.CONTENT_URI, projection, selection, selectionArgs, null);
    if (c == null) return null;
    int rawContactId = -1;
    if (c.moveToFirst()) {
        rawContactId = c.getInt(c.getColumnIndex(ContactsContract.RawContacts._ID));
    }
    c.close();
    return String.valueOf(rawContactId);
}


public void showSelectedNumber(int type, String number) {
    Toast.makeText(this, type + ": " + number, Toast.LENGTH_LONG).show();
}
}

感谢Stack Overflow社区为我提供的帮助。希望这对其他人有帮助。