我正在努力从我的应用程序中获取电话簿和SIM卡的所有联系人.我想将所有联系人存储在我的应用程序sqlite DB中.我正在使用的代码在正常条件下正常工作.在以下条件下遇到问题:
>没有姓名的联系人,这只是号码.
> SIM卡的联系方式.
public void getDefaultContactsToDB(){
CallBackDatabase callbackDB = new CallBackDatabase(RequestCallBack.this);
callbackDB.open();
//clean the database before entering new values.
callbackDB.deleteTable(CallBackDatabase.DATABASE_TABLE);
ContentResolver cr = getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,null, null, null, null);
if (cur.getCount() > 0) {
while (cur.movetoNext()) {
String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
Cursor cur1 = cr.query(
ContactsContract.CommonDataKinds.Email.CONTENT_URI, null,
ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?",
new String[]{id}, null);
while (cur1.movetoNext()) {
//to get the contact names
ArrayList<String> numbers= new ArrayList<String>();
String contactNoumber="";
String name=cur1.getString(cur1.getColumnIndex(ContactsContract.CommonDataKinds.Phone.disPLAY_NAME));
if(name!=null){
Log.e("Name :", name);
if(name.equalsIgnoreCase("011999999999999999")){
System.out.println("got it");
}
}
//to get the contact email
String email = cur1.getString(cur1.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
if(email!=null)
Log.e("Email", email);
String hasNoumber = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
if(Integer.parseInt(hasNoumber)>0){
Cursor pCur = getContentResolver().query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "
+cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID)),null, null);
int i = 0;
while (pCur.movetoNext()) {
//to get the contact number
contactNoumber = pCur.getString(pCur.getColumnIndex("DATA1"));
if(contactNoumber.equalsIgnoreCase("011999999999999999")){
System.out.println("got it");
}
contactNoumber = Constant.removeSpecialCharacters(contactNoumber);
Log.e("contactNoumber", contactNoumber);
// Getting Phone numbers
String numType = null;
if(pCur.getString(pCur.getColumnIndex("mimetype")).equals(ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)){
switch(pCur.getInt(pCur.getColumnIndex("data2"))){
case ContactsContract.CommonDataKinds.Phone.TYPE_HOME :
numType = "HOME";
break;
case ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE :
numType = "MOBILE";
break;
case ContactsContract.CommonDataKinds.Phone.TYPE_WORK :
numType = "WORK";
break;
case ContactsContract.CommonDataKinds.Phone.TYPE_OTHER :
numType = "OTHER";
break;
case ContactsContract.CommonDataKinds.Phone.TYPE_ASSISTANT :
numType ="OTHER";
break;
case ContactsContract.CommonDataKinds.Phone.TYPE_CALLBACK :
numType = "CALLBACK";
break;
case ContactsContract.CommonDataKinds.Phone.TYPE_CAR :
numType ="CAR";
break;
case ContactsContract.CommonDataKinds.Phone.TYPE_COMPANY_MAIN :
numType = "COMPANY MAIN";
break;
case ContactsContract.CommonDataKinds.Phone.TYPE_FAX_HOME :
numType = "FAX HOME";
break;
case ContactsContract.CommonDataKinds.Phone.TYPE_FAX_WORK :
numType = "FAX WORK";
break;
case ContactsContract.CommonDataKinds.Phone.TYPE_MAIN :
numType = "MAIN";
break;
case ContactsContract.CommonDataKinds.Phone.TYPE_ISDN :
numType = "ISDN";
break;
case ContactsContract.CommonDataKinds.Phone.TYPE_MMS :
numType = "MMS";
break;
case ContactsContract.CommonDataKinds.Phone.TYPE_OTHER_FAX :
numType = "OTHER FAX";
break;
case ContactsContract.CommonDataKinds.Phone.TYPE_PAGER :
numType = "PAGER";
break;
case ContactsContract.CommonDataKinds.Phone.TYPE_RAdio :
numType = "RAdio";
break;
case ContactsContract.CommonDataKinds.Phone.TYPE_TELEX :
numType ="TELEX";
break;
case ContactsContract.CommonDataKinds.Phone.TYPE_TTY_TDD :
numType = "TTY TDD";
break;
case ContactsContract.CommonDataKinds.Phone.TYPE_WORK_MOBILE :
numType = "WORK MOBILE";
break;
case ContactsContract.CommonDataKinds.Phone.TYPE_WORK_PAGER :
numType = "WORK PAGER";
break;
case ContactsContract.CommonDataKinds.Phone.TYPE_CUSTOM :
numType = "CUSTOM";
break;
default:
break;
}
}
numbers.add(i, contactNoumber+"("+numType+")");
i++;
}
String numInDB = null;
for (int j = 0; j < i; j++) {
if(j==0)
numInDB =numbers.get(j);
else
numInDB =numInDB + "," +numbers.get(j);
}
if(contactNoumber.length()>0){
if(name!=null){
}else{
name = contactNoumber;
}
callbackDB.InsertContacts(null, name+"="+numInDB, contactNoumber, email);
}
}
}
cur1.close();
}
//CLOSE DB
callbackDB.close();
}
}
解决方法:
Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,null,null, null);
while (phones.movetoNext())
{
String name=phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.disPLAY_NAME));
String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
}
phones.close();
另外,对于Sim联系人,您可以使用以下代码:
private void allSIMContact()
{
try
{
String m_simPhonename = null;
String m_simphoneNo = 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())
{
m_simPhonename =cursorSim.getString(cursorSim.getColumnIndex("name"));
m_simphoneNo = cursorSim.getString(cursorSim.getColumnIndex("number"));
m_simphoneNo.replaceAll("\\D","");
m_simphoneNo.replaceAll("&", "");
m_simPhonename=m_simPhonename.replace("|","");
Log.i("PhoneContact", "name: "+m_simPhonename+" phone: "+m_simphoneNo);
}
}
catch(Exception e)
{
e.printstacktrace();
}
}
编辑:
要获取家庭,移动设备,传真等联系人的详细信息,您需要单独检查以下内容:
06002
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。