I am trying to get name and email-id from Android inbuilt phone book into my page, I am able to get name, contact ID, phone number. but I am unable to get email ID from the Android phone book.
Code is:
public static final int PICK_CONTACT = 1;
#Override
button.setOnClickListener(new OnClickListener() {
public void onClick(View _view) {
Intent intent = new Intent(Intent.ACTION_PICK,ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(intent, PICK_CONTACT);
}
});
}
#Override
public void onActivityResult(int reqCode, int resCode, Intent data)
{
super.onActivityResult(reqCode, resCode, data);
switch(reqCode) {
case (PICK_CONTACT) : {
if (resCode == Activity.RESULT_OK) {
Uri contactData = data.getData();
Cursor c = managedQuery(contactData, null, null, null, null);
c.moveToFirst();
String name = c.getString(c.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME));
String name1 = c.getString(c.getColumnIndexOrThrow(ContactsContract.Contacts.HAS_PHONE_NUMBER));
String ContactID = c.getString(c.getColumnIndex(ContactsContract.Contacts._ID));
if(Integer.parseInt(name1) == 1){
Cursor emails = getContentResolver().query(ContactsContract.CommonDataKinds.Email.CONTENT_URI,null,
ContactsContract.CommonDataKinds.Email.CONTACT_ID+ " = " + ContactID, null, null);
TextView tv = (TextView)findViewById(R.id.selected_contact_textview);
TextView tv1 = (TextView)findViewById(R.id.selected_email_textview);
tv.setText(name);
tv1.setText(ContactID);
}
}
break;
}
}
Here I am able to get name and contact ID of a selected person from the phonebook. Now I want to get name and email ID of a selected person from phone book.
How can I achieve this?
You can get the email address of the contact directly using the contact id as follows :
String selection = ContactsContract.CommonDataKinds.Email.CONTACT_ID + " =? ";
String[] selectionArgs = new String[]{aContactId};
Cursor emailCursor = aContext.getContentResolver().query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, null, selection, selectionArgs, null);
while (!emailCursor.isAfterLast())
{
emailString = emailCursor.getString(emailCursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
// do something with email string
emailCursor.moveToNext();
}
emailCursor.close ();
Related
I'm trying to get the Email address of a user I select in the contact list. I am able to get the ID, phone and name of the contact and by using the ID, I am trying to get the Email address using a new Cursor but am unable to get the details from the contact.
Below is the code I am using:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent aData) {
super.onActivityResult(requestCode, resultCode, aData);
if (requestCode == PICK_CONTACT && resultCode == Activity.RESULT_OK && aData != null) {
ContentResolver cr = getContentResolver();
String phoneNo = "";
String name = "";
String id = "";
String email = "";
Uri uri = aData.getData();
if (uri != null) {
Cursor cursor = cr.query(uri, null, null, null, null);
if (cursor != null) {
if (cursor.moveToFirst()) {
id = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
int phoneIndex = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
int nameIndex = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
phoneNo = cursor.getString(phoneIndex);
name = cursor.getString(nameIndex);
}
cursor.close();
} else {
showDialog(getString(R.string.lbl_error_message_contact));
}
} else {
showDialog(getString(R.string.lbl_error_message_contact));
}
Cursor emailCur = cr.query(
ContactsContract.CommonDataKinds.Email.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?",
new String[]{id}, null);
while (emailCur.moveToNext()) { // when it gets here, it just skips the while loop and jumps down to the to close the emailCur
// This would allow you get several email addresses
// if the email addresses were stored in an array
email = emailCur.getString(
emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.ADDRESS));
}
emailCur.close();
mMedia.setLendersName(name);
mMedia.setPhoneNumber(phoneNo);
mMedia.setEmail(email);
}
}
Could it be that the ID may not be correct?
EDIT
One thing that is really weird is that when I select a contact and return back to the screen. The ID is very different to the ID I get when I run #Android Team's code which returns all the contacts with email addresses. What could be the reason behind this?
My thinking when I was trying to get the email address was that the ID's wont be different, but it seems it is the case.
when you getting device contact information you can add permission in manifest file..
<uses-permission android:name="android.permission.READ_CONTACTS"/>
then after used below method to get all the email id which device store in contact..
/**
* this method read device in contact name and email.
*/
public void getContact() {
Cursor cur = getActivity().getContentResolver().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));
Cursor cur1 = getActivity().getContentResolver().query(
ContactsContract.CommonDataKinds.Email.CONTENT_URI, null,
ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?",
new String[]{id}, null);
while (cur1.moveToNext()) {
//to get the contact names
String name = cur1.getString(cur1.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
Log.e("Name :", name);
String email = cur1.getString(cur1.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
Log.e("Email", email);
}
cur1.close();
}
}
}
check in log cat show all email id to be added in device.
I hope you know the Permission part and all, just add this code once you are allowed permission by user, and populate the email string in Edittext use it wherever you want
Account[] accounts = AccountManager.get(activityContext).getAccountsByType("com.google");
for (Account account : accounts) {
// if (emailPattern.matcher(account.name).matches()) {
String possibleEmail = account.name;
}
You might have to use this piece of code in activity to Populate the data, just an Add On
runOnUiThread(new Runnable() {
public void run() {
regEmail.setText(emailId);
}
});
I don't think some of you understood what I was trying to say, but this is the solution I found to get the Contact details of a selected contact by starting an intent to get the details from the Systems contact list.
Start the intent with the following intent to get the Name and Phone number:
Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.CommonDataKinds.Phone.CONTENT_URI);
startActivityForResult(intent, PICK_CONTACT);
Once the contact is selected, it comes back into onActivityResult. Here is an example of getting the Name and Phone number from the response.
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent aData) {
super.onActivityResult(requestCode, resultCode, aData);
if (requestCode == PICK_CONTACT && resultCode == Activity.RESULT_OK && aData != null) {
final String[] projection = new String[]{ContactsContract.CommonDataKinds.Email.DATA, ContactsContract.CommonDataKinds.Email.TYPE};
ContentResolver cr = getContentResolver();
String phoneNo;
String name;
String lookUpKey = "";
String email;
Uri uri = aData.getData();
if (uri != null) {
// ****************************First we want to get the Lookup Key, Name and Phone number****************************
Cursor cursor = cr.query(uri, null, null, null, null);
if (cursor != null) {
if (cursor.moveToFirst()) {
lookUpKey = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY));
int phoneIndex = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
int nameIndex = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
phoneNo = cursor.getString(phoneIndex);
name = cursor.getString(nameIndex);
mMedia.setLendersName(name);
mMedia.setPhoneNumber(phoneNo);
}
// ****************************Now we want to get the email address, if any from the contact****************************
// At this point, we use the Lookup key of the contact above, to get the email address (If any)
if (!TextUtils.isEmpty(lookUpKey)) {
//Do a query with the Lookup key to get the email details (Could possibly get more data as well, but I just needed the email address
cursor = cr.query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, projection, ContactsContract.Data.LOOKUP_KEY + "=?", new String[]{lookUpKey}, null);
if (cursor != null) {
while (cursor.moveToNext()) {
// Get the index of the column for the email address here
final int contactEmailColumnIndex = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA);
while (!cursor.isAfterLast()) {
// Here is where we get the email address itself
email = cursor.getString(contactEmailColumnIndex);
// Set it where ever you need it.
mMedia.setEmail(email);
cursor.moveToNext();
}
}
// Dont forget to close the cursor once you done with it
cursor.close();
}
}
} else {
showDialog(getString(R.string.lbl_error_message_contact));
}
} else {
showDialog(getString(R.string.lbl_error_message_contact));
}
}
}
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.
I'm trying to retrieve the phone number of a user after selecting him in a picker activity.
This is the code to start the Activity:
Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.CommonDataKinds.Phone.CONTENT_URI);
startActivityForResult(intent, Constants.COMMUNICATION_CONFIG_RESULT);
This is the code to retrive user name, id and phone number, it's used in a onActivityResult:
Uri contactData = data.getData();
ContentResolver cr = getContentResolver();
Cursor cursor = cr.query(contactData, null, null, null, null);
if (cursor.moveToFirst()) {
String contactId =
cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone._ID));
String name = cursor.getString(
cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
String hasPhone =
cursor.getString(cursor.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 +" = "+ contactId,null, null);
phones.moveToFirst();
String contactNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
Log.i("phoneNUmber", "The phone number is "+ contactNumber);
}
}
I get always the user name and id, and the user has always the phone number, but the cursor phone is always empty, regardless of the user selected.
Why can't I get the phone numbers?
Found out the problem. It must be:
String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.CONTACT_ID));
if then I query the phone number via
Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ contactId,null, null);
The problem was that I selected different fields for the query.
// try this way,hope this will help you...
Intent i = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(i, 1);
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
String phoneNumber = getPhoneNumber(data.getData());
if (phoneNumber.trim().length() > 0) {
// use this phoneNumber
} else {
ting("Phone Number Not Founded ...");
}
}
}
private String getPhoneNumber(Uri contactInfo) {
String phoneNumbers = "";
Cursor cursor = getContentResolver().query(contactInfo, null, null, null, null);
while (cursor.moveToNext()) {
String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
String hashPhone = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
if ("1".equalsIgnoreCase(hashPhone)) {
Cursor contactCursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + contactId, null, null);
while (contactCursor.moveToNext()) {
int type = contactCursor.getInt(contactCursor.getColumnIndex(Phone.TYPE));
if (type == Phone.TYPE_MOBILE) {
int colIdx = contactCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
phoneNumbers = phoneNumbers.concat(contactCursor.getString(colIdx));
break;
}
}
contactCursor.close();
}
}
cursor.close();
return phoneNumbers;
}
I know you might think this subject has been treated several times but this is different!
My app is supposed to get contact information (name, number) from a picked contact but I only get the name and I can't get the number.
#Override
public void onClick(View v) {
// Opening Contacts Window as a Window
Intent contactPickerIntent = new Intent(Intent.ACTION_PICK,
ContactsContract.Contacts.CONTENT_URI);
// calling OnActivityResult with intent And Some contact for Identifie
startActivityForResult(contactPickerIntent, PICK);
}
#Override
public void onActivityResult(int reqCode, int resultCode, Intent data) {
super.onActivityResult(reqCode, resultCode, data);
switch (reqCode) {
case (PICK) :
if (resultCode == Activity.RESULT_OK) {
Uri contactData = data.getData();
Cursor c = managedQuery(contactData, null, null, null, null);
if (c.moveToFirst()) {
int indexName = c.getColumnIndex(
ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
int indexNumber = c.getColumnIndex(
ContactsContract.CommonDataKinds.Phone.NUMBER);
nom = c.getString(indexName);
numero = c.getString(indexNumber);
//Visual confirm
Toast.makeText(this, "Contact " + nom +" enregistré!",
Toast.LENGTH_LONG).show();
//Save in prefs:
SharedPreferences manager =
PreferenceManager.getDefaultSharedPreferences(this);
Editor editor = manager.edit();
editor.putString("num", numero);
editor.putString("nom", nom);
editor.commit();
The name is correct but the number causes a force close.
But if I replace it with the following there is no longer a force close, but the number is still incorrect (0 or 1).
int indexNumber = c.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)+1;
What should I do?
private void getDetails(){
Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
ContentResolver cr = getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,null, null, null, null);
String[] projection = new String[] {ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Phone.NUMBER };
Cursor names = getContentResolver().query(uri, projection, null, null, null);
int indexName = names.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
int indexNumber = names.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
names.moveToFirst();
do {
String name = names.getString(indexName);
Log.e("Name new:", name);
String number = names.getString(indexNumber);
Log.e("Number new:","::"+number);
} while (names.moveToNext());
}
The above retrun all the name and number from from your contact database..
if you even need email id you add these lines also:::
while (cur.moveToNext()) {
String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
Cursor email = cr.query(
ContactsContract.CommonDataKinds.Email.CONTENT_URI, null,
ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?",
new String[]{id}, null);
while (email.moveToNext()) {
//to get the contact names
// if the email addresses were stored in an array
String emailid = email.getString(email.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
Log.e("Email id ::", emailid);
String emailType = email.getString(email.getColumnIndex(ContactsContract.CommonDataKinds.Email.TYPE));
Log.e("Email Type ::", emailType);
}
email.close();
}
I tried so much tutorials, and read a lot here at SO, but i cant solve my problem:
When a button is clicked, the user can choose the mobile number of a contact. Actual I can get the name of the selected contact, but i can't find a way, to get/chose the mobile number..
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
/** Layouting */
this.mGetMobileNumberButton = (Button)findViewById(R.id.getMobileNumberButton);
this.mNameTextView = (TextView)findViewById(R.id.nameTextView);
this.mMobileNumberTextView = (TextView)findViewById(R.id.mobileNumberTextView);
/** onClick getContactInfos*/
this.mGetMobileNumberButton.setOnClickListener(new OnClickListener() {
public void onClick(View v){
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));
mNameTextView.setText(name);
}
}
}
Hope anyone can help :)
This will get the cursor holding base contact data, and will loop through the phone numbers the contact has, can have multiple.
Uri uri = data.getData();
Cursor cursor=ctx.getContentResolver().query(uri, null, null, null, null);
while (cursor.moveToNext()) {
String contactId = cursor.getString(cursor.getColumnIndex(
ContactsContract.Contacts._ID));
String hasPhone = cursor.getString(cursor.getColumnIndex(
ContactsContract.Contacts.HAS_PHONE_NUMBER));
if (Boolean.parseBoolean(hasPhone)) {
// You know have the number so now query it like this
Cursor phones = getContentResolver().query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ contactId,
null, null);
while (phones.moveToNext()) {
String phoneNumber = phones.getString(
phones.getColumnIndex(
ContactsContract.CommonDataKinds.Phone.NUMBER));
}
phones.close();
}
}
Is unless when you get all contact from phone and after that you test the result by one to one if has has number or not. You can set condition into query function.
Uri uri = data.getData();
Cursor cursor=ctx.getContentResolver().query(uri, null, ContactsContract.Contacts.HAS_PHONE_NUMBER + " = 1", null, null);
The rest of code will be the same without this test:
String hasPhone = cursor.getString(cursor.getColumnIndex(
ContactsContract.Contacts.HAS_PHONE_NUMBER));
if (Boolean.parseBoolean(hasPhone)) {