How can retrieve only name on contact in android - android

I use a below code, but I need only contact name and this code retrieve all data from contact, Facebook ,Nimbuzz and ....
Cursor cursor = getContentResolver().query(
ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
while (cursor.moveToNext()) {
//numberPhone = 0;
contactId = cursor.getString(cursor
.getColumnIndex(ContactsContract.Contacts._ID));
Uri contactUri = ContentUris.withAppendedId(Contacts.CONTENT_URI,
Long.parseLong(contactId));
Uri dataUri = Uri.withAppendedPath(contactUri,
Contacts.Data.CONTENT_DIRECTORY);
try {
Cursor nameCursor = getContentResolver()
.query(dataUri,null,Data.MIMETYPE + "=?",new String[] { StructuredName.CONTENT_ITEM_TYPE }, null);
int indexDisplayName = nameCursor.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME);
nameCursor.moveToFirst();
do {
String firstName = nameCursor.getString(nameCursor
.getColumnIndex(Data.DATA2));
String lastName = nameCursor.getString(nameCursor
.getColumnIndex(Data.DATA3));
Arrayename.add(firstName+" "+lastName);
//String display = nameCursor.getString(indexDisplayName);
//Log.d("display name", "display name is: " + display);
} while (nameCursor.moveToNext());
nameCursor.close();
} catch (Exception e) {
Toast.makeText(this, "Error Happend While Reading Names",
Toast.LENGTH_SHORT).show();
}
}
how can fetch only contact data?

If i'm getting you correctly below is what you need:
create a xml file for displaying results which will get:
create a button will will fire up the intent:
create a textview or edittext(i have used edit text)
Start an intent like this calling startActivityforresult(its args....) on button click:
final static int CONTACTS_RESULT = 3;
Intent intent = new Intent(Intent.ACTION_PICK, Contacts.CONTENT_URI);
startActivityForResult(intent, CONTACTS_RESULT);
Then handle the result on callback:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case CONTACTS_RESULT:
if (resultCode == RESULT_OK) {
Uri returnedData = data.getData();
id = returnedData.getLastPathSegment();
name = retriveName(data);
etName.setText(name);
}
break;
}
}
// This method is for getting the Name of the contact and setting its value to the returned value by calling setText() on the
private String retriveName(Intent data)
{
String name = null;
Uri returnedData = data.getData();
String id = returnedData.getLastPathSegment();
// Cursor for name
Cursor cursor = getContentResolver().query(returnedData, null, null,
null, null);
if (cursor.moveToFirst())
{
name = cursor.getString(cursor.getColumnIndex(Contacts.DISPLAY_NAME));
}
return name;
}
See if this works for you....
EDIT: Have you tried replacing Contacts.DISPLAY_NAME with these two:
Contacts.DISPLAY_NAME_PRIMARY
Contacts.DISPLAY_NAME_SOURCE

Related

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

get contact and email id from users phonebook

I want to get contact and email id from users phonebook and i want to populate them in textfields.
My code is..
Uri uri = Uri.parse("content://contacts");
Intent intent = new Intent(Intent.ACTION_PICK, uri);
intent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE);
Log.i("please","work");
startActivityForResult(intent, REQUEST_CODE);
and onActivityResult() method
#Override
protected void onActivityResult(int requestCode, int resultCode,
Intent intent) {
if (requestCode == REQUEST_CODE) {
if (resultCode == RESULT_OK) {
Uri uri = intent.getData();
String[] projection = { Phone.NUMBER, Phone.DISPLAY_NAME };
Cursor cursor = getContentResolver().query(uri, projection,
null, null, null);
cursor.moveToFirst();
int numberColumnIndex = cursor.getColumnIndex(Phone.NUMBER);
String number = cursor.getString(numberColumnIndex);
int nameColumnIndex = cursor.getColumnIndex(Phone.DISPLAY_NAME);
String name = cursor.getString(nameColumnIndex);
Log.d(TAG, "ZZZ number : " + number +" , name : "+name);
}
}
};
but this is not calling the onActivityResult function.
please help me with the issue?
get contact list like
private void insertDummyContact() {
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));
AppController.MyApplication.getInstance().trackScreenView("Contact List Invite");
Name.add(name);
Number.add(phoneNumber);
}
phones.close();
// Log.e("number", Name.toString());
// Log.e("name", Number.toString());
ListAdapter = new ContactListAdapter(ContactListInvite.this, Name, Number);
ContactList.setAdapter(ListAdapter);
ListAdapter.notifyDataSetChanged();
}

How to get phone number type label from given phone number in android

