gpt4 book ai didi

android - 在不扩展 Activity 的情况下从联系人获取电子邮件和电话号码

转载 作者:太空宇宙 更新时间:2023-11-03 12:42:04 26 4
gpt4 key购买 nike

我正在尝试获取联系人的电子邮件地址和电话号码。我的类(class)没有extend Activity,现在我卡住了。

我怎样才能克服这个问题?

最佳答案

用于获取电子邮件联系人

public void getEmailContacts() 
{
try
{
String name;

ContentResolver cr = getContentResolver();
cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null,null, null, null);

emailIndex=0;
if (cur.getCount() > 0)
{
ArrayList<String> emailNameList=new ArrayList<String>();
ArrayList<String> emailPhoneList=new ArrayList<String>();
while (cur.moveToNext())
{
String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));

Cursor emails = getContentResolver().query(ContactsContract.CommonDataKinds.Email.CONTENT_URI,null,
ContactsContract.CommonDataKinds.Email.CONTACT_ID+ " = " + id, null, null);
while (emails.moveToNext())
{
// This would allow you get several email addresses
String emailAddress = emails.getString(emails.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
Log.v(name+"==>", emailAddress);
if ((!emailAddress.equalsIgnoreCase(""))&&(emailAddress.contains("@")))
{
emailNameList.add(name);
emailPhoneList.add(emailAddress);
emailIndex++;
}
}
emails.close();
}

获取手机联系人

String name;

ContentResolver cr = getContentResolver();

Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null,null, null, null);

if (cur.getCount() > 0)
{
ArrayList<String> phoneNameList=new ArrayList<String>();
ArrayList<String> PhoneList=new ArrayList<String>();

while (cur.moveToNext())
{
String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0)
{
phoneNameList.add(name);
Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID+ " = ?", new String[] { id }, null);

while (pCur.moveToNext())
{

PhoneList.add(pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)));

}
pCur.close();
}
}

关于android - 在不扩展 Activity 的情况下从联系人获取电子邮件和电话号码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6223811/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com