So, I have my DB Table (sql or mysql) :
Users table fields: - idPhoneNumber - name
My application need to load all the phone numbers from contact list, check if that number exists in db then save in list or insert into sqlite.
Actually my code is this:
private void getContact() {
List<String> contacts = new ArrayList<>();
ContentResolver cr = getContentResolver();
Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
assert cursor != null;
if (cursor.moveToFirst()) {
do {
if(cursor.getInt(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER)) > 0) {
String id = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ id,null, null);
assert phones != null;
phones.moveToNext();
// int type = phones.getInt(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));
if(phones.getInt(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE) == ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE) {
String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
phoneNumber = phoneNumber.replace("-", "").trim().replaceAll(" ", "");
String name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
/* HERE */
checkContact() ????
contacts.add(name);
}
phones.close();
}
} while (cursor.moveToNext());
}
cursor.close();
// ***so here i should update my SQLITE with all contacts in my list 'contacts'.***
retu contacts;
}
What is the best practice to do this ? I used volley to do the request to my http server. If I want to use that I was thinking in something like this in "getContact"
private checkContact(String phoneNumberFromGetContact )
{
- volley response
- post phoneNumberFromGetContact
- db exec query to check if exists number
- retu true if exists
- if response == true then add number to list
// after
}
Remember that I have to check number per number if exists in DB.
