How to import contacts from phonebook to our application

前端 未结 4 1507
长发绾君心
长发绾君心 2020-11-29 13:37

I am developing the android application ,when ever user clicks on the button it should show all contacts from the phone book with in a table.How can i achieve it,any one can

相关标签:
4条回答
  • 2020-11-29 14:01

    If you query the ContactsContract.Contacts content provider you will get cursor with the list of contacts.

    0 讨论(0)
  • 2020-11-29 14:03
                Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
                intent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE);
                startActivityForResult(intent, 1);
    

    Use this piece of code under the button.setOnClick function you'll get the display of all the contacts in the phone book

    0 讨论(0)
  • 2020-11-29 14:13

    You can use this code inside the button.setonclicklistener.

    Intent intent = new Intent(Intent.ACTION_PICK,ContactsContract.Contacts.CONTENT_URI); startActivityForResult(intent, PICK_CONTACT);

    0 讨论(0)
  • 2020-11-29 14:17

    give you some codes:

         ContentResolver cr = getContentResolver();
        Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
        while(cursor.moveToNext()){
        //get name
        int nameFiledColumnIndex = cursor.getColumnIndex(PhoneLookup.DISPLAY_NAME);
        String contact = cursor.getString(nameFiledColumnIndex);
    
        String[] PHONES_PROJECTION = new String[] { "_id","display_name","data1","data3"};//
        String contactId = cursor.getString(cursor.getColumnIndex(PhoneLookup._ID));
        Cursor phone = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, PHONES_PROJECTION,
        ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "=" + contactId, null, null);
        //name type ..
        while(phone.moveToNext()) {
            int i = phone.getInt(0);
            String str = phone.getString(1);
            str = phone.getString(2);
            str = phone.getString(3);
            }
            phone.close();
            //addr
            Cursor addrCur = cr.query(ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_URI ,
         new String[]{"_id","data1","data2","data3"}, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "=" + contactId , null, null);
            while(addrCur.moveToNext()) {
            int i = addrCur.getInt(0);
            String str = addrCur.getString(1);
            str = addrCur.getString(2);
            str = addrCur.getString(3);
            }
            addrCur.close();
    
            //email
            Cursor emailCur = cr.query(ContactsContract.CommonDataKinds.Email.CONTENT_URI ,
         new String[]{"_id","data1","data2","data3"}, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "=" + contactId , null, null);
            while(emailCur.moveToNext()) {
            int i = emailCur.getInt(0);
            String str = emailCur.getString(1);
            str = emailCur.getString(2);
            str = emailCur.getString(3);
            }
            emailCur.close();
    
        }
        cursor.close();
    
    0 讨论(0)
提交回复
热议问题