Fetch address from "canonical_addresses" table - android

I am working on an Android application. I fetched recipient_id from the following uri.
content://mms-sms/conversations?simple=true
Now I want to fetch the address from canonical_addresses" table using the recipient id. But I don't have any idea to query canonical_addresses table". I searched a lot in web. Please help me to find a solution friends.

Canonical table has two columns, _id and address. When you get recipient id you do a lookup for that id in the canonical addresses table. For multiple recipients the ids are separated by a space, so you'll have to split the result like
recipient_ids.split(" ")
and lookup each id.
getContentResolver().query(Uri.parse("content://mms-sms/canonical-addresses"), null, "_id = " + recipientId, null, null);
or
getContentResolver().query(Uri.parse("content://mms-sms/canonical-address/" + recipientId), null, null, null, null);

message = contentResolver.query(uri, null,null, null,SORT_ORDER);
if(message != null){
while (message.moveToNext()) {
String id = message.getString(message.getColumnIndex("_id"));
String thid = message.getString(message.getColumnIndex("thread_id"));
String phoneNumber = message.getString(message.getColumnIndex("address"));
String person = message.getString(message.getColumnIndex("person"));
//String present = message.getString(message.getColumnIndex("reply_path_present"));
//Log.v("sms "+thid,id);
//Log.v("sms "+present,present);
if(phoneNumber !=null ){
//if(phoneNumber.contains("-"))
phoneNumber=phoneNumber.replaceAll("[-() ]", "");
}
else
{
Cursor c =contentResolver.query(Uri.parse("content://mms-sms/conversations?simple=true"), null, "_id = " + thid, null, null);
//getContentResolver().query(Uri.parse("content://mms-sms/canonical-addresses"), null, "_id = " + recipientId, null, null);
if(c.moveToNext())
{
phoneNumber = c.getString(3);
if(phoneNumber !=null ){
//if(phoneNumber.contains("-"))
Log.v(" id=>" +id+ " thid=>"+thid +" first = > " , c.getString(3));
}
}
c.close();
c =contentResolver.query(Uri.parse("content://mms-sms/canonical-addresses"), null, "_id = " + phoneNumber, null, null);
if(c.moveToNext())
{
Log.v(thid +" second = > " , c.getString(1));
phoneNumber = c.getString(1);
phoneNumber=phoneNumber.replaceAll("[-() ]", "");
}
c.close();

Related

How to organize a search a phone number on his part (Android)

My task is to find data on the first entered telephone number.
Do so but fails due to the different format in the database.
Maybe there's another way or how to do the query in one format
private DataContact getDataContact1(String numberPhone) {
DataContact dataContact = new DataContact();
Cursor cursor = contentResolver.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
if (cursor == null) {
return dataContact;
}
countContact = cursor.getCount();
while (cursor.moveToNext()) {
String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
String name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
//numbers
Cursor phones = contentResolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + contactId + " AND " +
ContactsContract.CommonDataKinds.Phone.TYPE + " = " + ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE + " AND " +
ContactsContract.CommonDataKinds.Phone.NUMBER + " LIKE '" + numberPhone + "%'", null, null);
if (phones == null) {
continue;
}
if (phones.moveToNext()) {
String number = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
dataContact.setNumberFormat(number);
dataContact.setNumber(FormatConstants.replaceOnlyNumbers(number));
dataContact.setName(name);
return dataContact;
}
phones.close();
}
cursor.close();
return dataContact;
}

get all mobile number accociated with one contact android

I have contact added to my addressbook which has mutiple number like below.
I want to fetch all 3 number of "User" using ContactsContract content uri.
By using below code i got only one contact.
Cursor cursorAddressBook = mContentResolver.query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);
if (cursorAddressBook != null) {
while (cursorAddressBook.moveToNext()) {
String dataName = cursorAddressBook.getString(cursorAddressBook.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
String dataNumber = cursorAddressBook.getString(cursorAddressBook.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
int dataType = cursorAddressBook.getInt(cursorAddressBook.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DATA2));
String contactId = cursorAddressBook.getString(cursorAddressBook.getColumnIndex(ContactsContract.CommonDataKinds.Phone.CONTACT_ID));
Log.e("last updat Name last", dataName);
Log.e("last updated No last", dataNumber);
Log.e("last updated Type last", dataType);
}
cursorAddressBook.close();
}
From this blogpost If a contact has multiple phone numbers, then You can retrieve all phone numbers and other details using Android’s inbuilt classes (Cursor and ContactsContract ) in Android. And you need to retrieve the contact number based on phone type like (TYPE_MOBILE, TYPE_HOME ect)
{
Cursor cursor = cntx.getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
Integer contactsCount = cursor.getCount(); // get how many contacts you have in your contacts list
if (contactsCount > 0)
{
while(cursor.moveToNext())
{
String id = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
String contactName = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
if (Integer.parseInt(cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0)
{
//the below cursor will give you details for multiple contacts
Cursor pCursor = cntx.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?",
new String[]{id}, null);
// continue till this cursor reaches to all phone numbers which are associated with a contact in the contact list
while (pCursor.moveToNext())
{
int phoneType = pCursor.getInt(pCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));
//String isStarred = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.STARRED));
String phoneNo = pCursor.getString(pCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
//you will get all phone numbers according to it's type as below switch case.
//Logs.e will print the phone number along with the name in DDMS. you can use these details where ever you want.
switch (phoneType)
{
case Phone.TYPE_MOBILE:
Log.e(contactName + ": TYPE_MOBILE", " " + phoneNo);
break;
case Phone.TYPE_HOME:
Log.e(contactName + ": TYPE_HOME", " " + phoneNo);
break;
case Phone.TYPE_WORK:
Log.e(contactName + ": TYPE_WORK", " " + phoneNo);
break;
case Phone.TYPE_WORK_MOBILE:
Log.e(contactName + ": TYPE_WORK_MOBILE", " " + phoneNo);
break;
case Phone.TYPE_OTHER:
Log.e(contactName + ": TYPE_OTHER", " " + phoneNo);
break;
default:
break;
}
}
pCursor.close();
}
}
cursor.close();
}
}
here is the code for fetching all the phone numbers of one contact
String id, name;
ContentResolver cr = getContentResolver();
String sortOrder = ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC";
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, sortOrder);
if (cur.getCount() > 0) {
while (cur.moveToNext()) {
id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
Log.i(tag, "Id is " + id + "\t Name is" + name);
if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[] {
id
}, null);
// this second loop will retrieve all the contact numbers for a paricular contact id
while (pCur.moveToNext()) {
// Do something with phones
int phNumber = pCur.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.NUMBER);
String phn = pCur.getString(phNumber);
Log.i("phn number", phn);
}
pCur.close();
}
}
}

