How to get the address field of a contact? - android

In my app, I want to get the address field of a selected contact. I managed to get the contact name but I don't know how to get the address. This is my code:
#Override
public void onActivityResult(int reqCode, int resultCode, Intent data) {
super.onActivityResult(reqCode, resultCode, data);
switch (reqCode) {
case (PICK_CONTACT) :
if (resultCode == Activity.RESULT_OK) {
Uri contactData = data.getData();
Cursor c = getActivity().getContentResolver().query(contactData, null, null, null, null);
c.moveToFirst();
String name = c.getString(c.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
Log.i(TAG, "name contact: "+ name);
}
break;
}
}
Thank you so much!

Try following
String id = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
Cursor address_cursror = getContentResolver().query(ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_URI, null, ContactsContract.CommonDataKinds.StructuredPostal.CONTACT_ID + " = ?", new String[] { id }, null);
while (address_cursror.moveToNext())
{
String name = address_cursror.getString(address_cursror.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.DISPLAY_NAME));
String street = address_cursror.getString(address_cursror.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.STREET));
String state = address_cursror.getString(address_cursror.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.REGION));
String zip = address_cursror.getString(address_cursror.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.POSTCODE));
String city = address_cursror.getString(address_cursror.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.CITY));
}
} while (cursor.moveToNext());

ContactsContract.CommonDataKinds.StructuredPostal.CITY
ContactsContract.CommonDataKinds.StructuredPostal.STREET
ContactsContract Structured Postal docs
Please check the dev docs before come here to ask question.

Related

Retrieve all numbers from one contact issue

I know this is a common question since I found many answers about it, but I still have an issue regarding phone number retrieving.
I want to get ALL numbers from one contact picked from the PICK_CONTACT activity.
Here is my code from the onActivityResult() block
#Override
public void onActivityResult(int reqCode, int resultCode, Intent data) {
super.onActivityResult(reqCode, resultCode, data);
switch (reqCode) {
case (PICK_CONTACT) :
if (resultCode == Activity.RESULT_OK) {
ArrayList<String> allContacts = new ArrayList<String>();
Uri contactData = data.getData();
Cursor c = getContentResolver().query(contactData, null, null, null, null);
while (c.moveToNext()) {
String contact_id = c.getString(c.getColumnIndex( _ID ));
String hasPhone =c.getString(c.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
if (hasPhone.equalsIgnoreCase("1")) {
Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[] { contact_id }, null);
while (phones.moveToNext())
{
String contactNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
allContacts.add(contactNumber);
break;
}
phones.close();
}
String name = c.getString(c.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
TXT_EmergencyContactNameProfile.setText(name);
}
}
break;
}
}
I expected the loop while (phones.moveToNext()) to get all different numbers ... but no
Thank you very much in advance ;)
Try to delete the "break;" inside the while loop.

How to get phone number type label from given phone number in android

I tried to print Contact name(Eg. "Jose") & Contact phone number type value(Eg. "Home","Work",etc.,) from phone contact for given particular phone number(Eg. "9600515852")
for this i used bellow code. but i can able get Contact name only. i am getting app crash when i am trying to get the phone number type.
Please guide me.
String number = getContactName(this, "9600515852");
Toast.makeText(getApplicationContext(),number,Toast.LENGTH_LONG).show();
private String getContactName(Context context, String number) {
String TAG ="" ;
String name = null, label=null;
int type=0;
// define the columns I want the query to return
String[] projection = new String[] { ContactsContract.PhoneLookup.DISPLAY_NAME,ContactsContract.PhoneLookup._ID };
// encode the phone number and build the filter URI
Uri contactUri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(number));
// query time
Cursor cursor = context.getContentResolver().query(contactUri, projection, null, null, null);
if(cursor != null) {
if (cursor.moveToFirst()) {
name = cursor.getString(cursor.getColumnIndex(ContactsContract.PhoneLookup.DISPLAY_NAME));
// type = cursor.getInt(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));
} else {
Log.v(TAG, "Contact Not Found # " + number);
}
cursor.close();
}
return name+" - "+number;
}
use
name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
instead of
name = cursor.getString(cursor.getColumnIndex(ContactsContract.PhoneLookup.DISPLAY_NAME));
In My case, On button click getting contact information from Contacts app is like this. And it is working
Call an Intent
Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
intent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE);
startActivityForResult(intent, 1);
Handle the result
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case (1):
if (resultCode == Activity.RESULT_OK) {
Uri contactData = data.getData();
Cursor c = managedQuery(contactData, null, null, null, null);
if (c.moveToFirst()) {
String id = c.getString(c.getColumnIndex(ContactsContract.Contacts._ID));
String name = c.getString(c.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
String resultedPhoneNum = pop.getContactNumber(id, MainActiv.this);
String resultedName=pop.getContactName(name, MainActiv.this);
contactName.setText(resultedName);
}
}
break;
}
}
Hope that helps!
Well the correct approach is to include the Phone.TYPE in your Projection query. It contains only DISPLAY_NAME andID and it crashes as there is no field as "type" included in your query.
This will definitely solve your issue.
Try this:
type = cursor.getString(cursor.getColumnIndex(ContactsContract.PhoneLookup.TYPE));

