gpt4 book ai didi

android - 仅从 SIM 卡获取本地电话簿联系人 android

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

我想知道是否可以获取仅存在于 SIM 卡或电话簿中的联系人。现在我正在使用以下代码来获取联系人,它会获取所有联系人,甚至是我的 gmail 和 Facebook 联系人。

Cursor cursor = getContentResolver().query(
ContactsContract.Contacts.CONTENT_URI, null, null, null, Phone.DISPLAY_NAME + " ASC");
if (cursor.getCount() > 0)
{
while (cursor.moveToNext())
{

PhoneBookUserEntity user = new PhoneBookUserEntity();
// Pick out the ID, and the Display name of the
// contact from the current row of the cursor
user.setId(cursor.getString(cursor.getColumnIndex(BaseColumns._ID)));
user.setPhoneBookName(cursor.getString(cursor.getColumnIndex(
ContactsContract.Contacts.DISPLAY_NAME)));
if(user.getphonebookname().length() > 4)
username = user.getphonebookname();//.substring(0,4);
else
username = user.getphonebookname();//.substring(0,1);
String hasPhone = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
// if (Boolean.parseBoolean(hasPhone)) {
Cursor phones = getContentResolver().query( ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ user.getId(), null, null);
while (phones.moveToNext()) {
user.sePhoneNumber(phones.getString(phones.getColumnIndex( ContactsContract.CommonDataKinds.Phone.NUMBER)));
}
phones.close();
//}
// user.sePhoneNumber(cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)));

Cursor emails = getContentResolver().query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, null, ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = " + user.getId(), null, null);
while (emails.moveToNext()) {
// This would allow you get several email addresses
user.setEmailAddress(emails.getString(emails.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA)));
}
emails.close();
user.setImageURI(getPhotoUri(user.getId()));
SearchContactsActivity.this.runOnUiThread(new Runnable() {

@Override
public void run() {
// TODO Auto-generated method stub
_progressDialog.setMessage("Copying over your local phone book. Retrieving contact information for \n"+ username.toUpperCase());
}
});
arraylist.add(user);
}

}
cursor.close();

最佳答案

仅对于 Sim 联系人,您可以使用以下代码

private void allSIMContact()
{
try
{
String ClsSimPhonename = null;
String ClsSimphoneNo = null;

Uri simUri = Uri.parse("content://icc/adn");
Cursor cursorSim = this.getContentResolver().query(simUri,null,null,null,null);

Log.i("PhoneContact", "total: "+cursorSim.getCount());

while (cursorSim.moveToNext())
{
ClsSimPhonename =cursorSim.getString(cursorSim.getColumnIndex("name"));
ClsSimphoneNo = cursorSim.getString(cursorSim.getColumnIndex("number"));
ClsSimphoneNo.replaceAll("\\D","");
ClsSimphoneNo.replaceAll("&", "");
ClsSimPhonename=ClsSimPhonename.replace("|","");

Log.i("PhoneContact", "name: "+ClsSimPhonename+" phone: "+ClsSimphoneNo);
}
}
catch(Exception e)
{
e.printStackTrace();
}

}

关于android - 仅从 SIM 卡获取本地电话簿联系人 android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10412634/

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