How to Get Contact name, number and emai ID from a list of contacts?

前端 未结 2 1920
后悔当初
后悔当初 2021-01-15 07:54

I am new to android ,I need to get the details of my contacts, but the details include only 3

  1. Contact name

  2. contact number and

2条回答
  •  悲&欢浪女
    2021-01-15 08:10

    You can access addressbook like this;

    ContentResolver cr = getContentResolver();
    Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null,null, null,ContactsContract.Contacts.DISPLAY_NAME);
    int kisiSayisi =  cur.getCount();
    
    if(kisiSayisi > 0)
    {
        int KisiIndex = 0;
        while(cur.moveToNext())
        {
            String id = cur.getString(cur.getColumnIndex(BaseColumns._ID));
            String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
            if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0)
            {
                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.DATA));
                     //String phoneType = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));
                     String dogruGSM = gsmNoKontrol(phone);
                     if(dogruGSM.compareTo("0") != 0){
                            Kisi kisi = new Kisi(KisiIndex, name, dogruGSM, false);
                            MyList.add(kisi);
                            KisiIndex ++;
                     }
                 }
                 pCur.close();
             }
         }
     }
    

提交回复
热议问题