I try to synchronize the contacts of the mobile device with a database in this case Postgresql, the way I do it does not seem to be the most convenient.
public void sincronizarcontactos(){
// Se tiene permiso
final String[] projeccion = new String[]{ContactsContract.CommonDataKinds.Phone._ID,ContactsContract.Data.DISPLAY_NAME, ContactsContract.CommonDataKinds.Phone.NUMBER, ContactsContract.CommonDataKinds.Phone.NORMALIZED_NUMBER};
String selectionClause = ContactsContract.Data.MIMETYPE + "='" +
ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE + "' AND "
+ ContactsContract.CommonDataKinds.Phone.NUMBER + " IS NOT NULL";
String sortOrder = ContactsContract.Data.DISPLAY_NAME + " ASC";
final Cursor c = getContentResolver().query(
ContactsContract.Data.CONTENT_URI,
projeccion,
selectionClause,
null,
sortOrder);
HashSet<String> normalizedNumbersAlreadyFound = new HashSet<>();
int indexOfNormalizedNumber = c.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NORMALIZED_NUMBER);
progreso= ProgressDialog.show(SincronizarActivity.this, "", "Sincronizando...", true);
int o=0;
while(c.moveToNext()){
o++;
progreso.show();
String email=c.getString(0).replace(" ","_");
String normalizedNumber = c.getString(indexOfNormalizedNumber);
if(normalizedNumbersAlreadyFound.add(normalizedNumber)){
String urlinsert="http://192.168.1.33/Agenda/insert_persona.php?name="+c.getString(0)+
"&city=Paute&email="+email+"@gmail.com&phone="+c.getString(1)+"&mobile="+c.getString(2);
JsonObjectRequest solicitud = new JsonObjectRequest(urlinsert,null, new Response.Listener<JSONObject>(){
@Override
public void onResponse(JSONObject datos) {
Toast.makeText(SincronizarActivity.this, "Guardado", Toast.LENGTH_SHORT).show();
}
},new Response.ErrorListener(){
@Override
public void onErrorResponse(VolleyError error) {
progreso.dismiss();
Toast.makeText(SincronizarActivity.this, "Tuvimos un problema de conexión...", Toast.LENGTH_SHORT).show();
}
});
VolleyRP.addToQueue(solicitud,request,this,volley);
}
if (o==projeccion.length){
progreso.dismiss();
}
}
progreso.dismiss();
Toast.makeText(SincronizarActivity.this, "Se han sicronizado sus contactos...", Toast.LENGTH_SHORT).show();
c.close();
}
As I say, it does not seem to me to be the most correct way, I would like you to help me to get them to synchronize automatically, as in the photo ...
I hope you can help me with this ...