gpt4 book ai didi

android - 在字符串中获取联系人的手机、工作和家庭号码

转载 作者:行者123 更新时间:2023-11-29 00:17:32 25 4
gpt4 key购买 nike

我想获取联系人(保存在电话簿中)手机号码、工作号码和家庭号码。我想在我的 3 个 edittext View 中设置这些数字。这个怎么做?这是我的代码

Cursor phones = getActivity().getContentResolver().query(
Phone.CONTENT_URI,
null,
Phone.CONTACT_ID + " = " + phoneId,
null,
null
);
while (phones.moveToNext()) {
number = phones.getString(phones.getColumnIndex(Phone.NUMBER));
int type = phones.getInt(phones.getColumnIndex(Phone.TYPE));
if (type == Phone.TYPE_HOME) {
number = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DATA));
return number;
}
if (type == Phone.TYPE_MOBILE) {
number = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DATA));
return number;
}
if (type == Phone.TYPE_WORK) {
number = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DATA));
return number;
}
}

最佳答案

尝试 this链接以获取联系人详细信息,如电话号码、电话簿中的姓名。并查看 jon

发布的答案
public List<Person> getContactList(){
ArrayList<Person> contactList = new ArrayList<Person>();

Uri contactUri = ContactsContract.Contacts.CONTENT_URI;
String[] PROJECTION = new String[] {
ContactsContract.Contacts._ID,
ContactsContract.Contacts.DISPLAY_NAME,
ContactsContract.Contacts.HAS_PHONE_NUMBER,
};
String SELECTION = ContactsContract.Contacts.HAS_PHONE_NUMBER + "='1'";
Cursor contacts = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, PROJECTION, SELECTION, null, null);


if (contacts.getCount() > 0)
{
while(contacts.moveToNext()) {
Person aContact = new Person();
int idFieldColumnIndex = 0;
int nameFieldColumnIndex = 0;
int numberFieldColumnIndex = 0;

String contactId = contacts.getString(contacts.getColumnIndex(ContactsContract.Contacts._ID));

nameFieldColumnIndex = contacts.getColumnIndex(PhoneLookup.DISPLAY_NAME);
if (nameFieldColumnIndex > -1)
{
aContact.setName(contacts.getString(nameFieldColumnIndex));
}

PROJECTION = new String[] {Phone.NUMBER};
final Cursor phone = managedQuery(Phone.CONTENT_URI, PROJECTION, Data.CONTACT_ID + "=?", new String[]{String.valueOf(contactId)}, null);
if(phone.moveToFirst()) {
while(!phone.isAfterLast())
{
numberFieldColumnIndex = phone.getColumnIndex(Phone.NUMBER);
if (numberFieldColumnIndex > -1)
{
aContact.setPhoneNum(phone.getString(numberFieldColumnIndex));
phone.moveToNext();
TelephonyManager mTelephonyMgr;
mTelephonyMgr = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
if (!mTelephonyMgr.getLine1Number().contains(aContact.getPhoneNum()))
{
contactList.add(aContact);
}
}
}
}
phone.close();
}

contacts.close();
}

return contactList;
}

和人员类别

public class Person {
String myName = "";
String myNumber = "";

public String getName() {
return myName;
}

public void setName(String name) {
myName = name;
}

public String getPhoneNum() {
return myNumber;
}

public void setPhoneNum(String number) {
myNumber = number;
}
}

希望对你有帮助。

关于android - 在字符串中获取联系人的手机、工作和家庭号码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25564724/

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