Java 在Android中列出组中的联系人

Java 在Android中列出组中的联系人,java,android,android-contentprovider,Java,Android,Android Contentprovider,我正在尝试列出联系人(姓名+手机号码)。 以下是我实际使用的代码: private void listContactLookupKey() { final String[] PROJECTIONS = { ContactsContract.Contacts._ID, ContactsContract.Contacts.LOOKUP_KEY, Build.VERSION.SDK_INT

我正在尝试列出联系人(姓名+手机号码)。 以下是我实际使用的代码:

    private void listContactLookupKey() {
        final String[] PROJECTIONS = {
            ContactsContract.Contacts._ID,
            ContactsContract.Contacts.LOOKUP_KEY,
            Build.VERSION.SDK_INT
                >= Build.VERSION_CODES.HONEYCOMB ?
                ContactsContract.Contacts.DISPLAY_NAME_PRIMARY :
                ContactsContract.Contacts.DISPLAY_NAME,
            ContactsContract.CommonDataKinds.GroupMembership.GROUP_ROW_ID
        };
        final String SELECTION =
            ContactsContract.Data.MIMETYPE  + " = '"
            + ContactsContract.CommonDataKinds.GroupMembership.CONTENT_ITEM_TYPE + "'"
            + " AND " +
            ContactsContract.CommonDataKinds.GroupMembership.GROUP_ROW_ID + " = "
            + 48;

        Cursor c = mContentResolver.query(
            ContactsContract.Data.CONTENT_URI,
            PROJECTIONS,
            SELECTION, null,
            null
        );
        mLookupKeys = new ArrayList<String>( c.getCount() );
        for(c.moveToFirst(); ! c.isAfterLast(); c.moveToNext()) {
            android.util.Log.i("CONTACTS",
                "_ID=" + c.getLong(0) +", "+
                "LOOKUP_KEY=" + c.getString(1) +", "+
                "NAME="+c.getString(2) +", "+
                "GROUP_ROW_ID=" + c.getLong(3)
            );
            mLookupKeys.add(c.getString(1));
        }
    }
    private void listContactsInGroup() {
        final String[] PROJECTIONS = {
            ContactsContract.Contacts._ID,
            Build.VERSION.SDK_INT
                >= Build.VERSION_CODES.HONEYCOMB ?
                ContactsContract.Contacts.DISPLAY_NAME_PRIMARY :
                ContactsContract.Contacts.DISPLAY_NAME,
            ContactsContract.CommonDataKinds.Phone.NORMALIZED_NUMBER,
            ContactsContract.Contacts.LOOKUP_KEY
        };
        StringBuilder argBuilder = new StringBuilder();
        for(int i=0; i<mLookupKeys.size(); i++) {
            argBuilder.append("'" + mLookupKeys.get(i) + "'");
            if( i < mLookupKeys.size()-1 )
                argBuilder.append(", ");
        }
        android.util.Log.i("CONTACT", "arg= "+argBuilder.toString());
        String SELECTION =
            ContactsContract.Data.MIMETYPE + " = '"
            + ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE
            + "' AND " +
            ContactsContract.CommonDataKinds.Phone.TYPE + " = '"
            + ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE
            + "' AND " +
            ContactsContract.Contacts.LOOKUP_KEY + " IN ("+argBuilder.toString()+")";

        String[] arg = {argBuilder.toString() };
        Cursor c = mContentResolver.query(
            ContactsContract.Data.CONTENT_URI,
            PROJECTIONS,
            SELECTION, null,
            null
        );
        for(c.moveToFirst(); ! c.isAfterLast(); c.moveToNext()) {
            android.util.Log.i("CONTACTS",
                "NAME="+c.getString(1) +", "+
                "NORMALIZED_NUMBER=" + c.getString(2) +", "+
                "LOOKUP_KEY=" + c.getString(3)
            );
        }
    }

    @Override
    public void run() {
        listContactLookupKey();
        listContactsInGroup();
    }
可能是由于多个帐户出现相同的号码(谷歌、Whatsapp等)

其次,代码似乎太长了。我是不是错过了一条捷径

我读过Android开发者关于联系人提供商的话题,很难理解。 根据表格结构,目的是首先将联系人的LOOKUP_键放入一个组中,然后为每个first get LOOKUP_键获取联系人姓名和手机号码

请问有人能帮我吗

谢谢

"Germain D." "+33......"
"Germain D." "+33......"
"Other Person" "12356"