如何从电话簿中获取与 android 中特定联系人关联的所有社交帐户?
how to get all social accounts linked with the particular contact in android from phonebook?
我从电话簿中获取了所有设备联系人。现在我想从从 phonebook.What 获取的特定联系人获取关联帐户(facebook、twitter、instagram、LinkedIn)的 url,我应该怎么做?
这是获取联系人的代码。
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 等)。
谁能指导一下。
更新:
在下面的附图中,单击以红色突出显示的部分,将在浏览器中打开用户配置文件中的链接。因此我愿意获取用于打开用户个人资料页面的字段。
提前致谢。
Get first account info. fetch all accounts
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);
Use below sample account type.
/**
* 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()));
您需要计算出您感兴趣的所有帐户的确切 MIMETYPE
,例如,Google+ 的 MIMETYPE
是: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
的固定列表后,您可以在其中查询特定联系人的信息:
// 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();
更新:
对于 linkedin,mimetype 确实是:vnd.android.cursor.item/vnd.com.linkedin.android.profile
。
关于您关于没有配置文件 url 的评论,在 Data1 中您应该有一些长 ID,例如 AC...UQ4
(大约 40 个字符)。
那么你的url是:
https://www.linkedin.com/profile/view?id=<data1Id>
喜欢:https://www.linkedin.com/profile/view?id=AC...UQ4
我从电话簿中获取了所有设备联系人。现在我想从从 phonebook.What 获取的特定联系人获取关联帐户(facebook、twitter、instagram、LinkedIn)的 url,我应该怎么做?
这是获取联系人的代码。
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 等)。
谁能指导一下。
更新: 在下面的附图中,单击以红色突出显示的部分,将在浏览器中打开用户配置文件中的链接。因此我愿意获取用于打开用户个人资料页面的字段。
提前致谢。
Get first account info. fetch all accounts
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);
Use below sample account type.
/**
* 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()));
您需要计算出您感兴趣的所有帐户的确切 MIMETYPE
,例如,Google+ 的 MIMETYPE
是: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
的固定列表后,您可以在其中查询特定联系人的信息:
// 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();
更新:
对于 linkedin,mimetype 确实是:vnd.android.cursor.item/vnd.com.linkedin.android.profile
。
关于您关于没有配置文件 url 的评论,在 Data1 中您应该有一些长 ID,例如 AC...UQ4
(大约 40 个字符)。
那么你的url是:
https://www.linkedin.com/profile/view?id=<data1Id>
喜欢:https://www.linkedin.com/profile/view?id=AC...UQ4