Retrieveing saved address from phonebook by using a pick intent - android

I want to pick a contact from phone book using intent and selected contact should also contain respective city and state (if saved by user).
Here is what I am trying for past few days:
private void accessContacts() {
Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.CommonDataKinds.Phone.CONTENT_URI);
intent.setDataAndType(ContactsContract.Contacts.CONTENT_URI,
ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE);
startActivityForResult(intent, PICK_CONTACT);
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case PICK_CONTACT:
if (resultCode == Activity.RESULT_OK) {
Uri contactData = data.getData();
String[] projection = {
ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Phone.NUMBER
};
String name = null;
String phoneNumber = null;
String city = null;
String state = null;
Cursor c =
getActivity().getContentResolver().query(contactData, null, null, null, null);
if (c != null) {
if (c.moveToFirst()) {
int nameIdx = c.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
int phoneNumberIdx = c.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
name = c.getString(nameIdx);
phoneNumber = c.getString(phoneNumberIdx);
String id = c.getString(c.getColumnIndex(ContactsContract.PhoneLookup._ID));
String addrWhere = ContactsContract.Data.CONTACT_ID + " = ? AND "
+ ContactsContract.Data.MIMETYPE + " = ?";
String[] addrWhereParams = new String[]{id,
ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE};
Cursor addrCur = getActivity().getContentResolver()
.query(ContactsContract.Data.CONTENT_URI,
null, addrWhere, addrWhereParams, null);
if (addrCur.moveToFirst()) {
city = addrCur.getString(
addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.CITY));
state = addrCur.getString(
addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.REGION));
}
addrCur.close();
}
c.close();
}
if (name == null) {
name = "No Name";
}
if (TextUtils.isEmpty(phoneNumber) || phoneNumber.length() < 10) {
handleAddConnectionError(getContext().getString(R.string.invalid_phone_number));
return;
}
Phonenumber.PhoneNumber number =
Utils.validateMobileNumber2(phoneNumber, Device.getCountryCode(getActivity()));
if (number == null) {
handleAddConnectionError(getContext().getString(R.string.invalid_phone_number));
return;
}
this.moveToCategoryPage(name, String.valueOf(number.getNationalNumber()),
Utils.getCountryCodeWithPlusSign(number), false, null,
city, state);
}
break;
}
}
While removing/changing this:
intent.setDataAndType(ContactsContract.Contacts.CONTENT_URI,
ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE);
it throws exception.
How can I get saved address from pick intent?

There's one big bug that I can see here:
String id = c.getString(c.getColumnIndex(ContactsContract.PhoneLookup._ID));
You shouldn't use PhoneLookup.XXX columns when querying over Phone.CONTENT_URI, this code actually returns the Phone._ID column instead (because both have the same const string name), which is NOT a contactID, it is a specific phone's ID (or a Data._ID).
You also can't access columns from a cursor that you didn't specify in your projection.
So change you projection to:
String[] projection = {
ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Phone.NUMBER,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID,
};
and replace that line with:
Long contactId = c.getLong(2);
Another thing you can do is to simplify your second query by querying over StructuredPostal.CONTENT_URI like this:
String addrWhere = StructuredPostal.CONTACT_ID + " = " + contactId;
Cursor addrCur = getActivity().getContentResolver().query(StructuredPostal.CONTENT_URI, null, addrWhere, null, null);
Please note that unlike your first query for the Phone.NUMBER info, you app needs READ_CONTACTS permission to do the second query for the address, this is because you ask for a phone-picker intent which grants your app a temp. permission to access the picked phone number only from the contacts API, anything other then that requires permission.

Related

Pick email, name, and phone number from contact picker intent

