How to get all contacts from phonebook & SIM card in Android?

前端 未结 3 1866
太阳男子
太阳男子 2021-02-03 16:15

I am working to get all the contacts from phone book and SIM card in my application. I want to store those all contacts in my application SQLite DB. The code with which I am wor

3条回答
  •  名媛妹妹
    2021-02-03 16:41

    The following code will return all the names of the contact and you can set in text view-

            Cursor cursor = null;
            String name, phoneNumber,image,email;
            try {
                cursor = getApplicationContext().getContentResolver()
                        .query(Phone.CONTENT_URI, null, null, null, null);
                int nameIdx = cursor.getColumnIndex(Phone.DISPLAY_NAME);
                int phoneNumberIdx = cursor.getColumnIndex(Phone.NUMBER);
                int photoIdIdx = cursor.getColumnIndex(Phone.PHOTO_URI);
                cursor.moveToFirst();
                do {
                    HashMap hashMap = new HashMap();
                    name = cursor.getString(nameIdx);
                    phoneNumber = cursor.getString(phoneNumberIdx);
                    image = cursor.getString(photoIdIdx);
                    //email=cursor.getString(emailIdx);
                    if(!phoneNumber.contains("*"))
                    {
                        hashMap.put("name", "" + name);
                        hashMap.put("phoneNumber", "" + phoneNumber);
                        hashMap.put("image", "" + image);
                        //hashMap.put(email, ""+email);
                        hashMapsArrayList.add(hashMap);
                    }
                } while (cursor.moveToNext());
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                if (cursor != null) {
                    cursor.close();
                }
            }
            myAdapter=new MyAdapter();
            listView.setAdapter(myAdapter);
            myAdapter.notifyDataSetChanged();
    
        }
    

提交回复
热议问题