I'm trying to access the contact name, phone number, and address of a person which I selected from contact picker intent.
Here is the code which I am using to open the contact picker Intent :
Here is the code which I am using :
Intent intent = new Intent(Intent.ACTION_PICK,
ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(intent, PICK_CONTACT);
case PICK_CONTACT:
if (resultCode == Activity.RESULT_OK) {
System.out.println("in on ActivityResult");
Uri contactData = data.getData();
Cursor c = getActivity().managedQuery(contactData, null,
null, null, null);
if (c.moveToFirst()) {
String id =
c.getString(c.getColumnIndexOrThrow(ContactsContract.Contacts._ID));StringhasPhone=c.getString(c.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
if (hasPhone.equalsIgnoreCase("1")) {
Cursor phones getActivity().getContentResolver().query(ContactsContract.CommonDataKind s.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID
+ " = " + id, null, null);
phones.moveToFirst();
String cNumber =
phones.getString(phones.getColumnIndex("data1"));String name phones.getString(phones.getColumnIndex(ContactsContract.Data.DISPLAY_NAME));
//here you can find out all the thing.
System.out.println("NAME:"+name);
}Cursor postal_cursor=getActivity().getContentResolver().query(ContactsContract.Data.CONTENT_URI,
new String[]{
ContactsContract.CommonDataKinds.StructuredPostal.STREET,
ContactsContract.CommonDataKinds.StructuredPostal.CITY,
ContactsContract.CommonDataKinds.StructuredPostal.FORMATTED_ADDRESS},
ContactsContract.Data.CONTACT_ID + "=? AND " +
ContactsContract.CommonDataKinds.StructuredPostal.MIMETYPE + "=?",
new String[]{String.valueOf(id),
ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE},
null);
postal_cursor.moveToFirst();
while(postal_cursor.moveToNext())
{
String Strt =
postal_cursor.getString(postal_cursor.getColumnIndex(
ContactsContract.CommonDataKinds.StructuredPostal.STREET));
String Cty =
postal_cursor.getString(postal_cursor.getColumnIndex(
ContactsContract.CommonDataKinds.StructuredPostal.CITY));
String cntry =
postal_cursor.getString(postal_cursor.getColumnIndex(
ContactsContract.CommonDataKinds.StructuredPostal.COUNTRY));
String address =
postal_cursor.getString(postal_cursor.getColumnIndex(
ContactsContract.CommonDataKinds.StructuredPostal.FORMATTED_ADDRESS));
}
postal_cursor.close(); }
but I am not able to get the address which is stored.
Any help is appreciated. Thanks
If you don't have READ_CONTACTS permission, you'll only be able to get information from the Contacts.CONTENT_URI table, not specific info like phone or postal address.
If you'd like to get a phone, you can use the special phone-picker intent instead like:
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType(CommonDataKinds.Phone.CONTENT_TYPE);
startActivityForResult(intent, REQUEST_CODE);
or to get a postal address:
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType(CommonDataKinds.StructuredPostal.CONTENT_TYPE);
startActivityForResult(intent, REQUEST_CODE);
See here: https://developer.android.com/guide/components/intents-common#PickContactDat
If you want to get both pieces of information, you'd need to ask the user for a READ_CONTACTS permissions for those queries to work.
Related
I'm making an application in which user can add users from the application into his contacts.
Intent intent = new Intent(ContactsContract.Intents.Insert.ACTION);
intent.setType(ContactsContract.RawContacts.CONTENT_TYPE);
intent.putExtra(ContactsContract.Intents.Insert.PHONE, bean.getMobileNo());
intent.putExtra(ContactsContract.Intents.Insert.NAME, bean.getName());
intent.putExtra(ContactsContract.Intents.Insert.EMAIL, bean.getEmailID());
startActivity(intent);
so adding it is not the issue the issue when it goes to add contact screen and if the user presses back button the contact is getting saved even if the user doesn't want to save the contact.
I want to do this using Intent only not through app. Is there any solution for this or is it device specific?
Try this
/**
* Open the add-contact screen with pre-filled info
*/
Intent intent = new Intent(Intent.ACTION_INSERT);
intent.setType(ContactsContract.Contacts.CONTENT_TYPE);
intent.putExtra(ContactsContract.Intents.Insert.NAME, bean.getName());
intent.putExtra(ContactsContract.Intents.Insert.PHONE, bean.getMobileNo());
intent.putExtra(ContactsContract.Intents.Insert.EMAIL, bean.getEmailID());
context.startActivity(intent);
Try this,
// Declare
static final int PICK_CONTACT = 1;
Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(intent, PICK_CONTACT);
//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();
cNumber = phones.getString(phones.getColumnIndex("data1"));
System.out.println("number is:" + cNumber);
}
String name = c.getString(c.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
}
}
break;
}
}
This may helps you.
If I understand your post properly, the issue is that regardless if the user wants to add a contact or not, the contact information gets added anyway.
As I noticed, you are already calling startActivity(intent), which saves the contact. Only run startActivity(intent) if the user has already approved/verified that he wants to add the contact. I suggest using a Dialog for the confirmation. If the user accepts, run startActivity(intent), if not, then simply disregard the action. Like so:
if (true){ // user accepts
Intent intent = new Intent(ContactsContract.Intents.Insert.ACTION);
intent.setType(ContactsContract.RawContacts.CONTENT_TYPE);
intent.putExtra(ContactsContract.Intents.Insert.PHONE, bean.getMobileNo());
intent.putExtra(ContactsContract.Intents.Insert.NAME, bean.getName());
intent.putExtra(ContactsContract.Intents.Insert.EMAIL, bean.getEmailID());
startActivity(intent);
} else {
// your code here.
}
I think this post might also be useful for you. Cheers! :D
I'm writing an app that get contacts and all detail , such as any numbers in each contact. it works fine but i have a problem. as you see in below images when I call:
Intent intent = new Intent(Intent.ACTION_GET_CONTENT); intent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE);
startActivityForResult(intent, 1);
first of all I see the (open from) screen like this:
and when i select " Contacts " the second screen is shown:
Is there any way that I call the the method, the second screen with contacts and detail numbers directly shown and i needless to see the first screen and select " Contacts ".
Thanks.
After all I found this way:
instead of:
Intent intent = new Intent(Intent.ACTION_GET_CONTENT); intent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE);
I have write these lines:
Intent intent = new Intent(Intent.ACTION_PICK,Uri.parse("content://contacts/people")); intent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE);
I also had the same problem. Finally, I got rid of intermediate picker screen using below code,
Intent i=new Intent(Intent.ACTION_PICK);
i.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE);
startActivityForResult(i, SELECT_PHONE_NUMBER);
In onActivityResult get phone number as below
if (requestCode == SELECT_PHONE_NUMBER && resultCode == RESULT_OK) {
// Get the URI and query the content provider for the phone number
Uri contactUri = data.getData();
String[] projection = new String[]{ContactsContract.CommonDataKinds.Phone.NUMBER};
Cursor cursor = getContext().getContentResolver().query(contactUri, projection,
null, null, null);
// If the cursor returned is valid, get the phone number
if (cursor != null && cursor.moveToFirst()) {
int numberIndex = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
String number = cursor.getString(numberIndex);
// Do something with the phone number
...
}
cursor.close();
}
I want to display the native android contacts list and then once the user picks a contact, i would like to move to the next native screen where it shows the details of the contact (like phone numbers, email, etc).
Now once the user selects either an email address or a phone number, I would then like to get that data back to my original activity and do further processing.
I know that I could use
startActivityForResult(new Intent(Intent.ACTION_PICK, Contacts.CONTENT_URI), REQUEST_CODE_PICK_CONTACT);
and then myself parse the contact using getContentResolver().query() and then store each of the required fields. Then finally display on a separate activity as contact details and then once the user chooses any one of the phone numbers or emails, then i use that to do further processing.
I tried to do the following inside onActivityResult():
Uri contactData = data.getData();
Intent intent = new Intent(Intent.ACTION_PICK, contactData);
startActivityForResult(intent, REQUEST_CODE_PICK_CONTACT_DETAIL);
This displays the details of the contact but when the user selects a particular number, android calls it instead of passing it back to me in the previous activity.
But I would like to use the native screens if possible and it would require a lot less parsing and no extra activities/fragments.
Could someone suggest how to go about doing this?
Thanks
You can get contact details from phone contact doing this way
Uri uri = data.getData();
String[] projection = new String[] {ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Phone.NUMBER};
Cursor people = getActivity().getContentResolver().query(uri, projection, null, null, null);
int indexName = people.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
int indexNumber = people.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
int type = people.getColumnIndex(ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE);
people.moveToFirst();
do {
String name = people.getString(indexName);
String number = people.getString(indexNumber);
System.out.println(name+number);
} while (people.moveToNext());
This might help:
Uri contactData = data.getData();
ContentResolver cr = getContentResolver();
Cursor c = cr.query(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));
Toast.makeText(getApplicationContext(), cNumber,
Toast.LENGTH_SHORT).show();
String nameContact = c
.getString(c
.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME));
// contact number = cNumber
//contact name = nameContact
}
}
I am using ContactsContract api intent for showing all contacts from an activity. This intent returns an id of the contact. I need to get the postal address of this contact.
Here is the code which i am using for showing contacts:
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType(ContactsContract.Contacts.CONTENT_TYPE);
startActivityForResult(intent, PICK_CONTACT);
I am getting the result in the onActivityResult function.
Please help, how do i do this.
in your onActivityResult, first get the contact ID and query the ContactsContract.Data for the relevant data:
// get the contact ID
Cursor cursor = getContentResolver().query(data.getData(), null, null, null, null);
cursor.moveToFirst();
long id = cursor.getLong(cursor.getColumnIndex(ContactsContract.Contacts._ID));
cursor.close();
// get the data package containg the postal information for the contact
cursor = getContentResolver().query(ContactsContract.Data.CONTENT_URI,
new String[]{ StructuredPostal.STREET,
StructuredPostal.CITY,
// add more coluns from StructuredPostal if you need them
StructuredPostal.POSTCODE},
ContactsContract.Data.CONTACT_ID + "=? AND " +
StructuredPostal.MIMETYPE + "=?",
new String[]{String.valueOf(id), StructuredPostal.CONTENT_ITEM_TYPE},
null);
Street = cursor.getString(cursor.getColumnIndex(StructuredPostal.STREET));
Postcode = cursor.getString(cursor.getColumnIndex(StructuredPostal.POSTCODE));
City = cursor.getString(cursor.getColumnIndex(StructuredPostal.CITY)));
// etc.
Can be done this way too (little lesser code)
Send the Intent this way:
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType(ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_TYPE); startActivityForResult(intent, PICK_CONTACT);
In the onActivityResult method
Uri contactData = data.getData();
Cursor c = managedQuery(contactData, null, null, null, null);
if (c.moveToFirst()) {
String address = c.getString(c.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.StructuredPostal.FORMATTED_ADDRESS));
}
Kiran's answer probably works fine for Android 2.0 and above, but when you want it to be compatible for Android 1.6 and below you need to adapt the code a bit:
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType(Contacts.ContactMethods.CONTENT_POSTAL_TYPE);
startActivityForResult(intent, PICK_CONTACT);
And in onActivtyResult method:
Uri contactData = data.getData();
Cursor c = managedQuery(contactData, null, null, null, null);
if (c.moveToFirst()) {
String address = c.getString(c.getColumnIndexOrThrow(Contacts.ContactMethodsColumns.DATA));
}
Where PICK_CONTACT is a int of your own choice. (Right?)
This will return all the addresses from the contact Postal Address.
Uri uri = ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_URI;
String sortOrder = StructuredPostal.DISPLAY_NAME + " COLLATE LOCALIZED ASC";
Cursor cr = getContentResolver().query(uri,null, null, null, sortOrder);
while(cr.moveToNext())
{
String Street = cr.getString(cr.getColumnIndex(StructuredPostal.STREET));
String Postcode = cr.getString(cr.getColumnIndex(StructuredPostal.POSTCODE));
String City = cr.getString(cr.getColumnIndex(StructuredPostal.CITY));
Toast.makeText(this,"Address is : " +City+","+Street ,Toast.LENGTH_SHORT).show();
}
cr.close();
I'm trying to call the contact picker, get the persons name, phone and e-mail into strings and send them to another activity using an intent. So far this works:
Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(intent, 1);
// ...
#Override
public void onActivityResult(int reqCode, int resultCode, Intent data) {
super.onActivityResult(reqCode, resultCode, data);
if (resultCode == Activity.RESULT_OK) {
Uri contactData = data.getData();
Cursor c = managedQuery(contactData, null, null, null, null);
if (c.moveToFirst()) {
String name = c.getString(c.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME));
Intent intent = new Intent(CurrentActivity.this, NewActivity.class);
intent.putExtra("name", name);
startActivityForResult(intent, 0);
}
}
}
But if i add in:
String number = c.getString(c.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.NUMBER));
it force closes
Maybe theres another way to get their number?
Phone Numbers
Phone numbers are stored in their own table and need to be queried separately. To query the phone number table use the URI stored in the SDK variable ContactsContract.CommonDataKinds.Phone.CONTENT_URI. Use a WHERE conditional to get the phone numbers for the specified contact.
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);
while (pCur.moveToNext()) {
// Do something with phones
}
pCur.close();
}
Perform a second query against the Android contacts SQLite database. The phone numbers are queried against the URI stored in ContactsContract.CommonDataKinds.Phone.CONTENT_URI. The contact ID is stored in the phone table as ContactsContract.CommonDataKinds.Phone.CONTACT_ID and the WHERE clause is used to limit the data returned.
Email Addresses
Querying email addresses is similar to phone numbers. A query must be performed to get email addresses from the database. Query the URI stored in ContactsContract.CommonDataKinds.Email.CONTENT_URI to query the email address table.
Cursor emailCur = cr.query(
ContactsContract.CommonDataKinds.Email.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?",
new String[]{id}, null);
while (emailCur.moveToNext()) {
// This would allow you get several email addresses
// if the email addresses were stored in an array
String email = emailCur.getString(
emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
String emailType = emailCur.getString(
emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.TYPE));
}
emailCur.close();
As with the phone query the field names for the email table are also stored under ContactsContract.CommonDataKinds. The email query is performed on the URI in ContactsContract.CommonDataKinds.Email.CONTENT_URI and the WHERE clause has to match the ContactsContract.CommonDataKinds.Email.CONTACT_ID field. Since multiple email addresses can be stored loop through the records returned in the Cursor.
More tutorials here
This method requires Android API version 5 or higher.
Building on the accepted answer, if you want to jump straight to the desired email address and not require the contacts permission use something like this:
private static final int REQUEST_CODE_EMAIL = 1;
void startSelectingEmail() {
Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.CommonDataKinds.Email.CONTENT_URI);
startActivityForResult(intent, REQUEST_CODE_EMAIL);
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK && requestCode == REQUEST_CODE_EMAIL) {
Uri emailUri = data.getData();
Cursor emailCursor = getContext().getContentResolver().query(emailUri, null, null, null, null);
if (emailCursor != null) {
if (emailCursor.moveToFirst()) {
String email = emailCursor.getString(
emailCursor.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Email.DATA));
String emailType = emailCursor.getString(
emailCursor.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Email.TYPE));
Log.d(TAG, "Email: " + emailType + " " + email);
}
emailCursor.close();
}
}
}
This doesn't require the contacts permission to read the email address like the double query methods above. It also makes it so that you do not need to write UI for the user to select the appropriate email address for contacts with multiple emails, the user selects a specific email in the Contacts app so you only get one result.
The cursor comes back with quite a few columns in addition to just email address like display name, though that has only been verified on a Nexus 5 running Android M.