Get the email address from a contact - android

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

Related

Retrieveing saved address from phonebook by using a pick intent

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.

Contacts aren't showing up on my edittext

So i've been trying to add contact to a edittext,i've used Onclickevent to invoke contacts and then once a contact has been selected,it should be written to edittext,but i'm not able to do that,below is my Onactivity result,
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
switch (requestCode) {
case CONTACT_PICKER_RESULT:
Cursor cursor = null;
String name = "";
try {
Uri result = data.getData();
//writeToFile("uri" +result);
String id = result.getLastPathSegment();
// query for name
cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "=?", new String[] { id },
null);
if (cursor != null && cursor.moveToFirst())
{
int phoneIdx = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DATA);
int nameIdx = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
writeToFile("ifcursor" +phoneIdx+nameIdx);
name = cursor.getString(nameIdx);
}
} catch (Exception e) {
//Log.e(DEBUG_TAG, "Failed to get name", e);
} finally {
if (cursor != null) {
cursor.close();
}
// phNo = (EditText) findViewById(R.id.phone_number);
phNo.setText(name);
if (name.length() == 0) {
Toast.makeText(getApplicationContext(),"Name not found for contact.",Toast.LENGTH_LONG).show();
}
}
break;
}
} else {
//Log.w(DEBUG_TAG, "Warning: activity result not ok");
}
}
any help would be deeply appreciated,it stucks on "name not found"
Try this one
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// Check which request it is that we're responding to
if (requestCode == PICK_CONTACT_REQUEST) {
// Make sure the request was successful
if (resultCode == RESULT_OK) {
// Get the URI that points to the selected contact
Uri contactUri = data.getData();
// We only need the NUMBER column, because there will be only one row in the result
String[] projection = {Phone.NUMBER};
// Perform the query on the contact to get the NUMBER column
// We don't need a selection or sort order (there's only one result for the given URI)
// CAUTION: The query() method should be called from a separate thread to avoid blocking
// your app's UI thread. (For simplicity of the sample, this code doesn't do that.)
// Consider using CursorLoader to perform the query.
Cursor cursor = getContentResolver()
.query(contactUri, projection, null, null, null);
cursor.moveToFirst();
// Retrieve the phone number from the NUMBER column
int column = cursor.getColumnIndex(Phone.DISPLAY_NAME);
String number = cursor.getString(column);
phNo.setText(number );
// Do something with the phone number...
}
}
}
If you need the user to pick a contact with a phone number, you should call the following intent:
Intent i = new Intent(Intent.ACTION_PICK, Phone.CONTENT_URI);
startActivityForResult(i, CONTACT_PICKER_RESULT);
Then you can handle the response in onActivityResult like so:
Uri result = data.getData();
// because we asked for a Phone.CONTENT_URI picker, we'll get a uri for a Phone._ID entry
String id = result.getLastPathSegment();
String[] projection = new String[] { Phone.DISPLAY_NAME, Phone.NUMBER };
String selection = Phone._ID + "=" + id;
Cursor cursor = getContentResolver().query(Phone.CONTENT_URI, projection, selection,null, null);
if (cursor != null && cursor.moveToFirst()) {
String name = cursor.getString(0);
String number = cursor.getString(1);
Log.d("MyActivity", "got name=" + name + ", phone=" + number);
}
if (cursor != null) {
cursor.close();
}

Android - back from Contacts intent without selecting one

