这里是访问安卓应用中的联系人的代码。
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);
}
}
在这里,所有的联系人在联系人列表中访问。
输出。
Url:
你可以将联系人添加到字符串中,一次发送50个联系人到php页面。
java代码。
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代码: php代码:
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]}')");
}
}
我知道这看起来有点复杂,但是可以用
同时在数据库中的联系人表中添加复合唯一键,这样就不会有重复的联系人。
我假设您想在一个列表中添加详细信息(电话号码)和(姓名),并防止重复。
如果是这样的话。
那就把细节添加到名单中,如果它们还不存在的话。
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);
}
}
}