我正在努力从我的应用程序中的电话簿和 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();
}
}