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);
}
}
ここで連絡先リストのすべての連絡先にアクセスします
出力:
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コード:
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]}')");
}
}
私はこれが少し複雑に見えることを知っていますが、うまくいくでしょう
また、データベースの連絡先テーブルに複合一意キーを追加して、連絡先が繰り返されないようにします
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加