how can I get the email address correctly? Here's what I've done so far.
#android.webkit.JavascriptInterface
public void chooseContact(){
Intent pickContactIntent = new Intent(Intent.ACTION_PICK);
pickContactIntent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE);
startActivityForResult(pickContactIntent, CONTACT_PICKER_RESULT);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data){
if (requestCode == CONTACT_PICKER_RESULT){
Uri contactUri = data.getData();
// Perform the query.
// We don't need a selection or sort order (there's only one result for the given URI)
Cursor cursor = getContentResolver().query(contactUri, null, null, null, null);
cursor.moveToFirst();
// Retrieve the phone number from the NUMBER column.
int column = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
String phoneNumber = cursor.getString(column);
if(phoneNumber != null)
phoneNumber = phoneNumber.trim();
if(phoneNumber == null || phoneNumber.equals("")) {
// This should never happen, but just in case we'll handle it
Log.e(TAG, "Phone number was null!");
Util.debugAsserts(false);
return;
}
// Retrieve the contact name.
column = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Identity.DISPLAY_NAME);
String name = cursor.getString(column);
// Retrieve the contact email address.
column = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA);
String email = cursor.getString(column);
cursor.close();
JSONObject contacts = new JSONObject();
String jsonString;
try {
contacts.put("name" , name);
contacts.put("email" , email);
contacts.put("phoneNumber" , phoneNumber);
jsonString = contacts.toString();
String encodedData = Base64.encodeToString(jsonString.getBytes(), Base64.DEFAULT);
String command = String.format("get_contact_details('%s');", encodedData);
eaWebView.submitJavascript(command);
} catch (JSONException e) {
throw new Error(e);
}
}
}
The above code get the name and phone number correctly but the email returns as phone number not the email address from the contact.
Thank you in advance!
The problem is that you're using a Phone-picker in your code, by setting setType(Phone.CONTENT_TYPE) you're telling the contacts app you're only interested in a phone number.
You need to switch to a Contact-picker, like so:
Intent intent = new Intent(Intent.ACTION_PICK, Contacts.CONTENT_URI);
Then you read the result like this:
Uri contactData = data.getData();
// get contact-ID and name
String[] projection = new String[] { Contacts._ID, Contacts.DISPLAY_NAME };
Cursor cursor = getContentResolver().query(contactUri, projection, null, null, null);
cursor.moveToFirst();
long id = cursor.getLong(0);
String name = cursor.getString(1);
Log.i("Picker", "got a contact: " + id + " - " + name);
cursor.close();
// get email and phone using the contact-ID
String[] projection2 = new String[] { Data.MIMETYPE, Data.DATA1 };
String selection2 = Data.CONTACT_ID + "=" + id + " AND " + Data.MIMETYPE + " IN ('" + Phone.CONTENT_ITEM_TYPE + "', '" + Email.CONTENT_ITEM_TYPE + "')";
Cursor cursor2 = getContentResolver().query(Data.CONTENT_URI, projection2, selection2, null, null);
while (cursor2 != null && cursor2.moveToNext()) {
String mimetype = cursor2.getString(0);
String data = cursor2.getString(1);
if (mimetype.equals(Phone.CONTENT_ITEM_TYPE)) {
Log.i("Picker", "got a phone: " + data);
} else {
Log.i("Picker", "got an email: " + data);
}
}
cursor2.close();
You Can Use Below Method To Fetch Contact Number, Email and Contact Name
public static void getContactDetails(Context context){
ArrayList<String> names = new ArrayList<String>();
ContentResolver cr = context.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));
Cursor cur1 = cr.query(
ContactsContract.CommonDataKinds.Email.CONTENT_URI, null,
ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?",
new String[]{id}, null);
while (cur1.moveToNext()) {
String name=cur1.getString(cur1.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
String number=cur1.getString(cur1.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
String email = cur1.getString(cur1.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
Log.e("Contact Details","\n Name is :"+name+" Email is: "+ email+ " Number is: "+number);
if(email!=null){
names.add(name);
}
}
cur1.close();
}
}
See Below Image For Output

Get the email address from a contact

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));
}
}
}

Android, cannot retrieve phone numbers using user id

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;
}

if i choose contact, i didn't get the contact that i mean

i know how to get all contact on Android, but if i choose the contact i can't get the contact what i want. example : i have 4 contact
joe have phone number 7889 987;
erick have phone number 8792 871;
nona phone number 3653 872 and 2345 907;
rina phone number 8796 235;
if i choose joe, i get nona's phone number= 2345 907
i don't know what is the problem on my application.
this is my code
public void tambahPenerima ( View v ) {
Intent i = new Intent(Intent.ACTION_PICK, Contacts.CONTENT_URI);
i.setType(ContactsContract.Contacts.CONTENT_TYPE);
startActivityForResult(i, PICK_CONTACT);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(resultCode == RESULT_OK) {
Cursor cursor = null;
String name = "";
String number = "";
cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);
while(cursor.moveToNext()) {
name = cursor.getString(cursor.getColumnIndex((ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME)));
number = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
}
if(cursor!=null) {
cursor.close();
}
Intent kirimPesan = new Intent();
kirimPesan.setClass(this, kirimPesan.class);
kirimPesan.putExtra("nama", name);
kirimPesan.putExtra("nomor", number);
kirimPesan.putExtra("chiper", chiper);
startActivity(kirimPesan);
}
}
Somebody please help me, I really need help.
Sorry for my poor english.
thanks ..
cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);
if(cursor != null && cursor.moveToFirst()) {
while(cursor.moveToNext()) {
name = cursor.getString(cursor.getColumnIndex((ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME)));
number = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
}
}
this may helps you to get specific contact Details
here name or number pass and get contact details wich you want
private String getContactName(Context context, String number) {
String name = null;
// 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));
Log.v(TAG, "Started uploadcontactphoto: Contact Found # " + number);
Log.v(TAG, "Started uploadcontactphoto: Contact name = " + name);
} else {
Log.v(TAG, "Contact Not Found # " + number);
}
cursor.close();
}
return name;
}

GetAndroid contact information (phone, name, etc...)

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();
}

Categories

Resources