アプリケーションで電話帳と SIM カードからすべての連絡先を取得しようとしています。これらすべての連絡先をアプリケーションの SQLite DB に保存したいと考えています。私が作業しているコードは、通常の状態で正常に動作しています。以下の条件で問題が発生します。
- 名前のない連絡先で、それは番号だけです。
- SIM カードからの連絡先。
これら 2 種類の連絡先は、私のコードでは提供されません。以下のコードを使用しています。
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();
}
}