Getting contact number using content provider in android

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

But I would like to know how I can get the contact number, which is stored from the display name. tried with "ContactsContract.Contacts.CONTENT_VCARD_TYPE". But got an error.

Please let me know if there is any solution.

thanks
Sneha

+2
source share
2 answers

This is a good tutorial on how to get a contact number using android content provider

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 choose a contact number like this

add button click event e.g.

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(); } } 
+4
source

This code will work for contacts with a mobile number, and not for email or anything else. I found this code the simplest. let me know if there is any problem.

Get started with the goal of choosing the type of phone data

 findViewById(R.id.choose_contact_button).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent pickContact = new Intent(Intent.ACTION_PICK, ContactsContract.CommonDataKinds.Phone.CONTENT_URI); startActivityForResult(pickContact, PICK_CONTACT_REQUEST); } }); 

Now set onAcitivityResult ();

 @Override protected void onActivityResult(int requestCode, int resultCode, Intent intent){ switch (requestCode){ case PICK_CONTACT_REQUEST: if (resultCode == RESULT_OK){ Uri contactUri = intent.getData(); Cursor nameCursor = getContentResolver().query(contactUri, null, null, null, null); if (nameCursor.moveToFirst()){ String name = nameCursor.getString(nameCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME)); String number = nameCursor.getString(nameCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); ((EditText)findViewById(R.id.person_name)).setText(name); ((EditText)findViewById(R.id.enter_mobile)).setText(number); nameCursor.close(); } } break; } } 
+1
source

Source: https://habr.com/ru/post/912128/


All Articles