如何获得联系信息的Android? [英] How to get Contact Information in Android?
问题描述
我想从我的移动得到接触式图像,姓名,号码使用机器人。即时通讯使用ContactsContract获取这些信息。但我不能。任何人都可以知道学习的具体教程。我想列出在自定义列表视图这些信息。提前致谢。我希望您宝贵的指导会帮我。
PS:在DB我有我的朋友数量。我想手机的通讯录和DB接触使用匹配的手机没有同步。我需要显示所有在ListView匹配的联系人。列表视图具有ImageView的,姓名,号码..
setReminder.numbers.clear();
意向意图=新意图(Intent.ACTION_PICK);
intent.setType(ContactsContract.Contacts.CONTENT_TYPE);
((活动)mcontext).startActivityForResult(意向,1);
和对活动的结果做到这一点。
如果(要求code == 1安培;&安培;结果code == Activity.RESULT_OK)
{
getContactInfo(意向);
}
}保护无效getContactInfo(意向意图)
{
串phoneNumber的=;
//字符串email =; 光标光标=((活动)mcontext).managedQuery(intent.getData(),NULL,NULL,NULL,NULL);
而(cursor.moveToNext())
{
字符串的ContactID = cursor.getString(cursor.getColumnIndex(BaseColumns._ID)); 串hasPhone = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
如果(hasPhone.equalsIgnoreCase(1))
hasPhone =真;
其他
hasPhone =假; 如果(Boolean.parseBoolean(hasPhone))
{
光标手机=((活动)mcontext).getContentResolver()查询(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,空,ContactsContract.CommonDataKinds.Phone.CONTACT_ID +=+的ContactID,NULL,NULL);
//光标电子邮件=((活动)mcontext).getContentResolver()查询(ContactsContract.CommonDataKinds.Email.CONTENT_URI,空,ContactsContract.CommonDataKinds.Email.CONTACT_ID +=+的ContactID,NULL,NULL);
而(phones.moveToNext())
{
phoneNumber的= phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
numbers.add(phoneNumber的);
} phones.close();
光标emailCur =((活动)mcontext).getContentResolver()查询(
ContactsContract.CommonDataKinds.Email.CONTENT_URI,
空值,
ContactsContract.CommonDataKinds.Email.CONTACT_ID +=+的ContactID,
NULL,NULL);
而(emailCur.moveToNext()){
//这将让你得到一些电子邮件地址
//如果电子邮件地址被存储在一个阵列
电子邮件= emailCur.getString(
emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
字符串EMAILTYPE = emailCur.getString(
emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.TYPE));
}
emailCur.close(); 如果(电子邮件== NULL)
{
电子邮件=;
}
} }
如果(phoneNumber的=空&放大器;!&放大器; phoneNumber.length()大于0){ chooseContactArray.clear();
的for(int i = 0; I< numbers.size();我++)
{
chooseContactArray.add(numbers.get(I));
}
adapter.notifyDataSetChanged();
}
cursor.close();}
I want to get Contact Image, Name, Number from my mobile using android. im using ContactsContract to fetch these information. but i can't. can anyone know the exact tutorial for learning. i want to list these info in a custom listview. Thanks in Advance. i hope your valuable guiding will help me.
PS: In the DB i have my friends number. i want to synchronize the phone contact and the DB contact using the matched phone no. and i need to display all the matched contacts in the listview. the listview have imageView, name, number..
setReminder.numbers.clear();
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType(ContactsContract.Contacts.CONTENT_TYPE);
((Activity) mcontext).startActivityForResult(intent, 1);
and on activity result do this
if (requestCode == 1 && resultCode == Activity.RESULT_OK)
{
getContactInfo(intent);
}
}
protected void getContactInfo(Intent intent)
{
String phoneNumber = "";
//String email="";
Cursor cursor = ((Activity) mcontext).managedQuery(intent.getData(), null, null, null, null);
while (cursor.moveToNext())
{
String contactId = cursor.getString(cursor.getColumnIndex(BaseColumns._ID));
String hasPhone = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
if ( hasPhone.equalsIgnoreCase("1"))
hasPhone = "true";
else
hasPhone = "false" ;
if (Boolean.parseBoolean(hasPhone))
{
Cursor phones = ((Activity) mcontext).getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ contactId,null, null);
//Cursor Email = ((Activity) mcontext).getContentResolver().query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, null,ContactsContract.CommonDataKinds.Email.CONTACT_ID +" = "+ contactId,null, null);
while (phones.moveToNext())
{
phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
numbers.add(phoneNumber);
}
phones.close();
Cursor emailCur = ((Activity) mcontext).getContentResolver().query(
ContactsContract.CommonDataKinds.Email.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Email.CONTACT_ID +" = "+ contactId,
null, null);
while (emailCur.moveToNext()) {
// This would allow you get several email addresses
// if the email addresses were stored in an array
email = emailCur.getString(
emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
String emailType = emailCur.getString(
emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.TYPE));
}
emailCur.close();
if(email==null)
{
email="";
}
}
}
if(phoneNumber != null && phoneNumber.length() > 0){
chooseContactArray.clear();
for(int i=0;i<numbers.size();i++)
{
chooseContactArray.add(numbers.get(i));
}
adapter.notifyDataSetChanged();
}
cursor.close();
}
这篇关于如何获得联系信息的Android?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!