I tried to print Contact name(Eg. "Jose") & Contact phone number type value(Eg. "Home","Work",etc.,) from phone contact for given particular phone number(Eg. "9600515852")
for this i used bellow code. but i can able get Contact name only. i am getting app crash when i am trying to get the phone number type.
Please guide me.
String number = getContactName(this, "9600515852");
Toast.makeText(getApplicationContext(),number,Toast.LENGTH_LONG).show();
private String getContactName(Context context, String number) {
String TAG ="" ;
String name = null, label=null;
int type=0;
// 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));
// type = cursor.getInt(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));
} else {
Log.v(TAG, "Contact Not Found # " + number);
}
cursor.close();
}
return name+" - "+number;
}
use
name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
instead of
name = cursor.getString(cursor.getColumnIndex(ContactsContract.PhoneLookup.DISPLAY_NAME));
In My case, On button click getting contact information from Contacts app is like this. And it is working
Call an Intent
Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
intent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE);
startActivityForResult(intent, 1);
Handle the result
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case (1):
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.getColumnIndex(ContactsContract.Contacts._ID));
String name = c.getString(c.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
String resultedPhoneNum = pop.getContactNumber(id, MainActiv.this);
String resultedName=pop.getContactName(name, MainActiv.this);
contactName.setText(resultedName);
}
}
break;
}
}
Hope that helps!
Well the correct approach is to include the Phone.TYPE in your Projection query. It contains only DISPLAY_NAME andID and it crashes as there is no field as "type" included in your query.
This will definitely solve your issue.
Try this:
type = cursor.getString(cursor.getColumnIndex(ContactsContract.PhoneLookup.TYPE));

setting contact number to edittext in android

