Android 联系信息,包括 facebook 等联系人
我正在使用以下代码。在我的应用程序中,用户可以从联系人中进行选择以查看联系信息(应用程序跳转到相应的联系信息)。 这是代码的结果: 用户点击手机中数据的某人:应用程序跳转到联系信息 用户点击从 Facebook 导入数据的某人:如果这是应用程序的第一次运行,它会强制关闭。如果用户之前点击过手机中数据(手动添加)的某人,则会显示他/她的联系信息。
ContentResolver cr = getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
if (cur.getCount() > 0)
{
while (cur.moveToNext()) {
id_contact = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
name_contact =
cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0)
{
if (name_contact.equals(myArr2_cs[item]))
{
Toast.makeText(getApplicationContext(), "NAME2: " + name_contact, Toast.LENGTH_SHORT).show();
Toast.makeText(getApplicationContext(), "ID2: " + id_contact, Toast.LENGTH_SHORT).show();
if (Integer.parseInt(cur.getString(
cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0)
{
Cursor pCur = cr.query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?", new String[]{id_contact}, null);
id_contact2 = id_contact;
}
}
}
}
Toast.makeText(getApplicationContext(), "id_contact: " + id_contact2, Toast.LENGTH_SHORT).show();
Intent intent_contacts = new Intent(Intent.ACTION_VIEW, Uri.parse("content://contacts/people/" + id_contact2));
startActivity(intent_contacts);
}
此处 我发现访问 Facebook 联系人是允许的。该人还说“我通过 FB Graph api 获取缓存所有联系人姓名/ID 映射 (http:// /graph.facebook.com/me/friends)并使用它来查找 id。”。这意味着什么?
如果我不能解决这个问题,我的程序看起来很蹩脚。应用程序中显示 500 个联系人,但应用程序仅在 40 个左右的情况下跳转到联系人信息。在过去 460 个案例中,它不起作用。
I am using the following code. In my app user can choose from contacts to view there contact info (app jumps to the respective contact info).
This is the result of the code:
User clicks on someone whose data was in the phone: app jumps to the contact info
User clicks on someone whose data was imported from facebook: if this is the first run of the app, it force closes. If user had previously clicked on someone whose data had been in the phone (added manually), it shows his/her contact info.
ContentResolver cr = getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
if (cur.getCount() > 0)
{
while (cur.moveToNext()) {
id_contact = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
name_contact =
cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0)
{
if (name_contact.equals(myArr2_cs[item]))
{
Toast.makeText(getApplicationContext(), "NAME2: " + name_contact, Toast.LENGTH_SHORT).show();
Toast.makeText(getApplicationContext(), "ID2: " + id_contact, Toast.LENGTH_SHORT).show();
if (Integer.parseInt(cur.getString(
cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0)
{
Cursor pCur = cr.query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?", new String[]{id_contact}, null);
id_contact2 = id_contact;
}
}
}
}
Toast.makeText(getApplicationContext(), "id_contact: " + id_contact2, Toast.LENGTH_SHORT).show();
Intent intent_contacts = new Intent(Intent.ACTION_VIEW, Uri.parse("content://contacts/people/" + id_contact2));
startActivity(intent_contacts);
}
Here i found that accessing facebook contacts is permitted. The guy also says that "I fetch an cache all contact names/id mappings via FB Graph api (http://graph.facebook.com/me/friends) and use that for the id lookup.". What does that mean?
If i cannot resolve this issue, my program looks lame. 500 contacts appear in the app but the app jumps only in case of around 40 to the contact info. In the last 460 cases it's not working.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我设法解决了这个问题。事实证明,查询只包括那些有电话号码的人,但我的列表包括所有人。这就是运行导致强制关闭和错误结果的原因。我必须用 HAS_PHONE_NUMBER 删除这两行,一切正常。
I managed to solve this problem. It turned out the query included only those who has phone number, but my list included everyone. This is why the run resulted in force close and false results. I had to remove those two lines with HAS_PHONE_NUMBER and everything is working fine.