how can i get phone number of selected contact

c.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Intent i = new Intent(ContactsContract.Contacts.CONTENT_TYPE);
Intent i = new Intent(Intent.ACTION_PICK);
i.setType(ContactsContract.Contacts.CONTENT_TYPE);
startActivityForResult(i, CONTACT_PICKER_RESULT); }
});
}
public void onActivityResult(int requestCode ,int resultCode ,Intent data) {
if ((requestCode == CONTACT_PICKER_RESULT) && (resultCode == RESULT_OK)) {
}
}
This code contains the intent opens the contacts but I need if select contact take a contact phone number then set it to string
Check this solution: How to call Android contacts list?
Its pretty much what you need to do but instead name you should search the number with the cursor.
PS: Next time search better, there is alot of people asking it.
You can try this.
#Override
public void onActivityResult(int reqCode, int resultCode, Intent data){
super.onActivityResult(reqCode, resultCode, data);
if(reqCode == this.pickContact){
if (resultCode == Activity.RESULT_OK) {
Log.d("ContactsH", "ResOK");
Uri contactData = data.getData();
Cursor contact = getContentResolver().query(contactData, null, null, null, null);
if (contact.moveToFirst()) {
String name = contact.getString(contact.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
// TODO Whatever you want to do with the selected contact's name.
ContentResolver cr = getContentResolver();
Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null,
"DISPLAY_NAME = '" + name + "'", null, null);
if (cursor.moveToFirst()) {
String contactId =
cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
//
// Get all phone numbers.
//
Cursor phones = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + contactId, null, null);
while (phones.moveToNext()) {
String number = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
int type = phones.getInt(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));
switch (type) {
case ContactsContract.CommonDataKinds.Phone.TYPE_HOME:
// do something with the Home number here...
break;
case ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE:
// do something with the Mobile number here...
Log.d("ContactsH", number);
break;
case ContactsContract.CommonDataKinds.Phone.TYPE_WORK:
// do something with the Work number here...
break;
}
}
phones.close();
}
cursor.close();
}
}
}else{
Log.d("ContactsH", "Canceled");
}
}
That works for me.

Getting Contacts numbers

