Retrieving Phone Number from Android Contacts Doesn't Work On Samsung - android

When I put the code in starting from the line "// Using the contact ID now we will get contact phone number" to the line "cursorPhone.close", it doesn't display any contact info on my s3 or note 3, but does display it on my asus tablet. If i take out the code between the lines mentioned above, the code works on s3 and note 3. What am i doing wrong? There are no errors in the log.
private void getContacts() {
try {
String[] projection = new String[] {
ContactsContract.Contacts.DISPLAY_NAME,
ContactsContract.Contacts.HAS_PHONE_NUMBER,
ContactsContract.Contacts._ID };
mCursor = managedQuery(ContactsContract.Contacts.CONTENT_URI,
projection, ContactsContract.Contacts.HAS_PHONE_NUMBER + "=?", new String[] { "1" },
ContactsContract.Contacts.DISPLAY_NAME);
String phoneNumber = '';
while (mCursor.moveToNext()) {
contact contact = new contact();
String contactId = mCursor.getString(mCursor.getColumnIndex(ContactsContract.Contacts._ID));
contact.setContactName(mCursor.getString(mCursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)));
// Using the contact ID now we will get contact phone number
Cursor cursorPhone = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
new String[]{ContactsContract.CommonDataKinds.Phone.NUMBER},
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ? AND " +
ContactsContract.CommonDataKinds.Phone.TYPE + " = " +
ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE,
new String[]{contactId},
null);
if (cursorPhone.moveToFirst()) {
phoneNumber = (cursorPhone.getString(cursorPhone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)));
}
cursorPhone.close();//till here
contact_list.add(contact);
}
isChecked = new boolean[mCursor.getCount()];
for (int i = 0; i < isChecked.length; i++) {
isChecked[i] = false;
}
this.mContactAdapter = new contactAdapter(this, R.layout.contactlistview, contact_list);
lv.setAdapter(this.mContactAdapter);
mCursor.close();
runOnUiThread(returnRes);
} catch (Exception e) {
Log.d("getContacts", e.getMessage());
}
}

why dont you try to open inbuild Contact activity using -
Intent intent = new Intent(Intent.ACTION_PICK,
android.provider.ContactsContract.CommonDataKinds.Phone.CONTENT_URI);
startActivityForResult(intent, PICK_CONTACT); Hope this helps you.
You need to handle code In Activity Result Method of Activty like this:
if (reqCode == PICK_CONTACT && resultCode == Activity.RESULT_OK) {
// final String phoneNumber = data.getStringExtra("android.intent.extra.PHONE_NUMBER");
if (data != null) {
Uri uri = data.getData();
if (uri != null) {
Cursor c = null;
try {
c = getContentResolver().query(uri, new String[]{ContactsContract.CommonDataKinds.Phone.NUMBER},
null, null, null);
if (c != null && c.moveToFirst()) {
String number = c.getString(0);
number=number.replaceAll("\\s", "");
number=number.replaceAll("-", "");
edtPhoneNumber.setText(number);
}
} finally {
if (c != null) {
c.close();
}
}
}
}
finishActivity(PICK_CONTACT);
}
if (reqCode == PICK_CALLLOG && resultCode == Activity.RESULT_OK) {
edtPhoneNumber.setText(data.getStringExtra("CallerNumber"));
}
if (reqCode == PICK_SMSLOG && resultCode == Activity.RESULT_OK) {
edtPhoneNumber.setText(data.getStringExtra("CallerNumber"));
}
}

Related

How to retrieve company name from the selected contact

