gpt4 book ai didi

android - 我怎样才能只从安卓电话簿中获取所有手机号码?

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

这是我的代码,它为我提供了我的 android 设备的所有号码,例如手机号码、whatsapp 号码和固定电话号码。但我只想将安卓电话簿中的手机号码和座机号码添加到我的应用程序中。如何只获取每个联系人姓名的手机号码和座机号码?

private void addContactsInList() {
// TODO Auto-generated method stub

Cursor cursor = getContentResolver().query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null, null, null, null);

try {
ContactsListClass.phoneList.clear();
} catch (Exception e) {

}

while (cursor.moveToNext()) {
String phoneName = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
String phoneNumber = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
int phoneId = cursor.getInt(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.CONTACT_ID));


Contact cp = new Contact();

cp.setName(phoneName);
cp.setNumber(phoneNumber);
cp.setId(phoneId);


ContactsListClass.phoneList.add(cp);
// Log.e("add to list", "content" + phoneId +phoneName +phoneNumber);
}
cursor.close();

lv = new ListView(context);

lv.setLayoutParams(new LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT));


llContainer.addView(lv);

Collections.sort(ContactsListClass.phoneList, new Comparator<Contact>() {
@Override
public int compare(Contact lhs,
Contact rhs) {
return lhs.getName().compareTo(
rhs.getName());
}
});

contactAdapter = new ContactsAdapter(MainActivity.this,
ContactsListClass.phoneList);
lv.setAdapter(contactAdapter);

最佳答案

在你的 while 循环中,放这样的东西:

    while (phoneCursor.moveToNext()) {
phoneNumber = phoneCursor.getString(phoneCursor.getColumnIndex(NUMBER));
//String number = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
int type = phoneCursor.getInt(phoneCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));
switch (type) {
case ContactsContract.CommonDataKinds.Phone.TYPE_HOME:
// do something with the Home number here...
break;
case ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE:
output.append("\n Phone number:" + phoneNumber);
break;
case ContactsContract.CommonDataKinds.Phone.TYPE_WORK:
// do something with the Work number here...
break;
}
}

this回答

关于android - 我怎样才能只从安卓电话簿中获取所有手机号码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32712987/

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