如何在Android应用中访问唯一联系人?

以下是访问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);
        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,selection
                + " AND " + ContactsContract.Contacts.HAS_PHONE_NUMber
                + "=1",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);
            }
    }

在此访问联系人列表中的所有联系人

输出:

如何在Android应用中访问唯一联系人?

网址:

如何在Android应用中访问唯一联系人?

song0007 回答:如何在Android应用中访问唯一联系人?

我假设您想在列表中添加详细信息(电话号码)和(姓名),并防止重复。

如果是这样的话:

然后将详细信息添加到列表(如果还不存在):

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",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);
}


}

}
,

您可以将联系人添加到字符串,然后一次将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,-1);
        $arrayListmobno = substr($arrayListmobno,-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]}')");
        }
    }

我知道这看起来有些复杂,但可以使用

还将复合唯一键添加到数据库的联系人表中,这样就不会重复任何联系人

本文链接:https://www.f2er.com/2333725.html

大家都在问