Getting specific contact information

I'm trying to get the given name, family name etc of a specific contact on android but can't seem to be able to. I've been banging my head on this for hours, basically if (nameCur.moveNext()) is always false! This code is originally by #perborin (How to get the first name and last name from Android contacts?). Please help!
P.S. I've added <uses-permission android:name="android.permission.READ_CONTACTS"/> in the AndroidManifest, so that is not the problem.
// A contact ID is fetched from ContactList
Uri resultUri = data.getData();
Cursor cont = getContentResolver().query(resultUri, null, null, null, null);
if (!cont.moveToNext()) {
Toast.makeText(this, "Cursor contains no data", Toast.LENGTH_LONG).show();
return;
}
int columnIndexForId = cont.getColumnIndex(ContactsContract.Contacts._ID);
String contactId = cont.getString(columnIndexForId);
// Fetch contact name with a specific ID
String whereName = ContactsContract.Data.MIMETYPE + " = ? AND " + ContactsContract.CommonDataKinds.StructuredName.CONTACT_ID + " = " + contactId;
String[] whereNameParams = new String[] { ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE };
Cursor nameCur = getContentResolver().query(ContactsContract.Data.CONTENT_URI, null, whereName, whereNameParams, ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME);
while (nameCur.moveToNext()) {
String given = nameCur.getString(nameCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME));
String family = nameCur.getString(nameCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME));
String display = nameCur.getString(nameCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME));
Toast.makeText(this, "Name: " + given + " Family: " + family + " Displayname: " + display, Toast.LENGTH_LONG).show();
}
nameCur.close();
cont.close();
Solved by referring to user3717188 answer at Get first and last name of a contact rather than single display name?.
Cursor phone_cursor = cr.query(ContactsContract.CommonDataKinds.
Phone.CONTENT_URI, null, null, null, null);
while (phone_cursor.moveToNext()) {
try {
int id = Integer.parseInt(phone_cursor.getString(phone_cursor.getColumnIndex
(ContactsContract.CommonDataKinds.Phone.CONTACT_ID)));
Cursor name_cursor = cr.query(ContactsContract.Data.CONTENT_URI,null,
ContactsContract.Data.CONTACT_ID + " = " + id, null, null);
String name = phone_cursor.getString(phone_cursor.getColumnIndex
(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
String first_name ="";
String last_name = "";
while (name_cursor.moveToNext()) {
if(name_cursor.getString(name_cursor.getColumnIndex
(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME))!=null){
first_name = name_cursor.getString(name_cursor.getColumnIndex
(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME));
last_name = name_cursor.getString(name_cursor.getColumnIndex
(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME));
}}
name_cursor.close();
String phoneNumber = phone_cursor.getString(phone_cursor.getColumnIndex
(ContactsContract.CommonDataKinds.Phone.NUMBER));
} catch (Exception e) {
}
}
phone_cursor.close();

context.getContentResolver().query(ContactsContract.Data.CONTENT_URI, null , null ,null, null);

Hi I am working in Android Contact search module.I am running below Query.
cur = context.getContentResolver().query(ContactsContract.Data.CONTENT_URI, null , null ,null, null);
from this query I am getting Result Multiple times.Is there any thing which I am doing wrong.I want DISTINCT Result Set.
please help me.
I think you mean you got duplicate record for some contacts. So you must add condition for your query
String selection = ContactsContract.Contacts.IN_VISIBLE_GROUP + " = '"
+ ("1") + "'";
String sortOrder = ContactsContract.Contacts.DISPLAY_NAME
+ " COLLATE LOCALIZED ASC";
cur = context.getContentResolver().query(
ContactsContract.Contacts.CONTENT_URI, projection, selection
+ " AND " + ContactsContract.Contacts.HAS_PHONE_NUMBER
+ "=1", null, sortOrder);// this query only return contacts which had phone number and not duplicated
Try this code will help you
public void getContact() {
Cursor cur = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
ContentResolver contect_resolver = getContentResolver();
int size = cur.getCount();
if (size > 0 && cur != null) {
for (int i = 0; i < size; i++) {
cur.moveToPosition(i);
String id = cur.getString(cur.getColumnIndexOrThrow(ContactsContract.Contacts._ID));
String name = "";
Cursor phoneCur = contect_resolver.query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID
+ " = ?", new String[] { id }, null);
if (phoneCur.moveToFirst()) {
name = phoneCur.getString(phoneCur .getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
if (!name.equalsIgnoreCase("")) {
String id1 = phoneCur.getString(phoneCur
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.CONTACT_ID));
Cursor emails = getContentResolver()
.query(ContactsContract.CommonDataKinds.Email.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Email.CONTACT_ID
+ " = " + Integer.parseInt(id1),
null, null);
emailAddress="";
if (emails!=null && emails.getCount() > 0) {
emails.moveToFirst();
emailAddress = emails
.getString(emails
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DATA));
}
emails.close();
contact.setEmail(emailAddress);
id1 = "";
mcontact_arraylist.add(contact);
}
phoneCur.close();
}
}
cur.close();
}
}
Each record should contain a portion of the data for a contact (eg each phone number or address is a separate row) each row has a mimetype associated with it that is used to determine the data stored in each column. So for an address, the "data1" column holds the street data and data4 might hold the state.

