Reputation: 163
Here is the Code to access contacts in android:
private void getContact() {
arrayList=new ArrayList<>();
arrayListname=new ArrayList<>();
arrayListmobno=new ArrayList<>();
System.out.println("hello");
// Cursor cursor=getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,null,null,null);
String selection = ContactsContract.Contacts.IN_VISIBLE_GROUP + " = '"+ ("1") + "'";
String sortOrder = ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC";
Cursor cursor=getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null, selection
+ " AND " + ContactsContract.Contacts.HAS_PHONE_NUMBER
+ "=1", null, sortOrder);
while (cursor.moveToNext())
{
final String name=cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
final String mob=cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
final String regexStr = "^(?:(?:\\+|0{0,2})91(\\s*[\\-]\\s*)?|[0]?)?[789]\\d{9}$";
System.out.println("omgname "+name +" mob "+mob);
}
}
Here All the contacts in the contact list are accessed
Output:
Url:
Upvotes: 1
Views: 126
Reputation: 393
You may append the contacts to string and send 50 contacts at once to php page.
java code:
while (cursor.moveToNext()) {
final String name = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
final String mob = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
final String regexStr = "^(?:(?:\\+|0{0,2})91(\\s*[\\-]\\s*)?|[0]?)?[789]\\d{9}$";
System.out.println("omgname " + name + " mob " + mob);
arrayListname.add(name.trim());
arrayListmobno.add(mob.trim());
if (mob.length() > 9 && !mob.contains("-") && !name.equals("") && !name.equals(".")) {
final String myname = name.replaceAll(" ", "_").replaceAll("[^a-zA-Z0-9_]+", "");
final String mymobno = mob.replaceAll(" ", "");
arrayListmobno.add(mymobno);
count = count + 1;
if (count == 50) {
System.out.println(namearray + "namearrayomg");
System.out.println(mobnoarray + "mobnoarrayomg");
String url = getResources().getString(R.string.url);
String contacts_url = url + "request=contactsarrayList&arrayListname=" + namearray + "&arrayListmobno=" + mobnoarray + "&userid=" + id;
System.out.println(contacts_url);
AndroidNetworking.get(contacts_url)
.setPriority(Priority.LOW)
.build()
.getAsJSONArray(new JSONArrayRequestListener() {
@Override
public void onResponse(JSONArray response) {
try {
} catch (Exception e) {
Toast.makeText(getApplicationContext(), "JSON Error" + e.getMessage(), Toast.LENGTH_LONG).show();
}
}
@Override
public void onError(ANError anError) {
anError.printStackTrace();
}
});
namearray = "";
mobnoarray = "";
count = 0;
} else {
namearray = namearray + myname + ",";
mobnoarray = mobnoarray + mymobno.trim() + ",";
}
}
php code:
public function contactsarrayList($arrayListname=0,$arrayListmobno=0,$userid=0)
{
global $con;
$arrayListname = substr($arrayListname, 0, -1);
$arrayListmobno = substr($arrayListmobno, 0, -1);
$narray = explode(',', $arrayListname);
$marray = explode(',', $arrayListmobno);
$len=count($narray);
for ($i=0; $i < $len ; $i++) {
$retrived1 = $con->query("insert into contacts(userid,name,mobno) values({$userid},'{$narray[$i]}','{$marray[$i]}')");
}
}
I know this looks bit complicated but will work
also add composite unique key to the contacts table in database so that no contact will be repeated
Upvotes: 1
Reputation: 4035
I am assuming you want to add the details (phone number) and (name) in a list and prevent duplicates.
If that is the case:
Then add the details to the lists if they already don't exist:
private void getContact() {
arrayList=new ArrayList<>();
arrayListname=new ArrayList<>();
arrayListmobno=new ArrayList<>();
String selection = ContactsContract.Contacts.IN_VISIBLE_GROUP + " = '"+ ("1") + "'";
String sortOrder = ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC";
Cursor cursor=getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null, selection
+ " AND " + ContactsContract.Contacts.HAS_PHONE_NUMBER
+ "=1", null, sortOrder);
while (cursor.moveToNext()){
final String name=cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
final String mob=cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
final String regexStr = "^(?:(?:\\+|0{0,2})91(\\s*[\\-]\\s*)?|[0]?)?[789]\\d{9}$";
//add details to the list if they don't exist already in the list:
if(!arrayListname.contains(name)){
arrayListname.add(name);
}
if(!arrayListmobno.contains(mob)){
arrayListmobno.add(mob);
}
}
}
Upvotes: 1