Using ContactsContract I am able to retrieve and display selected mobile number and the relevant contact name.
But instead of returning the company name it's returning the mobile number again.
The intent I use to select a specific phone number when there are multiple numbers
Intent calContctPickerIntent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
calContctPickerIntent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE);
startActivityForResult(calContctPickerIntent, 1);
here is the main code
#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));
String Companyname = contctCursorVar.getString(contctCursorVar.getColumnIndex(ContactsContract.CommonDataKinds.Organization.DATA));
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));
String Companyname2 = contctCursorVar.getString(contctCursorVar.getColumnIndex(ContactsContract.CommonDataKinds.Organization.COMPANY));
mobile.setText(ContctMobVar);
custname.setText(ContctNamVar);
companyname.setText(Companyname2);
Log.i("Number", ContctMobVar);
}
}
}
}
break;
}
}
I need to find a way to retrieve the company name saved under the selected contact.
You may try something like this :
ContentResolver mContentResolver = this.getContentResolver();
Cursor contacts = cr.query(ContactsContract.Contacts.CONTENT_URI,
null, null, null, null);
String mContactId = contacts.getString(contacts.getColumnIndex(ContactsContract.Contacts._ID));
String mRawContactId = getRawContactID(mContactId);
String mCompanyName = getCompanyName(mRawContactId);
private String getRawContactID(String contactId) {
String[] projection = new String[]{ContactsContract.RawContacts._ID};
String selection = ContactsContract.RawContacts.CONTACT_ID + "=?";
String[] selectionArgs = new String[]{contactId};
Cursor c = mContentResolver.query(ContactsContract.RawContacts.CONTENT_URI, projection, selection, selectionArgs, null);
if (c == null) return null;
int rawContactId = -1;
if (c.moveToFirst()) {
rawContactId = c.getInt(c.getColumnIndex(ContactsContract.RawContacts._ID));
}
c.close();
return String.valueOf(rawContactId);
}
private String getCompanyName(String rawContactId) {
try {
String orgWhere = ContactsContract.Data.RAW_CONTACT_ID + " = ? AND " + ContactsContract.Data.MIMETYPE + " = ?";
String[] orgWhereParams = new String[]{rawContactId,
ContactsContract.CommonDataKinds.Organization.CONTENT_ITEM_TYPE};
Cursor cursor = mContentResolver.query(ContactsContract.Data.CONTENT_URI,
null, orgWhere, orgWhereParams, null);
if (cursor == null) return null;
String name = null;
if (cursor.moveToFirst()) {
name = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Organization.COMPANY));
}
cursor.close();
return name;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
With the help of the solution provided by #Shiva Snape I was able to solve the problem.
Code for button to call contacts app and select the relevant number
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE);
startActivityForResult(intent, 1);
Code to collect Contact Number, Name and company name.
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (data != null) {
Uri uri = data.getData();
if (uri != null) {
Cursor c = null;
try {
c = getContentResolver().query(uri, new String[]{
ContactsContract.CommonDataKinds.Phone.CONTACT_ID,
ContactsContract.CommonDataKinds.Phone.TYPE,
ContactsContract.CommonDataKinds.Phone.NUMBER,
ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME},
null, null, null);
ContentResolver mContentResolver = this.getContentResolver();
if (c != null && c.moveToFirst()) {
String number = c.getString(c.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
String name = c.getString(c.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
String contactId = c.getString(c.getColumnIndex(ContactsContract.CommonDataKinds.Phone.CONTACT_ID));
String rawContactId = getRawContactId(contactId);
String companyName = getCompanyName(rawContactId);
int type = c.getInt(1);
showSelectedNumber(type, number);
}
} finally {
if (c != null) {
c.close();
}
}
}
}
}
private String getCompanyName(String rawContactId) {
try {
String orgWhere = ContactsContract.Data.RAW_CONTACT_ID + " = ? AND " + ContactsContract.Data.MIMETYPE + " = ?";
String[] orgWhereParams = new String[]{rawContactId,
ContactsContract.CommonDataKinds.Organization.CONTENT_ITEM_TYPE};
Cursor cursor = getContentResolver().query(ContactsContract.Data.CONTENT_URI,
null, orgWhere, orgWhereParams, null);
if (cursor == null) return null;
String Vname = null;
if (cursor.moveToFirst()) {
Vname = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Organization.COMPANY));
}
cursor.close();
return Vname;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
private String getRawContactId(String contactId) {
String[] projection = new String[]{ContactsContract.RawContacts._ID};
String selection = ContactsContract.RawContacts.CONTACT_ID + "=?";
String[] selectionArgs = new String[]{contactId};
Cursor c = getContentResolver().query(ContactsContract.RawContacts.CONTENT_URI, projection, selection, selectionArgs, null);
if (c == null) return null;
int rawContactId = -1;
if (c.moveToFirst()) {
rawContactId = c.getInt(c.getColumnIndex(ContactsContract.RawContacts._ID));
}
c.close();
return String.valueOf(rawContactId);
}
public void showSelectedNumber(int type, String number) {
Toast.makeText(this, type + ": " + number, Toast.LENGTH_LONG).show();
}
}
Thanks to the Stack Overflow community for helping me out. Hope this becomes helpful to others.

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

