How to display numbers from contact list? - android

I use the code below to display contact numbers, but it is displaying names and numbers from the contact list.
public void getContacts(Context context){
context.startActivity(new Intent(Intent.ACTION_PICK, People.CONTENT_URI));
}
Like the above code how can I use the below code for getting only contact numbers from the contact list? I need to display only contact numbers without showing names. Please help with the right solution to solve this problem.
Uri personUri = ContentUris.withAppendedId(People.CONTENT_URI, personId);
Uri phonesUri = Uri.withAppendedPath(personUri, People.Phones.CONTENT_DIRECTORY);
String[] proj = new String[] {Phones._ID, Phones.TYPE, Phones.NUMBER, Phones.LABEL}
Cursor cursor = contentResolver.query(phonesUri, proj, null, null, null);

Try and look at this site:
Query contacts.
There are multiple methods for retrieving different content from contacts.
Hopefully that could help.

Related

How to search contacts by name and phone number?

I am using Contacts.CONTENT_FILTER_URI to search for contacts.
Uri contentUri = Uri.withAppendedPath(
ContactsContract.Contacts.CONTENT_FILTER_URI,
Uri.encode(searchString));
The searchstring can be either a number or a name. That works great.
My only problem is that the result does not contain a contact phone number.
I know that I can get it by querying ContactsContract.Data.CONTENT_URI. However, I would like to find a solution that will give me a contact name and phone number with a single query.
You should use Phone.CONTENT_FILTER_URI instead of Contacts.CONTENT_FILTER_URI
Docs say:
The filter is applied to display names as well as phone numbers.
Try this:
Uri filterUri = Uri.withAppendedPath(Phone.CONTENT_FILTER_URI, Uri.encode(searchString));
String[] projection = new String[]{ Phone.CONTACT_ID, Phone.DISPLAY_NAME, Phone.NUMBER };
Cursor cur = getContentResolver().query(filterUri, projection, null, null, null);

google libphonenumber get only mobile numbers from contacts

I'm using libphonenumber to format phonenumbers in contacts. Is there any way to display only mobile numbers like WhatsApp? no local phone numbers.
The only idea I have is to have a list with all mobile area codes like https://en.wikipedia.org/wiki/List_of_mobile_phone_number_series_by_country and check every number but this is a bit complicated I think.
found the answer...
PhoneNumberUtil.PhoneNumberType nrtype = phoneUtil.getNumberType(NumberProto);
if (nrtype.name() == PhoneNumberUtil.PhoneNumberType.MOBILE) {
}
Uri personUri = ContentUris.withAppendedId(People.CONTENT_URI, personId);
Uri phonesUri = Uri.withAppendedPath(personUri, People.Phones.CONTENT_DIRECTORY);
String[] proj = new String[] {Phones._ID, Phones.TYPE, Phones.NUMBER, Phones.LABEL}
Cursor cursor = contentResolver.query(phonesUri, proj, null, null, null);

Android get custom ringtone for a contact group

I need to determine the custom ringtone for a given contact group.
I can do this successfully to get the custom ringtone of a contact by using the following query:
Uri queryUri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(callerNumber));
String[] columns = new String[]{ContactsContract.Contacts.DISPLAY_NAME,
ContactsContract.Contacts.CUSTOM_RINGTONE, ContactsContract.CommonDataKinds.GroupMembership.CUSTOM_RINGTONE };
Cursor contactsCursor = getContentResolver().query(queryUri, columns, null, null, null);
But I can't find out how to determine the custom ringtone for a contact group.
Do you know how this can be done?
Thanks in advance!

Joining contacts and photo tables in Android

I am developing a phonebook app. I am trying to retrieve contact picture. At first I retrieved all the contacts and then took each contact_id and did a query on the photo table to get the picture. However, it is taking forever to query all the contacts for pics. As in my emulator there are more than 1000 contacts, so, more than 1000 hits on the photo table is being fired. So, is there a way to join the two tables and get the data in a single query?
Below is my code to do it. But I know its wrong. Just gave it a shot. Please someone correct it.
String[] projection = new String[]{Contacts._ID, Contacts.DISPLAY_NAME};
String joinCondition = "ContactsContract.Contacts._ID=ContactsContract.CommonDataKinds.Photo.CONTACT_ID";
ContentResolver cr = context.getContentResolver();
Uri contactUri = ContactsContract.Contacts.CONTENT_URI;
Uri fillUri = Uri.withAppendedPath(contactUri, Contacts.Photo.CONTENT_DIRECTORY);
Cursor cur = cr.query(contactUri, projection,
joinCondition, null, Contacts.DISPLAY_NAME);
Thx!
Rahul.
You should be retrieving the photo thumbnail in the list adapter's getView method. This is already optimized so that you only have to process the records which need to be shown on the screen. So at most you'll have to query a handful at a time, even if you have thousands of contacts.
you can try the following:
public final String[] columns = {ContactsContract.Contacts._ID,
ContactsContract.Contacts.DISPLAY_NAME,
ContactsContract.Contacts.PHOTO_THUMBNAIL_URI};
Cursor c = contentResolver.query(Contacts.CONTENT_URI, columns, where, args, ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC");
this should return you a cursor with all contacts and their pictures

Contact picker filtering

I use the contact picker in this way:
Intent intent = new Intent(Intent.ACTION_PICK, Contacts.CONTENT_URI);
this.startActivityForResult(intent, PICK_CONTACT_REQUEST);
My question is if somehow the contact list can be filtered? For example I want to see only those contacts in the contact list which have at least a phone number or an email address.
I would suggest to use your custom view for the contacts- it is not rather difficult and you can customize it however you want. I personally implemented that way the functionality you need.
See here:
String PHONE_CONTACTS_ORDER_CLAUSE = ContactsContract.Contacts.DISPLAY_NAME
+ " ASC";
List<PhoneContact> contacts = new ArrayList<PhoneContact>(); // I have defined the bean PhoneContact
String[] projection = { ContactsContract.Contacts._ID,
ContactsContract.Contacts.DISPLAY_NAME }; //Choose the columns you need
Cursor cursor = this.getContentResolver().query(
ContactsContract.Contacts.CONTENT_URI, projection, null/* the place for your where clause*/, null/* the place for your where args*/,
PHONE_CONTACTS_ORDER_CLAUSE);
startManagingCursor(cursor);
int contactIdIdx = cursor.getColumnIndex(ContactsContract.Contacts._ID);
int displayNameIdx = cursor
.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);
while (cursor.moveToNext()) {
PhoneContact contact = new PhoneContact(); // This is a class i defined, use the data the way you like.
contact.setContactId(cursor.getString(contactIdIdx));
contact.setDisplayName(cursor.getString(displayNameIdx));
contacts.add(contact);
}
EDIT
Sorry got distracted when writing the comment: the Contact id is actually the glue between the different content providers of the Contact related data. These are a few more providers you can use to see whether there are any associated phones or emails with the contact:
ContactsContract.CommonDataKinds.Phone.CONTENT_URI
ContactsContract.CommonDataKinds.Email.CONTENT_URI

Categories

Resources