取本地电话簿联系人从SIM卡中只有机器人 [英] Fetch Local phonebook contacts From SIM card only android
本文介绍了取本地电话簿联系人从SIM卡中只有机器人的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想知道,如果它能够读取SIM卡电话簿或只会存在联系。现在我用下面的code去取接触并获取所有的联系人,甚至我的Gmail和Facebook联系人。
光标光标= getContentResolver()查询(
ContactsContract.Contacts.CONTENT_URI,NULL,NULL,NULL,Phone.DISPLAY_NAME +ASC);
如果(cursor.getCount()大于0)
{
而(cursor.moveToNext())
{
PhoneBookUserEntity用户=新PhoneBookUserEntity();
//挑选出的ID和的显示名称
//从光标的当前行接触
user.setId(cursor.getString(cursor.getColumnIndex(BaseColumns._ID)));
user.setPhoneBookName(cursor.getString(cursor.getColumnIndex(
ContactsContract.Contacts.DISPLAY_NAME)));
如果(user.getphonebookname()长度()> 4)
用户名= user.getphonebookname(); //子(0,4);
其他
用户名= user.getphonebookname(); //子(0,1);
串hasPhone = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
//如果(Boolean.parseBoolean(hasPhone)){
。光标手机= getContentResolver()查询(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,空,ContactsContract.CommonDataKinds.Phone.CONTACT_ID +=+ user.getId(),NULL,NULL);
而(phones.moveToNext()){
user.sePhoneNumber(phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)));
}
phones.close();
//}
// user.sePhoneNumber(cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)));
。光标电子邮件= getContentResolver()查询(ContactsContract.CommonDataKinds.Email.CONTENT_URI,空,ContactsContract.CommonDataKinds.Email.CONTACT_ID +=+ user.getId(),NULL,NULL);
而(emails.moveToNext()){
//这将让你获得多个电子邮件地址
user.setEmailAddress(emails.getString(emails.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA)));
}
emails.close();
user.setImageURI(getPhotoUri(user.getId()));
SearchContactsActivity.this.runOnUiThread(新的Runnable(){
@覆盖
公共无效的run(){
// TODO自动生成方法存根
_progressDialog.setMessage(复制在你的本地电话簿检索联系人信息\ N+ username.toUpperCase());
}
});
arraylist.add(用户);
}
}
cursor.close();
解决方案
有关SIM卡联系人不仅可以使用以下code
私人无效allSIMContact()
{
尝试
{
字符串ClsSimPhonename = NULL;
字符串ClsSimphoneNo = NULL;
乌里simUri = Uri.parse(内容:// ICC / ADN);
光标cursorSim = this.getContentResolver()查询(simUri,NULL,NULL,NULL,NULL);
Log.i(PhoneContact,总:+ cursorSim.getCount());
而(cursorSim.moveToNext())
{
ClsSimPhonename = cursorSim.getString(cursorSim.getColumnIndex(名字));
ClsSimphoneNo = cursorSim.getString(cursorSim.getColumnIndex(数字));
ClsSimphoneNo.replaceAll(\\ D,);
ClsSimphoneNo.replaceAll(与&,);
ClsSimPhonename = ClsSimPhonename.replace(|,);
Log.i(PhoneContact,姓名:+ ClsSimPhonename +电话:+ ClsSimphoneNo);
}
}
赶上(例外五)
{
e.printStackTrace();
}
}
I want to know if its possible to fetch contacts which exist in SIM card or phonebook only. Right now I am using the following code to fetch contacts and it fetches all the contacts even my gmail and Facebook Contacts.
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();
解决方案
For Sim contact only you can use below code
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();
}
}
这篇关于取本地电话簿联系人从SIM卡中只有机器人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文