I am trying to get details about contact using phone number everything works perfect, but when the contact number is saved with some special characters then i unable to get the contact details below is my code:
//function called
getContactName("+11234567890");
and the same number saved in contact as (+1(123)456-789)
//function
public String getContactName(String number) {
String name;
if(number != null && !number.equals("")){
// define the columns I want the query to return
String[] projection = new String[] {
ContactsContract.PhoneLookup.DISPLAY_NAME,
ContactsContract.PhoneLookup._ID};
// encode the phone number and build the filter URI
Uri contactUri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(number));
// query time
Cursor cursor = _context.getContentResolver().query(contactUri, projection, null, null, null);
if(cursor != null) {
if (cursor.moveToFirst()) {
name = cursor.getString(cursor.getColumnIndex(ContactsContract.PhoneLookup.DISPLAY_NAME));
}
cursor.close();
}
}
return name;
}
Use the bellow function to get the name from phone number. I have tested it recently. It works fine. because phone lookup will replace all special character from phone number.
ex- I saved new number 0+0141(12-23) with name Gaurav. And I call the function getContactName(01411223) then it will returns the name Gauav.
Please use following function and let me know if it does not work.
public static String getContactName(Context context, String phoneNumber) {
ContentResolver cr = context.getContentResolver();
Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phoneNumber));
Cursor cursor = cr.query(uri, new String[]{PhoneLookup.DISPLAY_NAME}, null, null, null);
if (cursor == null) {
return null;
}
String contactName = null;
if(cursor.moveToFirst()) {
contactName = cursor.getString(cursor.getColumnIndex(PhoneLookup.DISPLAY_NAME));
}
if(cursor != null && !cursor.isClosed()) {
cursor.close();
}
return contactName;
}
You can delete all non digit chars from the string with replaceAll("\\D+","");, have a look and let me know if it works!
public String getContactName(String number) {
number = number.replaceAll("\\D+","");
String name;
if(number != null && !number.equals("")){
// define the columns I want the query to return
String[] projection = new String[] {
ContactsContract.PhoneLookup.DISPLAY_NAME,
ContactsContract.PhoneLookup._ID};
// encode the phone number and build the filter URI
Uri contactUri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(number));
// query time
Cursor cursor = _context.getContentResolver().query(contactUri, projection, null, null, null);
if(cursor != null) {
if (cursor.moveToFirst()) {
name = cursor.getString(cursor.getColumnIndex(ContactsContract.PhoneLookup.DISPLAY_NAME));
}
cursor.close();
}
}
return name;
}
may be this code will helps
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));
}
}
Related
We would like to automatically replace phone numbers within text (TextView) with their corresponding contact names.
Is there a library or code snippet that already does this? Else my naive approach would be to:
get phone numbers in the text (via a regex, maybe the one used for autolink phone)
query for contact that has this number (for each number found)
if found, replace with the corresponding name of the contact
(suggestion for query from #Skynet)
This is currently beeing discussed in a feature request for TextSecure but could be useful for other android apps as well.
You don't need to take the list of all contacts. You can get contact name from a phone number. Use this method:
public static String getContactName(Context context, String phoneNumber) {
ContentResolver contentResolver = context.getContentResolver();
Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phoneNumber));
Cursor cursor = contentResolver.query(uri, new String[]{ PhoneLookup.DISPLAY_NAME }, null, null, null);
if (cursor == null) {
return null;
}
String contactName = null;
if(cursor.moveToFirst()) {
contactName = cursor.getString(cursor.getColumnIndex(PhoneLookup.DISPLAY_NAME));
}
if(cursor != null && !cursor.isClosed()) {
cursor.close();
}
return contactName;
}
Good luck.
Try this method to get the contact name using a phone number:
public String getContactName(String number) {
Uri uri = Uri.withAppendedPath(
ContactsContract.PhoneLookup.CONTENT_FILTER_URI,
Uri.encode(number));
Cursor cur = context.getContentResolver().query(
uri,
new String[] { ContactsContract.PhoneLookup.DISPLAY_NAME,
ContactsContract.PhoneLookup.NUMBER,
ContactsContract.PhoneLookup._ID }, null, null, null);
String contactName = "";
if (cur.moveToNext()) {
int name = cur
.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);
contactName = cur.getString(name);
}
cur.close();
return contactName;
}
Use this when replacing numbers to contact names in your TextView. I hope it works. :)
I am making an call reader application in which i am able to find out the number from which call is coming but now i want my application to say the name of the contact who is placing the call.I am unable to findout the name of the contact. Can anyone help.
Thanks
public static String getContactName(Context context, String phoneNumber) {
ContentResolver cr = context.getContentResolver();
Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phoneNumber));
Cursor cursor = cr.query(uri, new String[]{PhoneLookup.DISPLAY_NAME}, null, null, null);
if (cursor == null) {
return null;
}
String contactName = null;
if(cursor.moveToFirst()) {
contactName = cursor.getString(cursor.getColumnIndex(PhoneLookup.DISPLAY_NAME));
}
if(cursor != null && !cursor.isClosed()) {
cursor.close();
}
return contactName;
}
For more Details Visit this
To get the name of the incoming call number,use
name = cursor.getString(cursor.getColumnIndex(CallLog.Calls.NAME));
I'm using the contact picker like below to get the id of a contact.
public void pickContact() {
Intent intent = new Intent(Intent.ACTION_PICK, Contacts.CONTENT_URI);
intent.setType(Phone.CONTENT_TYPE); // Show user only contacts w/ phone numbers
startActivityForResult(intent, PICK_CONTACT_REQUEST);
}
Then I retrieve the contact id from the uri returned by the above using this. And store that as a reference.
public static long getContactIdByUri(Context context, Uri uri)
{
Log.d(TAG, uri.toString());
String[] projection = { Contacts._ID };
Cursor cursor = context.getContentResolver().query(uri, projection, null, null, null);
try
{
cursor.moveToFirst();
int idx = cursor.getColumnIndex(Contacts._ID);
long id = -1;
if(idx != -1)
{
id = cursor.getLong(idx);
}
return id;
}
finally
{
cursor.close();
}
}
Later on when a text message arrives I fetch the phone number and based on that I try to look up the contact id using the following.
public static long getContactIdByPhoneNumber(Context context, String phoneNumber) {
ContentResolver contentResolver = context.getContentResolver();
Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phoneNumber));
String[] projection = new String[] { PhoneLookup._ID };
Cursor cursor = contentResolver.query(uri, projection, null, null, null);
if (cursor == null) {
return -1;
}
int idx = cursor.getColumnIndex(PhoneLookup._ID);
long id = -1;
if(cursor.moveToFirst()) {
id = cursor.getLong(idx);
}
if(cursor != null && !cursor.isClosed()) {
cursor.close();
}
return id;
}
The problem is that those two id's doesn't match!
So basically the question goes how can I get an id from the contact picker which I can match when looking up a phone number with PhoneLookup.CONTENT_FILTER_URI. Which I can also use to get additional information about the contact?
The url returned from the contact picker refers to the ContactsContract.Data provider which is joined with ContactsContract.RawContacts which again contains CONTACT_ID.
So extracting the actual contact id is trivial using the method below.
public static long getContactIdByDataUri(Context context, Uri uri)
{
String[] projection = new String[] { Data.CONTACT_ID };
Cursor cursor = context.getContentResolver().query(uri, projection, null, null, null);
long id = -1;
if(cursor.moveToFirst()) {
id = cursor.getLong(0);
}
return id;
}
I am trying to create a function that will take a String as an argument and it will return the Contact_ID for the contact that has a phone number like the String provided.
I have already found the following code
private String getContactName (String number)
{
String contactName = "";
ContentResolver context = getContentResolver();
/// number is the phone number
Uri lookupUri = Uri.withAppendedPath(
PhoneLookup.CONTENT_FILTER_URI,
Uri.encode(number));
String[] mPhoneNumberProjection = { PhoneLookup._ID, PhoneLookup.NUMBER, PhoneLookup.DISPLAY_NAME };
Cursor cur = context.query(lookupUri,mPhoneNumberProjection, null, null, null);
try
{
if (cur.moveToFirst())
{
contactName = cur.getString(2);
return contactName;
}
}
finally
{
if (cur != null)
cur.close();
}
return contactName;
}
It returns all contactsName for a given number.
How i can get the contact id from here?
Thanks!
You simply have to replace cur.getString(2) with cur.getString(0). You already have the _ID as part of your projection (which BTW could be reduced to just the _ID).
I have the following code:
public String getContactDisplayNameByNumber(String number) {
Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(number));
String name = "?";
ContentResolver contentResolver = getContentResolver();
Cursor contactLookup = contentResolver.query(uri, new String[] {BaseColumns._ID,
ContactsContract.PhoneLookup.DISPLAY_NAME }, null, null, null);
try {
if (contactLookup != null && contactLookup.getCount() > 0) {
contactLookup.moveToNext();
name = contactLookup.getString(contactLookup.getColumnIndex(ContactsContract.Data.DISPLAY_NAME));
//String contactId = contactLookup.getString(contactLookup.getColumnIndex(BaseColumns._ID));
}
} finally {
if (contactLookup != null) {
contactLookup.close();
}
}
Toast.makeText(this, name, Toast.LENGTH_LONG).show();
return name;
}
Here I am searching a contact name based on the full contact phone nummber. Is it possible to search contacs based just on 4 numbers from the phone number?
Get the entire contacts numbers and names, use regex to get entries that match?