How can I get certain mobile numbers from contact list? - android

I have an app that has to show my phone contact list. But how can I get some mobile numbers (that I want)?

For your example 6XXXXXXX type of number, here is a query to get that type of numbers only.
ContentResolver cr = getContentResolver();
Cursor contacts = cr.query(ContactsContract.Contacts.CONTENT_URI, null,
ContactsContract.Contacts.DISPLAY_NAME + " LIKE '"
+ " 6 " + "%' ", null, "UPPER("
+ ContactsContract.Contacts.DISPLAY_NAME + ") ASC");
Hope it will help you.

Working for me :
Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,null,null, null);
while (phones.moveToNext())
{
String Number=phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
}
Add a permisson to app manifest file :
<uses-permission android:name="android.permission.READ_CONTACTS"/>

Related

Contact list not sort in Android

I am trying to fetch my phone contacts in alphabetical sort order.Its fetch name by fast but not getting sort order .I tried ContactsContract.Contacts.SORT_KEY_PRIMARY + " ASC" also ContactsContract.Contacts.DISPLAY_NAME + " ASC" but not getting good result.
My code is
Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,null,null,ContactsContract.Contacts.DISPLAY_NAME + " ASC");
while (phones.moveToNext())
{
String name=phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
Log.e("Tag ","Name " + name);
}
phones.close();
you have to get the sort order of your phone contacts like this:
int sort_order=Settings.system.getInt (getApplicationContext ().getContentResolver (),"android.contacts.SORT_ORDER");
now your cursor query will be like this:
Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,null,null,sort_order);
you can get contacts in alphabatical order:
Cursor cursor = getContentResolver.query(Phone.CONTENT_URI, null, null, null, Phone.DISPLAY_NAME + " ASC");

android showing both sim and phone contacts

In my code I should display only phone contacts: I followed previous posts but I still display both phone and sim contacts. Here it is my code:
Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
String columIndex = ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME;
String columIndexId = ContactsContract.CommonDataKinds.Phone.CONTACT_ID;
String numIndex = ContactsContract.CommonDataKinds.Phone.NUMBER;
Cursor cursor = getContentResolver().query(uri, null, null, null,ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME+" ASC");
if(cursor!=null){
while(cursor.moveToNext()) {
ContactInfo ci = new ContactInfo();
ci.setIdContact(Integer.parseInt(cursor.getString(cursor.getColumnIndex(columIndexId))));
ci.setName(cursor.getString(cursor.getColumnIndex(columIndex)));
ci.setNumberTel(cursor.getString(cursor.getColumnIndex(numIndex)));
//if(!cursor.getString(cursor.getColumnIndex(columIndex)).equalsIgnoreCase(nome))
listContact.add(ci);
}
cursor.close();
}
These are all ContactInfo object and they will be showed in a list (listContact, which is an ArrayList).
It is really important for me because my application works good only on phone contacts and not on sim contacts.
You can try to replace your Cursor with this to exclude contacts on sim:
import android.provider.ContactsContract.RawContacts;
Cursor cursor = getContentResolver().query(
RawContacts.CONTENT_URI,
new String[] { RawContacts._ID, RawContacts.ACCOUNT_TYPE },
RawContacts.ACCOUNT_TYPE + " <> 'com.android.contacts.sim' AND "
+ RawContacts.ACCOUNT_TYPE + " <> 'com.anddroid.contacts.sim' AND " // HTC
+ RawContacts.ACCOUNT_TYPE + " <> 'vnd.sec.contact.sim' AND "
+ RawContacts.ACCOUNT_TYPE + " <> 'USIM Account' ",
null,
null);
I haven't tried this, modified from https://stackoverflow.com/a/4409453/262462
Note: on some phones you may need to exclude few other RawContacts.ACCOUNT_TYPE like com.anddroid.contacts.sim and vnd.sec.contact.sim, see https://stackoverflow.com/a/13656077/262462

Personal favourite and recent call list for android

I been trying out the other tutorials on the call log and favourite. But this method is always using the native android phonebook. Is there any way to make a favourite call list and recent call list just personal to my app (only my app can use it)? Any help/leads would be great.
Here is a code to get recent call list:
ContentResolver cr = getContentResolver();
String strOrder = CallLog.Calls.DATE + " DESC";
Cursor cur = cr.query(CallLog.Calls.CONTENT_URI, null, selection, null,
strOrder);
also here is a code to get your favorite contacts:
Cursor starred = cr.query(ContactsContract.Contacts.CONTENT_URI,null,ContactsContract.Contacts._ID + " = " + contactId + "AND" + ContactsContract.Contacts.STARRED + "= 1" ,null, null);
Hope this will help you.