retrieving photo and name from contacts in android [duplicate]

This question already has answers here:
Getting a Photo from a Contact
(9 answers)
Closed 8 months ago.
With my present code I am getting only the contact number. But I want to get contact's name and contact's photo path. Have tried many codes by googling, but I am not able to get it done. Tried this too, but got FileNotFoundException. Can someone please help me achieve that by adding code snippets to the below code?
public void getContact(View view)
{
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE);
startActivityForResult(intent, 1);
}
protected void onActivityResult(int requestCode, int resultCode,Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
if(resultCode==RESULT_OK && requestCode == 1)
{
if (data != null) {
Uri uri = data.getData();
if (uri != null) {
Cursor c = null;
try {
c = getContentResolver().query(uri, new String[]{
ContactsContract.CommonDataKinds.Phone.NUMBER,
ContactsContract.CommonDataKinds.Phone.TYPE },
null, null, null);
if (c != null && c.moveToFirst()) {
String phoneNumber = c.getString(0);
int type = c.getInt(1);
}
} finally {
if (c != null) {
c.close();
}
}
}
}
}
}
This code traverses all contacts and gets their first name, last name and photo as a bitmap:
Cursor cursor = App
.getInstance()
.getContentResolver()
.query(ContactsContract.Contacts.CONTENT_URI, null, null, null,
null);
if (cursor != null && cursor.getCount() > 0) {
while (cursor.moveToNext()) {
long id = cursor.getLong(cursor.getColumnIndex(ContactsContract.Contacts._ID));
// get firstName & lastName
Cursor nameCur = App.getInstance().getContentResolver()
.query(ContactsContract.Data.CONTENT_URI,
null,
ContactsContract.Data.MIMETYPE
+ " = ? AND "
+ StructuredName.CONTACT_ID
+ " = ?",
new String[] {
StructuredName.CONTENT_ITEM_TYPE,
Long.valueOf(id).toString() }, null);
if (nameCur != null) {
if (nameCur.moveToFirst()) {
String displayName = nameCur.getString(nameCur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
if (displayName == null) displayName = "";
String firstName = nameCur.getString(nameCur.getColumnIndex(StructuredName.GIVEN_NAME));
if (firstName == null) firstName = "";
Log.d("--> ", firstName.length()>0?firstName:displayName);
String middleName = nameCur.getString(nameCur.getColumnIndex(StructuredName.MIDDLE_NAME));
if (middleName == null) middleName = "";
String lastName = nameCur.getString(nameCur.getColumnIndex(StructuredName.FAMILY_NAME));
if (lastName == null) lastName = "";
lastName = middleName + (middleName.length()>0?" ":"") + lastName;
Log.d("--> ", lastName);
}
nameCur.close();
}
Bitmap photo = null;
try {
InputStream inputStream = ContactsContract.Contacts.openContactPhotoInputStream(App.getContext().getContentResolver(),
ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, Long.valueOf(id)));
if (inputStream != null) {
photo = BitmapFactory.decodeStream(inputStream);
}
if (inputStream != null) inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
Log.d("--> ", photo);
}
}
if (cursor != null) { cursor.close(); }
You also needs this permission: <uses-permission android:name="android.permission.READ_CONTACTS" />
Hope that helps!
This is the method am using to get contact photo, number and name but am dong this from a Fragment.
1 Set the permission in your manifest.
<uses-permission android:name="android.permission.READ_CONTACTS" />
2 Declare a constant:
private static final int REQUEST_CODE_PICK_CONTACT = 1;
3 In my app the user is required to choose a phone contact by clicking on a button. So in the onClick() method I do this:
contactChooserButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivityForResult(new Intent(Intent.ACTION_PICK,
ContactsContract.Contacts.CONTENT_URI), REQUEST_CODE_PICK_CONTACT);
}
});
4 Add the methods to retrieve photo, name, and number:
private String retrieveContactNumber() {
String contactNumber = null;
// getting contacts ID
Cursor cursorID = getActivity().getContentResolver().query(uriContact,
new String[]{ContactsContract.Contacts._ID},
null, null, null);
if (cursorID.moveToFirst()) {
contactID = cursorID.getString(cursorID.getColumnIndex(ContactsContract.Contacts._ID));
}
cursorID.close();
Log.e(TAG, "Contact ID: " + contactID);
// Using the contact ID now we will get contact phone number
Cursor cursorPhone = getActivity().getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
new String[]{ContactsContract.CommonDataKinds.Phone.NUMBER},
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ? AND " +
ContactsContract.CommonDataKinds.Phone.TYPE + " = " +
ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE,
new String[]{contactID},
null);
if (cursorPhone.moveToFirst()) {
contactNumber = cursorPhone.getString(cursorPhone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
phoneNumber = contactNumber;
}
cursorPhone.close();
Log.e(TAG, "Contact Phone Number: " + contactNumber);
return contactNumber;
}
//Retrieve name
private void retrieveContactName() {
String contactName = null;
// querying contact data store
Cursor cursor = getActivity().getContentResolver().query(uriContact, null, null, null, null);
if (cursor.moveToFirst()) {
// DISPLAY_NAME = The display name for the contact.
// HAS_PHONE_NUMBER = An indicator of whether this contact has at least one phone number.
contactName = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
personName = contactName;
}
cursor.close();
Log.e(TAG, "Contact Name: " + contactName);
}
//Retrieve photo (this method gets a large photo, for thumbnail follow the link below)
public void retrieveContactPhoto() {
Uri contactUri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, Long.parseLong(contactID));
Uri displayPhotoUri = Uri.withAppendedPath(contactUri, ContactsContract.Contacts.Photo.DISPLAY_PHOTO);
try {
AssetFileDescriptor fd =
getActivity().getContentResolver().openAssetFileDescriptor(displayPhotoUri, "r");
photoAsBitmap = BitmapFactory.decodeStream(fd.createInputStream());
} catch (IOException e) {
e.printStackTrace();
}
}
finally, in your onActivityForResult method, do this:
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
try {
if(requestCode == REQUEST_CODE_PICK_CONTACT && resultCode == Activity.RESULT_OK) {
Log.e(TAG, "Response: " + data.toString());
uriContact = data.getData();
personPhoneTextField.setText(retrieveContactNumber());
//the method retrieveContactNumber returns the contact number,
//so am displaying this number in my EditText after getting it.
//Make your other methods return data of
//their respective types (Bitmap for photo)
retrieveContactPhoto();
retrieveContactName();
}
} catch (Exception exception) {
exception.printStackTrace();
}
}
That's it.For Activities, take a look at this Android: Get Contact Details (ID, Name, Phone, Photo) on Github

