How to import a Specific Contact's phone number? - android

I'm trying to Read Phone number of a Contact Selected using Contact Picker.
The Display Name works fine, But Phone number doesn't.
Code:
//calling Contact Picker
public void CPick(View v){
Intent intent=new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(intent, PICK_CONTACT);
}
#Override
//Contact Picker here:
protected void onActivityResult(int reqCode, int resultCode, Intent data){
super.onActivityResult(reqCode,resultCode, data);
if (reqCode==PICK_CONTACT){
if(resultCode==AppCompatActivity.RESULT_OK){
Uri contatctData=data.getData();
Cursor c=getContentResolver().query(contatctData,null,null,null,null);
if (c.moveToFirst()){
//String name=c.getString(c.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME));
//Above line works Fine
String name=c.getString(c.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.NUMBER));
//Above line gives error on runtime "invalid column"
Toast.makeText(this,"U have picked:"+name,Toast.LENGTH_SHORT).show();
}
}
}
}
Anyhelp will be very Appreciated because I couldn't find relevant answer anywhere.

If you want to allow the user to pick a phone-number, the best option is to use a PHONE-PICKER not a CONTACT-PICKER:
Intent intent = new Intent(Intent.ACTION_PICK, CommonDataKinds.Phone.CONTENT_URI);
startActivityForResult(intent, PICK_PHONE);
...
protected void onActivityResult(int requestCode, int resultCode, Intent intent){
if (requestCode == PICK_PHONE && resultCode == RESULT_OK){
Uri phoneUri = intent.getData();
Cursor cur = getContentResolver().query(phoneUri, new String[] { Phone.DISPLAY_NAME, Phone.NUMBER }, null, null, null);
if (cur != null && cur.moveToFirst()){
String name = cur.getString(0);
String number = cur.getString(1);
Log.d("PHONE-PICKER", "User picker: " + name + " - " + number);
cur.close();
}
}
}

Try this method:
private void retrieveContactNumber() {
String contactNumber = null;
// getting contacts ID
Cursor cursorID = 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.d(TAG, "Contact ID: " + contactId);
// 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()) {
contactNumber = cursorPhone.getString(cursorPhone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
}
cursorPhone.close();
Log.d(TAG, "Contact Phone Number: " + contactNumber);
}
You should see the contact number in your logcat.

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

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

Android:- Select Contact and call to selected number

