如何从设备的联系人列表中获取用户的连接?

时间:2014-12-15 08:41:38

标签: android

是否可以从设备的联系人列表中获取任何联系人的连接?连接像谷歌+帐户的电子邮件ID,和其他第三方API,我正在搜索它的查询。但没有得到适当的解决方案。所以请朋友们帮我解决这些问题。提前致谢

3 个答案:

答案 0 :(得分:3)

在联系人提供商中,"连接"联系人只是RawContacts中针对该特定CONTACT_ID的所有条目。他们的数据存储在Data表中。

因此,要获取联系人所有连接的所有信息,只需使用适当的过滤器查询Data表即可。例如:

private static void getConnections(Context context, int contactId)
{
    // Read all data for contactId
    String selection = RawContacts.CONTACT_ID + " = ?";
    String[] selectionArgs = new String[] { String.valueOf(contactId) };

    Cursor c = context.getContentResolver().query(ContactsContract.Data.CONTENT_URI, null, selection, selectionArgs, null);
    while (c.moveToNext())
    {
        String accountType = c.getString(c.getColumnIndexOrThrow(ContactsContract.RawContacts.ACCOUNT_TYPE));
        String accountName = c.getString(c.getColumnIndexOrThrow(ContactsContract.RawContacts.ACCOUNT_NAME));
        String dataMimeType = c.getString(c.getColumnIndexOrThrow(ContactsContract.Data.MIMETYPE));
        String dataValue = c.getString(c.getColumnIndexOrThrow(ContactsContract.Data.DATA1));

        Log.d("contactInfo", accountName + " (" + accountType + ") - " + dataMimeType + " - " + dataValue);
    }

    c.close();
}

您可以使用限制性更强的selection / selectionArgs参数(例如针对特定ACCOUNT_TYPEMIMETYPE等)过滤此数据。

当然,您需要AndroidManifest.xml中的相应权限:

<uses-permission android:name="android.permission.READ_CONTACTS" />

答案 1 :(得分:1)

Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,null, null, null, null);
    if (cur.getCount() > 0) {
        while (cur.moveToNext()) {
            String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
            Cursor cur1 = cr.query( 
                    ContactsContract.CommonDataKinds.Email.CONTENT_URI, null,
                    ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?", 
                            new String[]{id}, null); 
            while (cur1.moveToNext()) { 
                //to get the contact names
                String name=cur1.getString(cur1.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
                Log.e("Name :", name);
                String email = cur1.getString(cur1.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
                Log.e("Email", email);
                if(email!=null){
                    names.add(name);
                }
            } 
            cur1.close();
        }
    }

答案 2 :(得分:0)

这是解决方案,它使用类似Whatsapp的第三方API:

Cursor connectionCur = cr.query(ContactsContract.RawContacts.CONTENT_URI,
                            new String[]{ContactsContract.RawContacts.ACCOUNT_NAME, ContactsContract.RawContacts.ACCOUNT_TYPE},
                            ContactsContract.RawContacts.CONTACT_ID +"=?",
                            new String[]{String.valueOf(id)},
                            null);
System.out.println("TOTAL NUMBERS OF COUNTS====================="+connectionCur.getCount());
if (connectionCur != null && connectionCur.getCount() >0)
{
    while (connectionCur.moveToNext()) 
    {
        String accountName = connectionCur.getString(connectionCur.getColumnIndex(ContactsContract.RawContacts.ACCOUNT_NAME));
        String accountType = connectionCur.getString(connectionCur.getColumnIndex(ContactsContract.RawContacts.ACCOUNT_TYPE));

        if(accountType.contains("whatsapp"))
        {
            Toast.makeText(LoginScreen.this, "whatsapp == "+accountType, Toast.LENGTH_SHORT).show();
        }
        else if(accountType.contains("google"))
        {
            Toast.makeText(LoginScreen.this, "google == "+accountType, Toast.LENGTH_SHORT).show();
        }
        else if(accountType.contains("facebook"))
        {
            Toast.makeText(LoginScreen.this, "facebook == "+accountType, Toast.LENGTH_SHORT).show();
        }
        else if(accountType.contains("twitter"))
        {
            Toast.makeText(LoginScreen.this, "twitter == "+accountType, Toast.LENGTH_SHORT).show();
        }
        else if(accountType.contains("linkedin"))
        {
            Toast.makeText(LoginScreen.this, "linkedin == "+accountType, Toast.LENGTH_SHORT).show();
        }
        System.out.println("a========="+accountName+"   b====="+accountType);   
    }
}