Retrieve a list of contacts having phone number like the dialed number

I'm trying to retrieve the phone contacts having phone number starting with the number being dialled. For eample if I type 123, I would like to retrieve all the contacts having contact number starting with 123. I'm using the following code for this:
Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
Cursor cursor = this.getContentResolver().query(
uri,
new String[] { ContactsContract.CommonDataKinds.Phone.NUMBER,
ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME },
ContactsContract.CommonDataKinds.Phone.NUMBER + " LIKE '" + dialledNumber + "%'", null,
ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " ASC");
The issue with this code is, if I have saved a contact like +919-9.... and another like +9199...., when I dial +9199 I can't retrieve both the contacts. I would like to escape the character "-" while querying the contacts. How could I do this? Please help. Thank you.
System.out.print("1-2-3".replaceAll("\\-", ""));
"My problem is not with the dialled number. Even if I dial 1234 or 123-4, I need all the contacts with phone number starting with 1234. But here if I type 1234 only contacts starting with 1234 is retrieved and not 123-4."
that is because you directly check for the number.try making your string dialledNumber such that it looks like 123-456-7890 then query for both this string like:
If suppose,
String dialledNumber="1234";
String dialledNumberFormatted="123-4"; // Create this on your own from dialledNumber you get
Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
Cursor cursor = this.getContentResolver().query(
uri,
new String[] { ContactsContract.CommonDataKinds.Phone.NUMBER,
ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME },
ContactsContract.CommonDataKinds.Phone.NUMBER + " LIKE '" + dialledNumber + "%'" +" OR "+ContactsContract.CommonDataKinds.Phone.NUMBER + " LIKE '" + dialledNumberFormatted + "%'", null,
ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " ASC");
This is a type of hack but you can use it doesn't cause problem for you anywhere else.

Android; I only have 2 contacts, yet I can obtain 5 from a query, why?

I have setup 2 test contacts in my emulator.
I'm running the following query, it should pick them both out, populate my domain object, and add to a list. The output at the bottom should therefore be 2, but it is 5, why is this? (cursor.getCount() is 5 instead of 2)
I have stepped through each iteration of the while loop and it is retreving the same contact multiple times, but with different values for POSTCODE, such as the phone number
ContentResolver cr = getContentResolver();
Cursor cursor = cr.query(ContactsContract.Data.CONTENT_URI,
null, null, null, null);
List<MeCercanaContact> contacts = new ArrayList<MeCercanaContact>();
if (cursor.getCount() > 0)
{
while (cursor.moveToNext())
{
MyContact myContact = new MyContact();
String givenName = cursor.getString(cursor.getColumnIndex(
ContactsContract.Contacts.DISPLAY_NAME));
String postcode = cursor.getString(cursor.getColumnIndex(
ContactsContract.CommonDataKinds.StructuredPostal.POSTCODE));
myContact.setFirstName(givenName);
myContact.setLastName(postcode);
contacts.add(myContact);
}
}
System.out.println(contacts.size());
After API 21 We Write this Query for remove contact duplicacy.
String select = ContactsContract.Data.HAS_PHONE_NUMBER + " != 0 AND " +
ContactsContract.Data.MIMETYPE
+ " = " + ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE + "
AND "+ ContactsContract.Data.RAW_CONTACT_ID + " = " +
ContactsContract.Data.NAME_RAW_CONTACT_ID;
Cursor cursor = mContent.query(ContactsContract.Data.CONTENT_URI, null, select,
null, null);
You are querying ContactsContract.Data, which is a generic container that holds a list of various contact details, such as phone numbers, postal codes etc.. You must filter the results for the rows whose ContactsContract.Data.MIMETYPE column equals StructuredPostal.CONTENT_ITEM_TYPE:
So change the query to:
Cursor cursor = cr.query(ContactsContract.Data.CONTENT_URI,
null, null, ContacsContract.Data.MIMETYPE + "='" +
ContactsContract.StructuredPostal.CONTENT_ITEM_TYPE + "'", null);
See ContactsContract.Data
a contact that is registered to multiple groups will show up multiple times
if you query the Uri CONTENT_URI = ContactsContract.Data.CONTENT_URI
Add this to your SELECTION:
+ ContactsContract.Data.DATA1 + " = 1 " ; //show only contacts in group 1

Categories

Resources