I have textview. Clicking on it opens up native contact list. Once the users selects a contact, i should display the number in my app. I could display the name but not able to display number. Please help.
Thanks in Advance.
This is my code but after selecting the contact my app crashes."Unfortunately 'app_name' has stopped"
public void dail(View v)
{
Intent contactPickerIntent = new Intent(Intent.ACTION_PICK, Contacts.CONTENT_URI);
startActivityForResult(contactPickerIntent, CONTACT_PICKER_RESULT);
}
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.NUMBER,
ContactsContract.CommonDataKinds.Phone.TYPE },
null, null, null);
if (c != null && c.moveToFirst()) {
String number = c.getString(0);
int type = c.getInt(1);
showSelectedNumber(type, number);
}
} finally {
if (c != null) {
c.close();
}
}
}
}
}
public void showSelectedNumber(int type, String number) {
Toast.makeText(this, type + ": " + number, Toast.LENGTH_LONG).show();
}
}
Try this in your onActivity result. It will work.
ContentResolver cr = getContentResolver();
cursor = cr.query(intent.getData(), null, null, null, null);
while (cursor.moveToNext()) {
String contactId = cursor.getString(cursor
.getColumnIndex(ContactsContract.Contacts._ID));
if (Integer
.parseInt(cursor.getString(cursor
.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
Cursor phones = getContentResolver().query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID
+ " = " + contactId, null, null);
while (phones.moveToNext()) {
phoneNumber = phones
.getString(phones
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
}
phones.close();
} else {
snipp.showAlertDialog(getApplicationContext(), "No Number",
"Cannot read number", false);
}
}
cursor.close();
Try out as below in your onActivityResult
ContentResolver cr = getContentResolver();
Uri contactData = data.getData();
Log.v("Contact", contactData.toString());
Cursor c = managedQuery(contactData,null,null,null,null);
if(c.moveToFirst()){
id = c.getString(c.getColumnIndex(ContactsContract.Contacts._ID));
Log.v("Contact", "ID: " + id.toString());
name = c.getString(c.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
Log.v("Contact", "Name: " + name.toString());
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()){
phone = pCur.getString(pCur .getColumnIndex(Phone.NUMBER));
Log.v("getting phone number", "Phone Number: " + phone);
}
}
}
Try this
Put this code in your textview OnclickListner
Intent intent = new Intent(Intent.ACTION_PICK, People.CONTENT_URI);
startActivityForResult(intent, PICK_CONTACT);
In Activity result put this code
#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))+" : "+c.getInt(c.getColumnIndexOrThrow(People.NUMBER));
//
txtContacts1.setText(name);
}
}
break;
}
For dail that no put this code
Intent intent = new Intent(Intent.ACTION_CALL);
intent.setData(Uri.parse("tel:" + c.getInt(c.getColumnIndexOrThrow(People.NUMBER)));
context.startActivity(intent);

Getting contact number using content provider in android

I followed this tutorial and got the basics of Content Providers : http://www.vogella.de/articles/AndroidSQLite/article.html
But wanted to know how can i get the contact number that is stored against display name. tried with "ContactsContract.Contacts.CONTENT_VCARD_TYPE". But got an error.
Please let me know if there is any solution.
Thanks
Sneha
This is a good tutorial about Getting contact number using content provider in android
http://www.higherpass.com/Android/Tutorials/Working-With-Android-Contacts/
and
http://www.app-solut.com/blog/2012/06/retrieval-of-contacts-with-contact-contract/
and can pick contact number like this
add button click event like
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
i = new Intent(Intent.ACTION_PICK,
ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(i, PICK_CONTACT);
}
});
//outside button click
public void onActivityResult(int reqCode, int resultCode, Intent data) {
super.onActivityResult(reqCode, resultCode, data);
switch (reqCode) {
case (PICK_CONTACT):
if (resultCode == Activity.RESULT_OK) {
getContactInfo(data);
}
}
}
// onActivityResult
private void getContactInfo(Intent data) {
// TODO Auto-generated method stub
ContentResolver cr = getContentResolver();
Cursor cursor = managedQuery(data.getData(), null, null, null, null);
while (cursor.moveToNext()) {
String contactId = cursor.getString(cursor
.getColumnIndex(ContactsContract.Contacts._ID));
Name = cursor
.getString(cursor
.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME));
String hasPhone = cursor
.getString(cursor
.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
Cursor emailCur = cr.query(
ContactsContract.CommonDataKinds.Email.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?",
new String[]{contactId}, null);
emailCur.close();
if (hasPhone.equalsIgnoreCase("1"))
hasPhone = "true";
else
hasPhone = "false";
if (Boolean.parseBoolean(hasPhone)) {
Cursor phones = getContentResolver().query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID
+ " = " + contactId, null, null);
while (phones.moveToNext()) {
phoneNo = phones
.getString(phones
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
}
phones.close();
}
pname.setText(Name);
//
phno.setText(phoneNo);
Toast.makeText(this, Name + " " + phoneNo, Toast.LENGTH_SHORT).show();
}
}
This code will work for mobile number contacts, not for email or something. i found this code simplest. let me know if there is any problem.
start activity with pick intent on phone data type:
findViewById(R.id.choose_contact_button).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent pickContact = new Intent(Intent.ACTION_PICK, ContactsContract.CommonDataKinds.Phone.CONTENT_URI);
startActivityForResult(pickContact, PICK_CONTACT_REQUEST);
}
});
Now set onAcitivityResult();
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent){
switch (requestCode){
case PICK_CONTACT_REQUEST:
if (resultCode == RESULT_OK){
Uri contactUri = intent.getData();
Cursor nameCursor = getContentResolver().query(contactUri, null, null, null, null);
if (nameCursor.moveToFirst()){
String name = nameCursor.getString(nameCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
String number = nameCursor.getString(nameCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
((EditText)findViewById(R.id.person_name)).setText(name);
((EditText)findViewById(R.id.enter_mobile)).setText(number);
nameCursor.close();
}
}
break;
}
}

Categories

Resources