Get a phone number from contacts - android

I have a button that I click and that shows contacts. It displays the name, but not the number of a selected contact...
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
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));
contactName.setText(name);
Something here !!!!!!!!!!!!!!!!!!!!!!!!
contactName.setText(number);
}
}
}
}
It String the NAME but how can I get the phone number?
#Override
public void onActivityResult(int reqCode, int resultCode, Intent data) {
super.onActivityResult(reqCode, resultCode, data);
if (resultCode == Activity.RESULT_OK) {
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 number=phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
contactName.setText(name);
contactNumber.setText(number);
}
}
With this code it opens my contacts. I touch one, but it always display the LAST name and number that I have added to contacts.
For example, I touch Adrian (22222222) and it shows Paul (33333333) which is the last person that I have added.
What should I do?

String phoneNumber = c.getString(c.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));

Related

Picking phone or email from contact list in android

In an android project i had this intent to pick a phone number from my contact list
btnContacts.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Open Contacts
Intent intent= new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
intent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE);
startActivityForResult(intent, PICK_CONTACT);
}
});
and this to get the result
#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 contactUri = data.getData();
String[] projection = new String[] { ContactsContract.CommonDataKinds.Phone.NUMBER, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME };
Cursor cursor = getActivity().getContentResolver().query(contactUri, projection, null, null, null);
if (cursor != null && cursor.moveToFirst()) {
insertedPhoneNumber = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
String name = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
formatContact(insertedPhoneNumber, name);
}
}
break;
}
}
I would like to know if there is a way to do the same operation but if the contact dindt had phone number i would retrive the email instead.
I would like to know if there is a way to do the same operation but if the contact dindt had phone number i would retrive the email instead.
No. You ask for phone numbers you get phone numbers. If you need additional logic or ifs, then you need to code it yourself.

Get phone number from Contacts - Crashes in my onActivityResult method when using ContactsContract

I created an onActivityResult method. I am in the contact list, Upon clicking I should get the name and the phone number of the contact, but it crashes.
#Override
protected void onActivityResult(int reqCode, int resultCode, Intent data) {
//TODO Auto-generated method stub
super.onActivityResult(reqCode, resultCode, data);
if (reqCode == PICK_CONTACT) {
if(resultCode == ActionBarActivity.RESULT_OK){
Uri contactData = data.getData();
Cursor c = getContentResolver().query(contactData, null, null, null, null);
if(c.moveToFirst()) {
Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(intent, PICK_CONTACT);
String name = c.getString(c.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME));
String phone = c.getString(c.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.NUMBER));
Toast.makeText(this, "You've picked" + name, Toast.LENGTH_SHORT).show();
}
}
}
}
Please note, if I remove the line
String phone = c.getString(c.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.NUMBER));
it doesn't crash. (But I don't get the phone number.

Application is restarting while clicking on menu item

I am using contact picker to chose a phone number from phone.But after using the contact picker once I click on any other button or menu, the app gets restarted.
Below is my code :- Here mobilText is the textbox where I am setting the phone number which I get from contact picker.
contact.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(Intent.ACTION_PICK, ContactsContract.CommonDataKinds.Phone.CONTENT_URI);
startActivityForResult(i, 1);
}
});
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1 && resultCode == RESULT_OK) {
Uri contactUri = data.getData();
Cursor cursor = getContentResolver().query(contactUri, null, null, null, null);
cursor.moveToFirst();
String cNumber = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
mobilText.setText(cNumber+"");
}
}

get 0 when selecting contact from contact list

I have made a application in which i want to user to select the contact from the contact list and display it in the EditText.But when ever my user selects a contact my edit text value shows only 0.Why this so???
Code
case R.id.bt_contacts:
intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(intent, 1);
break;
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == getActivity().RESULT_OK) {
Uri contactData = data.getData();
Cursor c = getActivity().getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);
if (c.moveToFirst()) {
contactNumber = c.getInt(c.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.NUMBER));
}
etMobile.setText("" + contactNumber);
}
}
I think you should get string value from cursor
contactNumber = c.getString(c.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.NUMBER));

search contacts and get the contact number from phone contacts in android

I am working with an app, in this I have to search contacts when the button click and select one of the contact from phone contacts. Finally I want to set it to edittext.I can able to search the contacts from phone contacts but I am unable to get contact from phone contacts.
activity:
searchBtn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE);
startActivityForResult(intent,1);
}
});
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if(requestCode==RESULT_OK){
Uri contactData = data.getData();
Cursor cursor = managedQuery(contactData, null, null, null, null);
cursor.moveToFirst();
String number = cursor.getString(cursor.getColumnIndexOrThrow
(ContactsContract.CommonDataKinds.Phone.NUMBER));
//contactName.setText(name);
ephoneNumber.setText(number);
//contactEmail.setText(email);
}
}
First set Intent for StartActivityForResult Like this
private void pickContact() {
Intent pickContactIntent = new Intent(Intent.ACTION_PICK, Uri.parse("content://contacts"));
pickContactIntent.setType(Phone.CONTENT_TYPE); // Show user only contacts w/ phone numbers
startActivityForResult(pickContactIntent, PICK_CONTACT_REQUEST);
}
then deal with result what intent carry from native contact application
#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.NUMBER);
String number = cursor.getString(column);
// Do something with the phone number...
}
}
}
see another reference links
Link 1
Link 2
Bharat,
You need permission like -
android:name="android.permission.READ_CONTACTS"/>
Then, Calling the Contact Picker
Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
Then,
#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 name = c.getString(c.getColumnIndexOrThrow(People.NAME));
// TODO Whatever you want to do with the selected contact name.
}
}
break;
}
}
OR, In other way You can use
Cursor cursor = null;
try {
cursor = context.getContentResolver().query(Phone.CONTENT_URI, null, null, null, null);
int contactIdIdx = cursor.getColumnIndex(Phone._ID);
int nameIdx = cursor.getColumnIndex(Phone.DISPLAY_NAME);
int phoneNumberIdx = cursor.getColumnIndex(Phone.NUMBER);
int photoIdIdx = cursor.getColumnIndex(Phone.PHOTO_ID);
cursor.moveToFirst();
do {
String idContact = cursor.getString(contactIdIdx);
String name = cursor.getString(nameIdx);
String phoneNumber = cursor.getString(phoneNumberIdx);
//...
} while (cursor.moveToNext());
} catch (Exception e) {
e.printStackTrace();
} finally {
if (cursor != null) {
cursor.close();
}
}
Above solution is simple and working for contact number fetching.
try this
CursorLoader cursorLoader = new CursorLoader(mActivity,
CallLog.Calls.CONTENT_URI, null, null, null, null);
managedCursor = cursorLoader.loadInBackground();
int number = managedCursor.getColumnIndex(CallLog.Calls.NUMBER);
int name = managedCursor.getColumnIndex(CallLog.Calls.CACHED_NAME);
String conTactnumber=managedCursor.getString(number);
String contactname=managedCursor.getString(name);
set permission
<uses-permission android:name="android.permission.READ_CONTACTS" >
</uses-permission>

Categories

Resources