I am looking to fetch complete contact detail of the owner (me profile - e.g. in Nexus). I found ContactsContract.Profile API which is available for API v14+.
I am successfully able to get display name, corresponding to which i further require contact number. To do all this, i am using following code
private void getSelfSimNumber() {
String displayName = null;
try {
Cursor c = this.getContentResolver().query(
ContactsContract.Profile.CONTENT_URI, null, null, null,
null);
int count = c.getCount();
boolean b = c.moveToFirst();
int position = c.getPosition();
if (count == 1 && position == 0) {
displayName = c.getString(c
.getColumnIndex(ContactsContract.Profile.DISPLAY_NAME));
if (Integer
.parseInt(c.getString(c
.getColumnIndex(ContactsContract.Profile.HAS_PHONE_NUMBER))) > 0) {
Cursor pCur = this.getContentResolver().query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME
+ " = ?", new String[] { displayName },
null);
while (pCur.moveToNext()) {
String ret = c.getString(0);
String ret1 = c.getString(1);
}
pCur.close();
}
}
c.close();
} catch (Exception e) {
}
}
The problem is I am unable to fetch contact number of "me" profile. If, suppose, I have total 3 contacts (including "me" profile) then total contacts available i am getting is only 2. The system is perhaps not considering "me" profile as a contact.
How to solve this problem and fetch contact number of "me" profile?
Thanks
Related
Below is the code I used to get all contacts from phone.
public static ArrayList<Recipient> getAllRecipient(Context context) {
ArrayList<Recipient> contacts = new ArrayList<>();
Cursor cursor = context.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);
if (cursor != null) {
try {
final int displayNameIndex = cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);
final int numberIndex = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
final int typeIndex = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE);
final int uriIndex = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.PHOTO_THUMBNAIL_URI);
String displayName, number, uri;
while (cursor.moveToNext()) {
int type = cursor.getInt(typeIndex);
if (type == ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE) {
displayName = cursor.getString(displayNameIndex);
number = cursor.getString(numberIndex);
number = number.replaceAll("[^0-9+]+", "");//remove all special character and space, just keep digit number and "+"
uri = cursor.getString(uriIndex);
Recipient recipient = new Recipient(displayName, number, uri);
contacts.add(recipient);
}
}
} catch (Exception e) {
LogUtil.debug("can't get recipient: " + e.getMessage());
} finally {
cursor.close();
}
}
cursor.close();
return contacts;
}
I got feedback from many users , they can not get full contacts in their phones, show almost contacts but missed some contacts.
Is there any problem with above code ? Thanks.
Use this code
Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,null,null, 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();
Phone.CONTENT_URI includes all Phone entries of the device. If a contact does not have some phone you are not going to get any information about it.
If contacts is what you are after, you should query the ContactsContract.Contacts.CONTENT_URI.
Keep in mind that contacts and phone are two separate things for Android. Not all contacts have phone numbers and you would have to query the numbers separately.
I am developing an application, where I need to fetch the phone number from the contacts corresponding to the name provided. I have tried many codes but none of them seem to work.
Here is the code I'm currently using now
public static String getContactPhoneNumber(Context context, String contactName, int type) {
String phoneNumber = null;
String[] whereArgs = new String[] { contactName, String.valueOf(type) };
Log.d(TAG, String.valueOf(contactName));
Cursor cursor = context.getContentResolver().query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.Contacts.DISPLAY_NAME + " = ? and "
+ ContactsContract.CommonDataKinds.Phone.TYPE + " = ?", whereArgs, null);
int phoneNumberIndex = cursor
.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.NUMBER);
Log.d(TAG, String.valueOf(cursor.getCount()));
if (cursor != null) {
Log.v(TAG, "Cursor Not null");
try {
if (cursor.moveToNext()) {
Log.v(TAG, "Moved to first");
Log.v(TAG, "Cursor Moved to first and checking");
phoneNumber = cursor.getString(phoneNumberIndex);
}
} finally {
Log.v(TAG, "In finally");
cursor.close();
}
}
Log.v(TAG, "Returning phone number");
return phoneNumber;
}
On passing Contact Name (say: John Doe) and type (2 which is int value for Mobile Type), the phone number returned is null even though the contact "John Doe" exists in my contact list.
Please help!!!
Try this
instead of ContactsContract.CommonDataKinds.Phone.CONTENT_URI Pass
ContactsContract.Contacts.CONTENT_URI parameter to query method.
Hi i want to retrieve name and numbers from contact list in android ,am using following code, it will give me name but not number it gives null value for number
NOTE: am using android emulator
public void demo(){
String phoneNumber;
String [] item = new String[]{ People._ID,
People.NAME,
People.NUMBER};
Cursor cur = getContentResolver().query(People.CONTENT_URI ,item , null, null, null);
int phoneNumberIndex = cur.getColumnIndexOrThrow(People.NUMBER);
if (cur != null) {
Log.v("cur not null", "Cursor Not null");
if (cur.moveToNext()) {
Log.v("moveToNext", "Moved to first");
Log.v("moveToNext", "Cursor Moved to first and checking");
phoneNumber = cur.getString(phoneNumberIndex);
System.out.println("****** from_number "+phoneNumber +" *****************");
}
}
}
It gives null because u r trying to retrieve only one number.. this should work..
if (cur.moveToNext()) {
Log.v("moveToNext", "Moved to first");
Log.v("moveToNext", "Cursor Moved to first and checking");
while(cur.moveToNext())
{
phoneNumber = cur.getString(phoneNumberIndex);
System.out.println("****** from_number "+phoneNumber +" *****************");
}
}
I want to query the Contacts Database for phone number and if the selected contact has multiple numbers, I display the available numbers for the user to select one of them.
I have code in place that would pick up the default number of the selected contact.
Cursor cursor = null;
String phoneNumber = "";
try
{
Uri result = data.getData();
// get the contact id from the Uri
String id = result.getLastPathSegment();
// query for everything email
cursor = getContentResolver().query(Phone.CONTENT_URI,
null, Phone.CONTACT_ID + "=?", new String[] { id },
null);
int pNumberIdx = cursor.getColumnIndex(Phone.NUMBER);
// let's just get the first email
if (cursor.moveToFirst())
{
phoneNumber = cursor.getString(pNumberIdx);
}
}
catch (Exception e)
{
}
finally
{
if (cursor != null)
{
cursor.close();
}
EditText emailEntry = (EditText) findViewById(R.id.pNumber);
emailEntry.setText(phoneNumber);
if (phoneNumber.length() == 0)
{
Toast.makeText(this, "No number found for contact.",
Toast.LENGTH_LONG).show();
}
}
i have an app which shows the received messages as a Toast through a BroadcastReceiver. I am presently using the SmsMessage.getOriginatingAddress() method which gives me the number of the sender, how can modify it to get the corresponding name of the sender if stored in the contacts?
You will need to query the contacts for the rest of the data.
First query for the contacts id using the phone number.
Cursor cursor = context.getContentResolver().query(
Uri.withAppendedPath(Contacts.Phones.CONTENT_FILTER_URL, address),
new String[] { Contacts.Phones.PERSON_ID }, null, null, null);
if (cursor != null) {
try {
if (cursor.getCount() > 0) {
cursor.moveToFirst();
Long id = Long.valueOf(cursor.getLong(0));
if (Log.DEBUG) Log.v("Found person: " + id);
return (String.valueOf(id));
}
} finally {
cursor.close();
}
}
Then query for the Contacts name with the id from the first query.
Cursor cursor = context.getContentResolver().query(
Uri.withAppendedPath(Contacts.People.CONTENT_URI, id),
new String[] { PeopleColumns.DISPLAY_NAME }, null, null, null);
if (cursor != null) {
try {
if (cursor.getCount() > 0) {
cursor.moveToFirst();
String name = cursor.getString(0);
if (Log.DEBUG) Log.v("Contact Display Name: " + name);
return name;
}
} finally {
cursor.close();
}
}
You may be able to combine these two queries somehow.