How to get contactname from phonenumber please help me - android

How to get contactname from phonenumber please help me
Code i used:
public String contactname(String phonenumber)
{
ContentValues contentValues = new ContentValues();
Uri contactUri = Uri.withAppendedPath(Contacts.Phones.CONTENT_FILTER_URL,
phonenumber);
Cursor cur = managedQuery(contactUri, null, null, null, null);
int nameColumn = cur.getColumnIndex(People.NAME);
String name = cur.getString(nameColumn);
return name;
}
It shows error

the type Contacts.People.Phones is deprecated, i think you should directly use Contacts, Beside, the string you set phonenumber is only used to match various parts of contact name.
maybe you can query all of the contacts first, and then match the one by giving the phonenumber.
Uri uri = Contacts.CONTENT_URI;
Cursor cursor = managedQuery(uri, null, null, null, null);
cursor.moveToFirst();
String name = null;
while (curcor.getPosition != cursor.getCount) {
if (cursor.getString(cursor.getColumnIndex("default_tel")).equals(phonenumber)) {
name = cursor.getString(cursor.getColumnIndex(Contacts.DISPLAY_NAME));
break;
}
}
return name;

Related

Search a contact with partial search string

I want to search the contact list of the android user, by typing just 3-4 digits of a particular number. I have written the following method to do the same, but this is only working when I write complete 10 digit of the number and not less than that. It means it is only working with the exact match.
e.g I want to the number starting 98965... and so on.. but i only gives me a match when I type complete 9896511112
public static void getContactDetails(Context context, String number, int type) {
String[] projection = new String[]{
ContactsContract.PhoneLookup.DISPLAY_NAME,
ContactsContract.PhoneLookup._ID,
ContactsContract.PhoneLookup.LOOKUP_KEY,
ContactsContract.PhoneLookup.PHOTO_URI,
ContactsContract.PhoneLookup.NUMBER};
Uri contactUri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(number.trim() + "%"));
Cursor cursor = context.getContentResolver().query(contactUri, null, null, null, null);
while (Common.nonNull(cursor) && cursor.moveToNext()) {
String lookUpKey = cursor.getString(cursor.getColumnIndex(ContactsContract.PhoneLookup.LOOKUP_KEY));
String mobileNumber = cursor.getString(cursor.getColumnIndex(ContactsContract.PhoneLookup.NUMBER));
Log.e("I am found", "Found " + number);
}
cursor.close();
}
Please help me to find my mistake.
Ahh finally. One thing that saved my day:
Uri uri = Uri.withAppendedPath(ContactsContract.CommonDataKinds.Phone.CONTENT_FILTER_URI, Uri.encode(partial));
Use "ContactsContract.CommonDataKinds.Phone.CONTENT_FILTER_URI" instead of ContactsContract.PhoneLookup.CONTENT_FILTER_URI, and it will work for you.
Here is my complete example below:
public static void getContactDetails(Context context, String number, int type) {
String[] projection = new String[]{
ContactsContract.PhoneLookup.DISPLAY_NAME,
ContactsContract.PhoneLookup._ID,
ContactsContract.PhoneLookup.LOOKUP_KEY,
ContactsContract.PhoneLookup.PHOTO_URI,
ContactsContract.CommonDataKinds.Phone.NUMBER};
Uri contactUri = Uri.withAppendedPath(ContactsContract.CommonDataKinds.Phone.CONTENT_FILTER_URI, Uri.encode(number.trim()));
Cursor cursor = context.getContentResolver().query(contactUri, projection, null, null, null);
while (Common.nonNull(cursor) && cursor.moveToNext()) {
String lookUpKey = cursor.getString(cursor.getColumnIndex(ContactsContract.PhoneLookup.LOOKUP_KEY));
String mobileNumber = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
String displayName = cursor.getString(cursor.getColumnIndex(ContactsContract.PhoneLookup.DISPLAY_NAME));
if (mobileNumber.length() > 10) {
mobileNumber = mobileNumber.substring(mobileNumber.length() - 10);
if (mobileNumber.startsWith(number)) {
Log.e("I am found", "I am found " + mobileNumber + displayName);
}
}
}
cursor.close();
}

Android Get contact name from phone number

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));
}
}

How to convert a phone number to that contacts name in TextView on Android?

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. :)

Get CONTACT_ID for a given number

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).

Delete all contacts in a group

I'm trying to delete all contacts from a defined group but I don't know how to do a join from the contact table and group table (if it's possible).
ContentResolver cr = getContentResolver();
String where = ContactsContract.Groups.TITLE + " =='LolGroup'";
Cursor cursor = cr.query(
ContactsContract.Contacts.CONTENT_URI, null, where, null, null);
while (cursor.moveToNext()) {
String lookupKey = cursor.getString(
cursor.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY));
Uri uri = Uri.withAppendedPath(
ContactsContract.Contacts.CONTENT_LOOKUP_URI, lookupKey);
cr.delete(uri, null, null);
}
Of course it gives me an error because there is no "title" in the contacts group, but if I do a join with the ID I should get what i want.
Any idea how to do that join?
It looks strange because ContactsContract.Contacts does not have ContactsContract.Groups.TITLE column. So I think that you can get group id with the group title you want and then search contacts with the group id. The idea might go like following:
public String getGroupIdByTitle(String groupTitle){
try {
cursor = mContentResolver.query(
ContactsContract.Groups.CONTENT_URI,
new String[] {Groups._ID},
Groups.TITLE + "=?",
new String[]{groupTitle},
null);
while (cursor.moveToNext()){
return cursor.getString(cursor.getColumnIndex(0);
}
} finally {
if (cursor!=null) cursor.close();
}
return "";
}
public String getGroupIdOfContact(String lookupKey) {
String where = String.format("%s=? AND %s=?", Data.LOOKUP_KEY, Data.MIMETYPE);
String[] whereArgs = {lookupKey, GroupMembership.CONTENT_ITEM_TYPE};
String groupRowId = "";
Cursor cursor = mContentResolver.query(
Data.CONTENT_URI,
new String[]{GroupMembership.GROUP_ROW_ID},
where, whereArgs, null);
try {
if (cursor.moveToNext()) {
return cursor.getString(cursor.getColumnIndex(GroupMembership.GROUP_ROW_ID));
}
} finally {
if (cursor!=null) cursor.close();
}
return "";
}
public void deleteContactByGroupTitle(String groupTitle) {
String targetGroupId = getGroupIdByTitle(groupTitle);
Cursor cursor = null;
try {
cursor = mContentResolver.query(Contacts.CONTENT_URI, null, null, null, null);
while (cursor.moveToNext()){
String lookupKey = cursor.getString(cursor.getColumnIndex(Contacts.LOOKUP_KEY));
String groupId = getGroupIdOfContact(lookupKey);
if (targetGroupId.equals(groupId)){
//TODO. delete this contact
}
}
} finally {
if (cursor!=null) cursor.close();
}
}
The above code has not tested but I think that basic idea would be same.

Categories

Resources