How to get rawcontact id with contact id and lookup key - android

I am trying to get rawcontacts._ID from contacts provider by using the lookup key and contact id stored in the application.
This function exists that might help me but I am not sure how to construct the rawContactUri
public static Uri getContactLookupUri (ContentResolver resolver, Uri rawContactUri)
Build a CONTENT_LOOKUP_URI style Uri for the parent ContactsContract.Contacts entry of the given ContactsContract.RawContacts entry.

ContentResolver cr = getBaseContext()
.getContentResolver();
Cursor cur = cr
.query(ContactsContract.Contacts.CONTENT_URI,
null,
null,
null,
null);
if (cur.getCount() > 0) {
Log.i("Content provider", "Reading contact emails");
while (cur
.moveToNext()) {
String contactId = cur
.getString(cur
.getColumnIndex(ContactsContract.Contacts._ID));
}
}

Related

How to get phone number for each SMS on the device?

I'm getting all phone's SMS with this URI - Telephony.Sms.Inbox.CONTENT_URI
TextBasedSmsColumns contains all columns for a projection.
Cursor is built like this:
cursor = getContentResolver().query(Telephony.Sms.Inbox.CONTENT_URI,
new String[] { BaseColumns._ID, TextBasedSmsColumns.ADDRESS },
null, null, null);
There is a TextBasedSmsColumns.ADDRESS column that contains a phone number OR a contact name. So how can I always get the phone number for the SMS? Should I use another approach to get all SMS?
This is how I got.
Uri uriSMSURI = Uri.parse("content://sms/inbox");
Cursor cur = getContentResolver().query(uriSMSURI, null, null, null, null);
if (cur != null) {
while (cur.moveToNext()) {
String body = cur.getString(cur.getColumnIndexOrThrow("body"));
//Here you will get all PhoneNumbers
String phoneNumber = cur.getString(cur.getColumnIndexOrThrow("address"));
}
cur.close();
}

Android: sms PERSON attribute different than _ID of contact

I've some problems with android database.
I've to fill a contactObject class from the default android database with the following code :
/**
* this function fill a contact with a given phoneNumber
* #param phoneNumber the phone number to fill the contact
* #return the recently created contact
*/
public Contact fillContact(String phoneNumber) {
Contact contact = new Contact(phoneNumber);
contact.setName(phoneNumber);
ContentResolver cr = getCurrentContex().getContentResolver();
Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phoneNumber));
Cursor cur = cr.query(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));
contact.setId(Integer.valueOf(id));
contact.setName(name);
}
}
cur.close();
return contact;
}
However, when I want to recover SMS from Telephony.Sms table, PERSON attribute is different than contact.getId().
How can I get the equivalent of PERSON attribute in a Contact ?
Thanks a lot.

How to get last inserted native contact id in android

I have to get distinct raw contact id using content resolver.
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));
System.out.println("id............"+id+"..."+name);
}
}
Ist i have inserted all contacts into native contacts.Then Using above code i got all native contact id. How to get one id at a time after inserting 1 contact. Please help me.Thanks in advance.
Replace to
cursor.moveToLast();
you can easily get last id of the database

Get RawContactId of specific Contact based off PhoneLookup

I am looking to get the RAW_CONTACT_ID of a specific Contact using PhoneLookup or even just the Contacts LookupKey.
I know the contacts table has a column name_raw_contact_id that references the raw_contacts._id column but it doesn't seem to be returned when querying ContactsContract.Contacts.CONTENT_LOOKUP_URI with the contacts lookup key.
My phone lookup query is:
String[] projection = new String[] {
PhoneLookup.DISPLAY_NAME, PhoneLookup.LOOKUP_KEY };
Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(number));
Cursor c = resolver.query(uri, projection, null, null, null);
Then I am looking up the Contact based on the lookup key:
String[] contactProjection = new String[] {
ContactsContract.Contacts.NAME_RAW_CONTACT_ID
};
Uri contactUri = Uri.withAppendedPath(
ContactsContract.Contacts.CONTENT_LOOKUP_URI, lookupKey);
Cursor contactCursor = resolver.query(contactUri,
contactProjection, null, null, null);
However, this doesn't compile and I get
cannot find symbol: variable NAME_RAW_CONTACT_ID
location: class android.provider.ContactsContract.Contacts
But the android documentation shows NAME_RAW_CONTACT_ID as a column.
Is there any way I can get the RAW_CONTACT_ID based off either phone number or lookup key?
I found that the answer is to make a third query:
long rawContactId = -1;
Cursor c = getContentResolver().query(RawContacts.CONTENT_URI,
new String[]{RawContacts._ID},
RawContacts.CONTACT_ID + "=?",
new String[]{String.valueOf(contactId)}, null);
try {
if (c.moveToFirst()) {
rawContactId = c.getLong(0);
}
} finally {
c.close();
}
But it should be noted that there can be multiple RawContact's per one Contact and the above query will get ALL RawContact's associated with the contactId
ContactsContract.Contacts.NAME_RAW_CONTACT_ID column needs API level 21 or greater so make sure your are compiling with this version.
Also you can use ContactsContract.PhoneLookup._ID column in your first query to get the Contact_Id and then use this Contact_Id in your 3rd query so your problem will be solved in 2 queries instead of 3.
private HashSet<Long> getRawContactIdsForContact(long contactId)
{
HashSet<Long> ids = new HashSet<Long>();
Cursor cursor = context.getContentResolver().query(RawContacts.CONTENT_URI,
new String[]{RawContacts._ID},
RawContacts.CONTACT_ID + "=?",
new String[]{String.valueOf(contactId)}, null);
if (cursor != null && cursor.moveToFirst())
{
do
{
ids.add(cursor.getLong(0));
} while (cursor.moveToNext());
cursor.close();
}
return ids;
}

Search contact by phone number

In my app, user writes a phone number, and I want to find the contact name with that phone number?
I usually search the contacts like this:
Cursor cur = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI,
null, null, null, null);
But I do this to access all contacts... In this app I only want to get the contact name of the given phone number... How can I restrict the query?
Or do I have to go trough all contacts and see if any has the given phone number? But I believe that this can be very slow this way...
If you want the complete 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();
}
}
return name;
}
You should have a look at the recommended ContactsContract.PhoneLookup provider
A table that represents the result of looking up a phone number, for example for caller ID. To perform a lookup you must append the number you want to find to CONTENT_FILTER_URI. This query is highly optimized.
Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phoneNumber));
resolver.query(uri, new String[]{PhoneLookup.DISPLAY_NAME,...

Categories

Resources