I'm able to retrieve the display name of contact using the id Code as follows:
Uri uri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, selectedid);
Cursor cur = managedQuery(uri, null, null, null, null);
startManagingCursor(cur);
cur.moveToNext();
String mname = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
Where the "selectedid" is my id of the contact to be retrieved.
Now i have a problem in retrieving the phone number from that id.
If possible can u type me the code which i have to add to get the phone number from that id
Have you tried:
String number = cur.getString(cur.getColumnIndex(
ContactsContract.CommonDataKinds.Phone.NUMBER));
i kind of figured it out .... try the link bellow...
Retrieve Contact Phone Number From URI in Android
Related
I've been having difficulty implementing the retrieval of the phone number of a contact whose name matches a given string and then dialing that number. I know this is possible because many existing apps include this feature. How can it be implemented?
this code getting all contacts in phone
Cursor phones = context.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);
if (phones != null) {
while (phones.moveToNext()) {
String name = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
}
phones.close();
}
Compare the variable "name" or "phoneNumber" with your String
and you have add permission "android.permission.READ_CONTACTS"
I am developing an application in which retrieving all contact details(number, name, email, photo) from mobile. its showing correct all details but problem is that email id is showing same as mobile number. so how to solve this problem.code snippet is below. please help me
Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
Cursor cursor = getApplicationContext().getContentResolver().query(uri, new String[] {ContactsContract.CommonDataKinds.Phone.NUMBER,
ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,ContactsContract.CommonDataKinds.Phone._ID,
ContactsContract.CommonDataKinds.Email.ADDRESS}, null, null, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " ASC");
cursor.moveToFirst();
while (cursor.isAfterLast() == false)
{
String contactNumber= cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
String contactName = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
String emailId = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.ADDRESS));
// int phoneContactID = cursor.getInt(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone._ID));
Contact contact = new Contact();
contact.setConatctId(count++);
//contact.setConatctId(phoneContactID);
contact.setEmailId(emailId);
contact.setDisplayName(contactName);
contact.setMobileNo(contactNumber);
It could be better to use CursorLoader for fetching data from database using providers. And user CursorAdapter to show the content in listview.
I have to get distinct raw contact id using content resolver.
ContentResolver cr = getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,
null, null, null, null);
if (cur.getCount() > 0){
while (cur.moveToNext()){
String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
System.out.println("id............"+id+"..."+name);
}
}
Ist i have inserted all contacts into native contacts.Then Using above code i got all native contact id. How to get one id at a time after inserting 1 contact. Please help me.Thanks in advance.
Replace to
cursor.moveToLast();
you can easily get last id of the database
I'm attempting to find someone's email address, given their phone number (assuming they have it stored in Contacts).
At the moment I've been fine if I want the person's name, or something, just doing:
Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI,
Uri.encode(phoneNumber));
Cursor cur = getContentResolver().query(uri, null, null, null, null);
String name = cur.getString(cur.getColumnIndex(ContactsContract.PhoneLookup.DISPLAY_NAME);
However, there is no way to search using a phone number, through the email DB
Uri emailUri = Uri.withAppendedPath(ContactsContract.CommonDataKinds.Email.CONTENT_LOOKUP_URI,
Uri.encode(phoneNumber));
Cursor cur = getContentResolver().query(emailUri, null, null, null, null);
//Returns nothing, unless someone has the email +441234567891#hotmail.com or something
Now I could use CONTENT_FILTER_URI, and search for the name of the person I just found... But then I could get 2 John Smiths... There must be some way to just join across the PhoneLookup and the Email DBs? Maybe using the LOOKUP_KEY, or something?
I want to get all phone contacts from device in android.i have used the following code.but the problem is it takes more time to return the results.is there any solution?
ContentResolver cr = getContentResolver();
int index=0;
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,
null, null, null, null);
if (cur.getCount() > 0)
{
phoneNames=new String[cur.getCount()];
phoneNumbers=new String[cur.getCount()];
while (cur.moveToNext())
{
String id = cur.getString(
cur.getColumnIndex(ContactsContract.Contacts._ID));
name = cur.getString(
cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0)
{
phoneNames[index]=name;
Cursor pCur = cr.query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?",
new String[]{id}, null);
while (pCur.moveToNext())
{
phoneIndex++;
phoneNumbers[index] = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
index++;
}
pCur.close();
}
}
After reading the code i assume that what you want is a list of contacts with DISPLAY NAMES and their respective phone numbers.
If you are specifically looking for data related to phone numbers i suggest you query on
android.provider.ContactsContract.PhoneLookup and fetch the results using a single cursor.
The following are the fields that you would be interested in:
DISPLAY_NAME
HAS_PHONE_NUMBER
NUMBER
TYPE
e.g
Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phoneNumber));
resolver.query(uri, new String[]{PhoneLookup.DISPLAY_NAME,...
Further details please refer this
Please post your requirement if the assumptions are not true.
Some of quick checks:
Select only the required columns and not all in the first query.
Instead of using Integer.parseInt(cur.getString) use
cur.getInt()
Use PhoneLookup whenever dealing with phone numbers ( the number
field gives the raw phone number
instead of the value stored in
the database which can contain
-,),( appended with it)
Avoid using Cursor within a cursor. Use the API's which includes
joins already implemented in it like
RawContactsEntity, PhoneLookup.
Hope that helps.
Don't do complex database queries on the UI thread. What are you trying to do with the results? If you are displaying things in a list, use a CursorAdapter so that you only pull out what you need when you need it.