如何从电话簿获取与android中特定联系人链接的所有社交帐户?

时间:2017-09-20 06:08:06

标签: android facebook twitter linkedin android-contacts

我从电话簿中获取了所有设备联系人。现在我想从电话簿中提取的特定联系人中获取链接帐户(facebook,twitter,instagram,LinkedIn)。我该怎么办?

以下是获取联系人的代码。

public Cursor getContactsCursor(FragmentActivity activity) {
        Cursor cursor = null;
        try {
            String selection = ContactsContract.Contacts.IN_VISIBLE_GROUP + "= 0" + " OR " + ContactsContract.Contacts.IN_VISIBLE_GROUP + "= 1";
            String sortOrder = ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC";
            ContentResolver cr = activity.getContentResolver();
            return cr.query(ContactsContract.Contacts.CONTENT_URI, null, selection, null, sortOrder);
        } catch (Exception e) {
            AppLogger.e(Helper.class.getSimpleName(), e.getMessage());
            return cursor;
        }
    }

现在我不知道如何获取与特定联系人链接的帐户(如facebook,linkedin等)

有人可以指导我。

更新 在下面附加的图像中,单击红色突出显示的部分,在浏览器中打开链接的用户配置文件。因此,我愿意获取用于打开用户个人资料页面的字段。

enter image description here

提前致谢。

2 个答案:

答案 0 :(得分:3)

您需要弄清楚您感兴趣的所有帐户的确切MIMETYPE,例如,Google + MIMETYPE是:{{1 }}

您可以转储联系人的所有vnd.android.cursor.item/vnd.googleplus.profile并手动找出您需要的内容:

MIMETYPE

获得所需// make sure you import Data from: ContactsContract.Data String[] projection = new String[] { Data.MIMETYPE }; String selection = Data.CONTACT_ID + " = '"+ contactId + "'"; Cursor cursor = getContentResolver().query(Data.CONTENT_URI, projection, selection, null, null); DatabaseUtils.dumpCursor(cursor); cursor.close(); 的固定列表后,您可以在其中查询特定联系人的信息:

MIMETYPE

<强>更新

对于linkedin,mimetype确实是:// Add more String[] mimetypes = new String[] { "vnd.android.cursor.item/vnd.googleplus.profile", "vnd.android.cursor.item/vnd.com.whatsapp.profile" }; // Usually the interesting info is on the first few fields, modify this if needed String[] projection = new String[] { Data.DATA1, Data.DATA2, Data.DATA3, Data.DATA4 }; String selection = Data.CONTACT_ID + " = '"+ contactId + "' AND " + Data.MIMETYPE + " IN (?,?)"; Cursor cursor = getContentResolver().query(Data.CONTENT_URI, projection, selection, mimetypes, null); DatabaseUtils.dumpCursor(cursor); cursor.close(); 。 关于您没有个人资料网址的评论,在Data1中您应该有一些长ID,例如vnd.android.cursor.item/vnd.com.linkedin.android.profile(大约40个字符)。

然后你的网址是: AC...UQ4 喜欢:https://www.linkedin.com/profile/view?id=<data1Id>

答案 1 :(得分:1)

  

获取第一个帐户信息。获取所有帐户

ArrayList<String> accountsInfo = new ArrayList<String>();
    AccountManager manager = AccountManager.get(this);
    Account[] accounts = manager.getAccounts();
    for (Account account : accounts) {
        String name = account.name;
        String type = account.type;
        int describeContents = account.describeContents();
        int hashCode = account.hashCode();

        accountsInfo.add("name = " + name +
                         "\ntype = " + type +
                         "\n");
    }
    String[] result = new String[accountsInfo.size()];
    accountsInfo.toArray(result);
  

使用以下示例帐户类型。

    /**
     * com.facebook.auth.login /
     * com.linkedin.android
     * flipkart.com
     * com.facebook.messenger
     * com.twitter.android.auth.login
     * com.truecaller.account
     * net.one97.paytm
     * com.whatsapp
     * com.google
     */

    Cursor c = getContentResolver().query(
            ContactsContract.RawContacts.CONTENT_URI,
            new String[]{ContactsContract.RawContacts.CONTACT_ID, ContactsContract.RawContacts.DISPLAY_NAME_PRIMARY},
            ContactsContract.RawContacts.ACCOUNT_TYPE + "= ?",
            new String[]{"com.whatsapp"},
            null);

    final ArrayList<String> myContacts = new ArrayList<String>();
    int contactNameColumn = c.getColumnIndex(ContactsContract.RawContacts.DISPLAY_NAME_PRIMARY);
    while (c.moveToNext()) {
        // You can also read RawContacts.CONTACT_ID to read the
        // ContactsContract.Contacts table or any of the other related ones.
        myContacts.add(c.getString(contactNameColumn));
    }
    Log.d("contact size", String.valueOf(myContacts.size()));