Android check contacts if they are registered in an app

前端 未结 2 646
有刺的猬
有刺的猬 2021-01-15 15:43

I need your help. I\'m developing an app which needs user to register account like Viber. What I want to include is to check if one of my contacts has already an account in

2条回答
  •  囚心锁ツ
    2021-01-15 15:59

    You can get all contacts from using code below and then send to your server:

    ArrayList listAllContactName = new ArrayList();
    ArrayList listAllContacts = new ArrayList();
    ContentResolver cr = getContentResolver();
         Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,
                null, null, null, null);
    
         if (cur.getCount() > 0) {
            while (cur.moveToNext()) {
                String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
                String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
                if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
    
        //here you can get all contact names
        listAllContactName.add(name); 
        System.out.println("name : " + name + ", ID : " + id);
    
                    // get the phone number
                    Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,
                                           ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?",
                                           new String[]{id}, null);
                    while (pCur.moveToNext()) {
                          String phone = pCur.getString(
                                 pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                          System.out.println("phone" + phone);
    listAllContacts.add(phone);
                    }
                    pCur.close();
    

提交回复
热议问题