I am trying to get the phone number by the code below but setting the number to the EditText field seems not to work.
The code in onActivityResult() is not giving me the contact name from the selected contacts.
EditText number;
public void chooseContact(View v) {
contact = (ImageView) findViewById(R.id.quickContact);
contact.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE);
startActivityForResult(intent, PICK_CONTACT);
}
});
// Toast.makeText(getApplicationContext(), "hi contact is selected!!",
// Toast.LENGTH_SHORT).show();
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
ContentResolver cr = getContentResolver();
Uri contactData = data.getData();
Cursor c = getContentResolver().query(contactData, null, null,
null, null);
if (c.moveToFirst()) {
id = c.getString(c
.getColumnIndex(ContactsContract.Contacts._ID));
name = c.getString(c
.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
if (Integer
.parseInt(c.getString(c
.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
Cursor pCur = cr.query(Phone.CONTENT_URI, null,
Phone.CONTACT_ID + " = ?", new String[] { id },
null);
while (pCur.moveToNext()) {
cnumber = pCur.getString(pCur
.getColumnIndex(Phone.NUMBER));
// Toast.makeText(getApplicationContext(), cnumber,
// Toast.LENGTH_SHORT).show();
number.setText(cnumber);
}
}
}
}
}
try
number.setText(cnumber+"");
or
number.setText(String.valueOf(cnumber));
Updates:
Change your logic
int i = 0;
while (pCur.moveToNext()) {
cnumber = pCur.getString(pCur
.getColumnIndex(Phone.NUMBER));
// Toast.makeText(getApplicationContext(), cnumber,
// Toast.LENGTH_SHORT).show();
if(i == 0){
number.setText(cnumber);
break;
}
}
here. Because this loop will set the last number only in your number edittext.
You didn't find your EditText in the code, number = findViewById(R.id.edittext), try finding and executing it.
It's also showing the error. So, to solve -
1.
id = c.getString(c.getColumnIndex(ContactsContract.Contacts._ID));
//Instead of this place String keyword at starting.
2.
cnumber = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
//Place String keyword at starting.
3.
name = c.getString(c.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
//Place String keyword at starting.

How to get contacts' phone number in Android

My code is as below:
String[] columns = {ContactsContract.Contacts._ID, ContactsContract.Contacts.DISPLAY_NAME, ContactsContract.Contacts.HAS_PHONE_NUMBER};
Cursor cursor = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, columns, null, null, null);
int ColumeIndex_ID = cursor.getColumnIndex(ContactsContract.Contacts._ID);
int ColumeIndex_DISPLAY_NAME = cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);
int ColumeIndex_HAS_PHONE_NUMBER = cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER);
while(cursor.moveToNext())
{
String id = cursor.getString(ColumeIndex_ID);
String name = cursor.getString(ColumeIndex_DISPLAY_NAME);
String has_phone = cursor.getString(ColumeIndex_HAS_PHONE_NUMBER);
if(!has_phone.endsWith("0"))
{
System.out.println(name);
GetPhoneNumber(id);
}
}
cursor.close();
public String GetPhoneNumber(String id)
{
String number = "";
Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone._ID + " = " + id, null, null);
if(phones.getCount() > 0)
{
while(phones.moveToNext())
{
number = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
}
System.out.println(number);
}
phones.close();
return number;
}
I get contacts' name success, but get phone number fail in GetPhoneNumber().
The phones.getCount() always equal 0.
How can I modify?
Android Contact API For 2.0
//
// Find contact based on name.
//
ContentResolver cr = getContentResolver();
Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null,
"DISPLAY_NAME = '" + NAME + "'", null, null);
if (cursor.moveToFirst()) {
String contactId =
cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
//
// Get all phone numbers.
//
Cursor phones = cr.query(Phone.CONTENT_URI, null,
Phone.CONTACT_ID + " = " + contactId, null, null);
while (phones.moveToNext()) {
String number = phones.getString(phones.getColumnIndex(Phone.NUMBER));
int type = phones.getInt(phones.getColumnIndex(Phone.TYPE));
switch (type) {
case Phone.TYPE_HOME:
// do something with the Home number here...
break;
case Phone.TYPE_MOBILE:
// do something with the Mobile number here...
break;
case Phone.TYPE_WORK:
// do something with the Work number here...
break;
}
}
phones.close();
}
cursor.close();
For more information see this link
try this.
ContentResolver cr = 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));
String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
Log.i("Names", name);
if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0)
{
// Query phone here. Covered next
Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ id,null, null);
while (phones.moveToNext()) {
String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
Log.i("Number", phoneNumber);
}
phones.close();
}
}
}
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;
}
}
Old question but I don't see the following answer here.
private static final String[] PROJECTION ={
ContactsContract.Contacts._ID,
ContactsContract.CommonDataKinds.Phone.NUMBER,
ContactsContract.Contacts.DISPLAY_NAME_PRIMARY,
ContactsContract.Contacts.PHOTO_THUMBNAIL_URI,
ContactsContract.Contacts.LOOKUP_KEY,
};
new CursorLoader(
this,
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
PROJECTION,
null,
null,//mSelectionArgs,
ContactsContract.Contacts.DISPLAY_NAME_PRIMARY
);
Use column index ContactsContract.CommonDataKinds.Phone.NUMBER to retreive the phone number from the cursor.
U can use contact picker.
Call contact picker on any button and use the below code :
|*| Add in : AndroidManifest.xml
<uses-permission android:name="android.permission.READ_CONTACTS"/>
|*| Add in : activity_name.java
void calContctPickerFnc()
{
Intent calContctPickerIntent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
calContctPickerIntent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE);
startActivityForResult(calContctPickerIntent, 1);
}
#Override
public void onActivityResult(int reqCode, int resultCode, Intent data)
{
super.onActivityResult(reqCode, resultCode, data);
switch (reqCode)
{
case (1) :
if (resultCode == Activity.RESULT_OK)
{
Uri contctDataVar = data.getData();
Cursor contctCursorVar = getContentResolver().query(contctDataVar, null,
null, null, null);
if (contctCursorVar.getCount() > 0)
{
while (contctCursorVar.moveToNext())
{
String ContctUidVar = contctCursorVar.getString(contctCursorVar.getColumnIndex(ContactsContract.Contacts._ID));
String ContctNamVar = contctCursorVar.getString(contctCursorVar.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
Log.i("Names", ContctNamVar);
if (Integer.parseInt(contctCursorVar.getString(contctCursorVar.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0)
{
// Query phone here. Covered next
String ContctMobVar = contctCursorVar.getString(contctCursorVar.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
Log.i("Number", ContctMobVar);
}
}
}
}
break;
}
}
Your can add this code on your Activity class:
private final int PICK_CONTACT = 5;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.your_layout);
Intent intent = new Intent(Intent.ACTION_PICK,
ContactsContract.Contacts.CONTENT_URI);
intent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE);
startActivityForResult(intent, PICK_CONTACT);
//Your code
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == PICK_CONTACT && resultCode == RESULT_OK) {
Uri contactData = data.getData();
Cursor phones = getContentResolver()
.query(contactData,
null,
null,
null,
null);
String name = "", phoneNumber = "";
while (phones.moveToNext()) {
name = phones.getString(phones.getColumnIndex(
ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
phoneNumber = phones.getString(phones.getColumnIndex(
ContactsContract.CommonDataKinds.Phone.NUMBER));
}
phones.close();
}
}

Categories

Resources