In my android application I have a list of people (name and telephone number) and I let the user add a person to the app's database by importing one from their contacts list. Everything works fine, but a user could click the button to import a new person and then go back, so the Contacts intent will be closed without actually selecting a contact.
So I need to know where to put an if, or something else, in my code in order to see whether the user selected a contact or not.
private void pickContact() {
Intent pickContactIntent = new Intent(Intent.ACTION_PICK, Uri.parse("content://contacts"));
pickContactIntent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE); // Show user only contacts w/ phone numbers
startActivityForResult(pickContactIntent, PICK_CONTACT_REQUEST);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// Check which request it is that we're responding to
if (requestCode == PICK_CONTACT_REQUEST) {
// Make sure the request was successful
if (resultCode == RESULT_OK) {
// Get the URI that points to the selected contact
Uri contactUri = data.getData();
String[] projection = {ContactsContract.CommonDataKinds.Phone.NUMBER};
Cursor cursor = getContentResolver().query(contactUri, null, null, null, null);
cursor.moveToFirst();
// Retrieve the name from the DISPLAY_NAME column
String name = cursor.getString(cursor.getColumnIndex(ontactsContract.Contacts.DISPLAY_NAME));
// Retrieve the phone number from the NUMBER column
int column = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
String number = cursor.getString(column);
// update database
dbManager.addPerson(name, number, true);
}
}
}
So how can I check if the user actually selected a contact or went back?
So I need to know where to put an if, or something else, in my code in order to see whether the user selected a contact or not.
You already have the code for this: if (resultCode == RESULT_OK). If the user presses BACK, you will not get RESULT_OK.
You are using an startActivityForResult which will return you back to the same activity once your
Intent.ACTION_PICK, Uri.parse("content://contacts")
jobs gets over that is the user has selected a contact.
And in order to get the name , number or any other details of the contact selected you do it in your onActivityResult callback like follows :
#Override
public void onActivityResult(int reqCode, int resultCode, Intent data) {
super.onActivityResult(reqCode, resultCode, data);
if(reqCode == PICK_CONTACT_REQUEST) {
String cNumber = null;
String cName = null;
if (resultCode == Activity.RESULT_OK) {
Uri contactData = data.getData();
Cursor cursor = getContentResolver().query(contactData, null, null, null, null);
String hasPhone = null;
String contactId = null;
if (cursor != null) {
cursor.moveToFirst();
try {
hasPhone = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.Contacts.HAS_PHONE_NUMBER));
contactId = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.Contacts._ID));
} catch (IllegalArgumentException e) {
Log.e(TAG, "Exception Message : " + e.getMessage());
DisplayMessage.error("Sorry can't access contacts. Check permissions settings.", this);
return;
}
if (hasPhone != null && hasPhone.equals("1")) {
Cursor phones = getContentResolver().query
(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID
+ " = " + contactId, null, null);
if (phones != null) {
while (phones.moveToNext()) {
String phoneNumber = phones.getString(phones.getColumnIndex
(ContactsContract.CommonDataKinds.Phone.NUMBER)).replaceAll("[-() ]", "");
cNumber = phoneNumber;
}
phones.close();
}
cName = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
Log.d(TAG, "Contact name is:" + cName);
}
cursor.close();
}
}
}
}

How to get email and last name from contacts?

I have used an intent of contact picker. I am getting the data of contact through the intent. By that that intent I am fetching the name, number of the contact person.
When I tried to fetch an email id of contact it only shows the number of the contact instead of an email id. Also for last name of contact it shows always null though the last is been set to the contact.
Code:
private void contactPicked(Intent data) {
Cursor cursor = null;
try {
String phoneNo = null ;
String name = null;
// getData() method will have the Content Uri of the selected contact
Uri uri = data.getData();
//Query the content uri
cursor = getContentResolver().query(uri, null, null, null, null);
cursor.moveToFirst();
// column index of the phone number
int phoneIndex =cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
// column index of the contact name
int nameIndex =cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
int contactIdIndex =cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.CONTACT_ID);
int emailIndex = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA);
int lastNameIndex = cursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME);
mOrganizerPhone = cursor.getString(phoneIndex);
mOraganizerName = cursor.getString(nameIndex);
mOrganizersId = cursor.getString(contactIdIndex);
mOrganizersEmail = cursor.getString(emailIndex);
mOrganizersLastName = cursor.getString(lastNameIndex);
Toast.makeText(PlanEventActivity.this,name+" "+phoneNo,Toast.LENGTH_SHORT).show();
} catch (Exception e) {
e.printStackTrace();
}
}
Thank you..
With "id", you can get the email address like this, this is an example:
public class CustomerForm extends Activity {
private final static int CONTACT_PICKER = 1;
private EditText txtMailContacto;
private EditText txtNombreContacto;
private EditText txtTelefono;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_customer_form);
txtMailContacto = (EditText) findViewById(R.id.txtMailContacto);
txtTelefono = (EditText) findViewById(R.id.txtTelefono);
txtNombreContacto = (EditText) findViewById(R.id.txtNombreContacto);
}
public void pickContact(View v)
{
Intent contactPickerIntent =
new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(contactPickerIntent, CONTACT_PICKER);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// check whether the result is ok
if (resultCode == RESULT_OK) {
// Check for the request code, we might be using multiple startActivityForReslut
switch (requestCode) {
case CONTACT_PICKER:
contactPicked(data);
break;
}
} else {
Log.e("MainActivity", "Failed to pick contact");
}
}
private void contactPicked(Intent data) {
ContentResolver cr = getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
cur.moveToFirst();
try {
// getData() method will have the Content Uri of the selected contact
Uri uri = data.getData();
//Query the content uri
cur = getContentResolver().query(uri, null, null, null, null);
cur.moveToFirst();
// column index of the contact ID
String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
// column index of the contact name
String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
txtNombreContacto.setText(name); //print data
// column index of the phone number
Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?",
new String[]{id}, null);
while (pCur.moveToNext()) {
String phone = pCur.getString(
pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
txtTelefono.setText(phone); //print data
}
pCur.close();
// column index of the email
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.ADDRESS));
txtMailContacto.setText(email); //print data
}
emailCur.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}

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

Categories

Resources