在这种情况下,我尝试将移动设备的联系人与数据库进行同步,而这似乎并不是最方便的方法。
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(); }
正如我所说,这似乎不是最正确的方法,我希望您能帮助我让它们自动同步,例如照片中的例子...
答案 0 :(得分:0)
查询代码看起来不错,我认为您要问的问题是“如何运行集成到Android生态系统中的联系人同步操作”。
答案将是创建联系人SyncAdapter,请在此处查看我对类似问题的答案:https://stackoverflow.com/a/44566012/819355