Getting multiple phone numbers from selected contact

I am developing an application in which i am using startactivity result and accessing default android phonebook in my application, after that on selecting one contact i am getting name, one phone number of the selected contact. I want to retrieve multiple phone numbers if any of the contacts have it and type of phone numbers like work, mobile etc. please help me in this, Any help would be appreciated.
try this as this works for me:
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType(ContactsContract.Contacts.CONTENT_TYPE);
startActivityForResult(intent, AppConstants.PICK_CONTACT);
then in onActivityResult do the following:
Cursor cursor = null;
String phoneNumber = "", primaryMobile = "";
List<String> allNumbers = new ArrayList<String>();
int contactIdColumnId = 0, phoneColumnID = 0, nameColumnID = 0;
try {
Uri result = data.getData();
Utils.printLog(TAG, result.toString());
String id = result.getLastPathSegment();
cursor = getContentResolver().query(Phone.CONTENT_URI, null, Phone.CONTACT_ID + "=?", new String[] { id }, null);
contactIdColumnId = cursor.getColumnIndex(ContactsContract.Data.RAW_CONTACT_ID);
phoneColumnID = cursor.getColumnIndex(Phone.DATA);
nameColumnID = cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);
if (cursor.moveToFirst()) {
while (cursor.isAfterLast() == false) {
idContactBook = cursor.getString(contactIdColumnId);
displayName = cursor.getString(nameColumnID);
phoneNumber = cursor.getString(phoneColumnID);
if (phoneNumber.length() == 0)
continue;
int type = cursor.getInt(cursor.getColumnIndex(Phone.TYPE));
if (type == Phone.TYPE_MOBILE && primaryMobile.equals(""))
primaryMobile = phoneNumber;
allNumbers.add(phoneNumber);
cursor.moveToNext();
}
} else {
// no results actions
}
} catch (Exception e) {
// error actions
} finally {
if (cursor != null) {
cursor.close();
}
}
try this
Uri personUri = ContentUris.withAppendedId(People.CONTENT_URI, personId);
Uri phonesUri = Uri.withAppendedPath(personUri, People.Phones.CONTENT_DIRECTORY);
String[] proj = new String[] {Phones._ID, Phones.TYPE, Phones.NUMBER, Phones.LABEL}
Cursor cursor = contentResolver.query(phonesUri, proj, null, null, null);