retriveing contacts in android 2.2

Hi i need to get contact details in edittext when i press a button.But i am getting it right when i have only one contact and it is showing an execption if it has more than one contact.Here is my code & i dont know what should i add to it.So help if anyone knows.Thanks in Adv.
Cursor cursor = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI,null, null, null, null);
while (cursor.moveToNext()) {
String contactId = cursor.getString(cursor.getColumnIndex(
ContactsContract.Contacts._ID));
Cursor phones = getContentResolver().query( ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + contactId , null, null);
startManagingCursor(phones);
while (phones.moveToNext()) {
String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
e2.setText(phoneNumber);
}
//}
Cursor emails = getContentResolver().query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, null, ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = " + contactId, null, null);
startManagingCursor(emails);
while (emails.moveToNext()) {
// This would allow you get several email addresses
String emailAddress = emails.getString(
emails.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
e3.setText(emailAddress);
}
Cursor street = getContentResolver().query(ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.StructuredPostal.CONTACT_ID + " = " + contactId, null, null);
startManagingCursor(street);
while (street.moveToNext()) {
String streetname=street.getString(street.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.FORMATTED_ADDRESS));
e1.setText(streetname);
}
phones.close();
emails.close();
street.close();
cursor.close();
you can visit Contact Information where all contacts and information is queried
Change your while condition in while(!cursor.isAfterLast()).

Categories

Resources