Search code examples
androidsqlitecontactsandroid-contentprovider

Getting contact number using content provider in android


I followed this tutorial and got the basics of Content Providers : http://www.vogella.de/articles/AndroidSQLite/article.html

But wanted to know how can i get the contact number that is stored against display name. tried with "ContactsContract.Contacts.CONTENT_VCARD_TYPE". But got an error.

Please let me know if there is any solution.

Thanks
Sneha


Solution

  • This is a good tutorial about Getting contact number using content provider in android

    http://www.higherpass.com/Android/Tutorials/Working-With-Android-Contacts/

    and

    http://www.app-solut.com/blog/2012/06/retrieval-of-contacts-with-contact-contract/

    and can pick contact number like this

    add button click event like

    button1.setOnClickListener(new View.OnClickListener() {
    
                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
    
                    i = new Intent(Intent.ACTION_PICK,
                            ContactsContract.Contacts.CONTENT_URI);
                    startActivityForResult(i, PICK_CONTACT);
                }
            });
    
    
    //outside button click
    
    public void onActivityResult(int reqCode, int resultCode, Intent data) {
            super.onActivityResult(reqCode, resultCode, data);
    
            switch (reqCode) {
            case (PICK_CONTACT):
    
    
                if (resultCode == Activity.RESULT_OK) {
                    getContactInfo(data);
                }
            }
        }
    
        // onActivityResult
    
        private void getContactInfo(Intent data) {
            // TODO Auto-generated method stub
    
             ContentResolver cr = getContentResolver();
    
    
    
    
            Cursor cursor = managedQuery(data.getData(), null, null, null, null);
            while (cursor.moveToNext()) {
                String contactId = cursor.getString(cursor
                        .getColumnIndex(ContactsContract.Contacts._ID));
                Name = cursor
                        .getString(cursor
                                .getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME));
    
                String hasPhone = cursor
                        .getString(cursor
                                .getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
    
                Cursor emailCur = cr.query( 
                        ContactsContract.CommonDataKinds.Email.CONTENT_URI, 
                        null,
                        ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?", 
                        new String[]{contactId}, null); 
    
    
                emailCur.close();
    
    
    
    
    
    
         if (hasPhone.equalsIgnoreCase("1"))
                    hasPhone = "true";
                else
                    hasPhone = "false";
    
                if (Boolean.parseBoolean(hasPhone)) {
                    Cursor phones = getContentResolver().query(
                            ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                            null,
                            ContactsContract.CommonDataKinds.Phone.CONTACT_ID
                                    + " = " + contactId, null, null);
                    while (phones.moveToNext()) {
                        phoneNo = phones
                                .getString(phones
                                        .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                    }
                    phones.close();
                }
    
                pname.setText(Name);
                //
                phno.setText(phoneNo);
    
            Toast.makeText(this, Name + "   " + phoneNo, Toast.LENGTH_SHORT).show();
    
    
            }
    
        }