I am having problem getting the number of contact that i have selected. In my app I call the built in application for contacts as a new activity, when you choose a contact the activity returns his name, now I need it to return all the numbers that the user entered for that contact. I found the code in some tutorial on the internet... Here is the code
This is how i call the contact activity
mSuspectButton = (Button) v.findViewById(R.id.crime_suspect);
mSuspectButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent i = new Intent(Intent.ACTION_PICK,
ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(i, REQUEST_CONTACT);
}
});
This is how i get the name of the selected contact:
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode != Activity.RESULT_OK)
return;
if (requestCode == REQUEST_CONTACT) {
Uri contactUri = data.getData();
// Specify which fields you want your query to return
// values for.
String[] queryFields = new String[] {
ContactsContract.Contacts.DISPLAY_NAME };
// Perform your query - the contactUri is like a "where"
// clause here
Cursor c = getActivity().getContentResolver().query(contactUri,
queryFields, null, null, null);
// Double-check that you actually got results
if (c.getCount() == 0) {
c.close();
return;
}
// Pull out the first column of the first row of data -
// that is your suspect's name.
c.moveToFirst();
String suspect = c.getString(0);
mCrime.setSuspect(suspect);
mSuspectButton.setText(suspect);
c.close();
}
}
So please can anyone help, I really don't understand how these works?
Use this in your onActivityResult, this will return you the selected number
#Override
public void onActivityResult(int reqCode, int resultCode, Intent data){
super.onActivityResult(reqCode, resultCode, data);
switch(reqCode){
case (PICK_CONTACT):
if (resultCode == Activity.RESULT_OK){
Uri contactData = data.getData();
Cursor c = managedQuery(contactData, null, null, null, null);
if (c.moveToFirst()){
String id = c.getString(c.getColumnIndexOrThrow(ContactsContract.Contacts._ID));
String hasPhone =
c.getString(c.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
if (hasPhone.equalsIgnoreCase("1")) {
Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ id,null, null);
phones.moveToFirst();
String cNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
}
}
}
}
}
1) Write this code to fetch all contact numbers and names.
2) Add them in separate arraylists.
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)).replaceAll(" ", "");
alContactNames.add(name);
alContactNumbers.add(phoneNumber);
}
phones.close();
3) make listview in your app with custom adapter. In each list item, there needs to be the contact number , contact name and check box for selection.
this will solve your problem.

How to pick contact number from phone-book of android in to my application?

all
i want to take number from phonebook of android in my application database..
i have tried it with below code but here am getting name of person instead i want number from phonebook and want to store it in my database..how to achieve this????can any one guide me..
#Override
public void onActivityResult(int reqCode, int resultCode, Intent data){
super.onActivityResult(reqCode, resultCode, data);
switch(reqCode){
case (PICK_CONTACT):
if (resultCode == Activity.RESULT_OK){
Uri contactData = data.getData();
Cursor c = managedQuery(contactData, null, null, null, null);
if (c.moveToFirst()){
// other data is available for the Contact. I have decided
// to only get the name of the Contact.
String name = c.getString(c.getColumnIndexOrThrow(ContactsContract.Contacts.CONTENT_TYPE));
Toast.makeText(getApplicationContext(), name, Toast.LENGTH_SHORT).show();
}
}
}
Thanks in Advance--
try this code,
#Override
public void onActivityResult(int reqCode, int resultCode, Intent
data){
super.onActivityResult(reqCode, resultCode, data);
switch(reqCode){
case (PICK_CONTACT):
if (resultCode == Activity.RESULT_OK)
{
Uri contactData = data.getData();
Cursor c = managedQuery(contactData, null, null, null, null);
if (c.moveToFirst()) {
String id =
c.getString(c.getColumnIndexOrThrow(ContactsContract.Contacts._ID));
String hasPhone =
c.getString(c.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
if (hasPhone.equalsIgnoreCase("1")) {
Cursor phones = getContentResolver().query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ id,
null, null);
phones.moveToFirst();
String cNumber = phones.getString(phones.getColumnIndex("data1"));
}
}
}
}
I know that this is an old question, but I think the below resource from Android is extremely helpful on this matter. The "bonus" section is the exact code that raj was looking for. I think this link should be helpful to anyone who sees this question in the future, especially if you don't understand CapDroid's snippet.
http://developer.android.com/training/basics/intents/result.html
This will help you:
public void onActivityResult(int reqCode, int resultCode, Intent data) { super.onActivityResult(reqCode, resultCode, data);
try {
if (resultCode == Activity.RESULT_OK) {
Uri contactData = data.getData();
Cursor cur = managedQuery(contactData, null, null, null, null);
ContentResolver contect_resolver = getContentResolver();
if (cur.moveToFirst()) {
String id = cur.getString(cur.getColumnIndexOrThrow(ContactsContract.Contacts._ID));
String name = "";
String no = "";
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));
no = phoneCur.getString(phoneCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
}
Log.e("Phone no & name :***: ", name + " : " + no);
txt.append(name + " : " + no + "\n");
id = null;
name = null;
no = null;
phoneCur = null;
}
contect_resolver = null;
cur = null;
// populateContacts();
}
} catch (IllegalArgumentException e) {
e.printStackTrace();
Log.e("IllegalArgumentException::", e.toString());
} catch (Exception e) {
e.printStackTrace();
Log.e("Error :: ", e.toString());
}
}

Categories

Resources