How to Get Contact name, number and emai ID from a list of contacts?

I am new to android ,I need to get the details of my contacts, but the details include only 3
Contact name
contact number and
Email ID
when I press a Button it will show these 3 details of my all contacts
I am using android Eclair version 2.1. Any solution ?
By below code you can do that -
public void doLaunchContactPicker(View view) {
Intent contactPickerIntent = new Intent(Intent.ACTION_PICK,Contacts.CONTENT_URI);
startActivityForResult(contactPickerIntent, CONTACT_PICKER_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 email = "", name = "";
try {
Uri result = data.getData();
Log.v(DEBUG_TAG, "Got a contact result: " + result.toString());
// get the contact id from the Uri
String id = result.getLastPathSegment();
// query for everything email
cursor = getContentResolver().query(Email.CONTENT_URI, null, Email.CONTACT_ID + "=?", new String[] { id }, null);
int nameId = cursor.getColumnIndex(Contacts.DISPLAY_NAME);
int emailIdx = cursor.getColumnIndex(Email.DATA);
// let's just get the first email
if (cursor.moveToFirst()) {
email = cursor.getString(emailIdx);
name = cursor.getString(nameId);
Log.v(DEBUG_TAG, "Got email: " + email);
} else {
Log.w(DEBUG_TAG, "No results");
}
} catch (Exception e) {
Log.e(DEBUG_TAG, "Failed to get email data", e);
} finally {
if (cursor != null) {
cursor.close();
}
EditText emailEntry = (EditText) findViewById(R.id.editTextv);
EditText personEntry = (EditText) findViewById(R.id.person);
emailEntry.setText(email);
personEntry.setText(name);
if (email.length() == 0 && name.length() == 0)
{
Toast.makeText(this, "No Email for Selected Contact",Toast.LENGTH_LONG).show();
}
}
break;
}
} else {
Log.w(DEBUG_TAG, "Warning: activity result not ok");
}
}
And, also refer these links -
Get Contact Details
How to Read Contacts
Get All Contact Details
Don't forget to add the required permission -
<uses-permission android:name="android.permission.READ_CONTACTS"/>
in your AndroidManifest.xml file. And, Just modify this code with your needs.
You can access addressbook like this;
ContentResolver cr = getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null,null, null,ContactsContract.Contacts.DISPLAY_NAME);
int kisiSayisi = cur.getCount();
if(kisiSayisi > 0)
{
int KisiIndex = 0;
while(cur.moveToNext())
{
String id = cur.getString(cur.getColumnIndex(BaseColumns._ID));
String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0)
{
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.DATA));
//String phoneType = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));
String dogruGSM = gsmNoKontrol(phone);
if(dogruGSM.compareTo("0") != 0){
Kisi kisi = new Kisi(KisiIndex, name, dogruGSM, false);
MyList.add(kisi);
KisiIndex ++;
}
}
pCur.close();
}
}
}

Categories

Resources