How to get contacts' phone number in Android
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
Get a single phone number from contacts Android
When you are in your contacts list and click a Contact, you are going to get the Result of your Intent.ACTION_PICK
intent.
So, to handle the result, you have to override onActivityResult()
method.
You receive the lookup URI of your contact in the data Intent
object returned by onActivityResult()
.
So, all you have to do is this:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == CONTACT_PICKER_RESULT){
if(resultCode==Activity.RESULT_OK){
Uri uri = data.getData();
ContentResolver contentResolver = getContentResolver();
Cursor contentCursor = contentResolver.query(uri, null, null,null, null);
if(contentCursor.moveToFirst()){
String id = contentCursor.getString(contentCursor.getColumnIndexOrThrow(ContactsContract.Contacts._ID));
String hasPhone =
contentCursor.getString(contentCursor.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 +" = "+ id,null, null);
phones.moveToFirst();
String contactNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
Log.i("phoneNUmber", "The phone number is "+ contactNumber);
}
}
super.onActivityResult(requestCode, resultCode, data);
}
}
}
Then you just have to do whatever you want with the value on contactNumber
.
Hope to have help you.
Android get phone number from contact list
The ContactsContract
Android API stores data about contacts like phone number in the Data
table, not the Contacts
table.
Read this carefully: https://developer.android.com/reference/android/provider/ContactsContract.html.
Update - here's a fixed version of your code (untested):
final ContentResolver cr = getContentResolver();
String[] projection = new String[] {Contacts.DISPLAY_NAME, Phone.NUMBER};
final Cursor c = cr.query(Data.CONTENT_URI, projection, null, null, null);
myCursorAdapter = new SimpleCursorAdapter(this, R.layout.list_item, c, new String[] {Phone.NUMBER}, new int[]{R.id.TVRow}, 0);
myPhoneList.setAdapter(myCursorAdapter);
myPhoneList.setOnItemClickListener(new AdapterView.OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id){
c.moveToPosition(position);
Toast.makeText(getApplicationContext(), c.getString(1), Toast.LENGTH_SHORT).show();
}
});
How to get phone number from contact in android
Thanks for @Levon Petrosyan
But I just need to add the part from his link and copy it to my function.
This is the working code:
@Override
public void onActivityResult(int reqCode, int resultCode, Intent data){
super.onActivityResult(reqCode, resultCode, data);
if(reqCode == this.pickContact){
if (resultCode == Activity.RESULT_OK) {
Log.d("ContactsH", "ResOK");
Uri contactData = data.getData();
Cursor contact = getContentResolver().query(contactData, null, null, null, null);
if (contact.moveToFirst()) {
String name = contact.getString(contact.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
// TODO Whatever you want to do with the selected contact's 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(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + contactId, null, null);
while (phones.moveToNext()) {
String number = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
int type = phones.getInt(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));
switch (type) {
case ContactsContract.CommonDataKinds.Phone.TYPE_HOME:
// do something with the Home number here...
break;
case ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE:
// do something with the Mobile number here...
Log.d("ContactsH", number);
this.callByNumber(number);
break;
case ContactsContract.CommonDataKinds.Phone.TYPE_WORK:
// do something with the Work number here...
break;
}
}
phones.close();
}
cursor.close();
}
}
}else{
Log.d("ContactsH", "Canceled");
}
}
get android contact phone number list
getNumber(this.getContentResolver());
public void getNumber(ContentResolver cr)
{
Cursor phones = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,null,null, null);
// use the cursor to access the contacts
while (phones.moveToNext())
{
String name=phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
// get display name
phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
// get phone number
System.out.println(".................."+phoneNumber);
}
}
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<ListView
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:id="@+id/lv"/>
</RelativeLayout>
MainActivity.java
public class MainActivity extends Activity {
String phoneNumber;
ListView lv;
ArrayList <String> aa= new ArrayList<String>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv= (ListView) findViewById(R.id.lv);
getNumber(this.getContentResolver());
}
public void getNumber(ContentResolver cr)
{
Cursor phones = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,null,null, null);
while (phones.moveToNext())
{
String name=phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
System.out.println(".................."+phoneNumber);
aa.add(phoneNumber);
}
phones.close()// close cursor
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1,aa);
lv.setAdapter(adapter);
//display contact numbers in the list
}
}
snap shot
Make sure you have this in manifest
<uses-permission android:name="android.permission.READ_CONTACTS"/>
Read all contacts' phone numbers in android
You can read all of the telephone numbers associated with a contact in the following manner:
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);
Please note that this example (like yours) uses the deprecated contacts API. From eclair onwards this has been replaced with the ContactsContract API.
Related Topics
Radiogroup with Two Columns Which Have Ten Radiobuttons
How to Join Png with Alpha/Transparency in a Frame in Realtime
How to Get App's Permission for Each App? How to Do It Programmatically on Android
Copy Database from Assets to Databases Folder
Firebase Admin Sdk for Android, Methods Not Found
Android:How to Capture a Image of Gps Location
Create Clickable Link in Text View in Android
How to Detect Power Connected State
Overriding Referenced Style Attributes
Android Device Gps On/Off Programmatically
Rotating Phone Quickly 180 Degrees, Camera Preview Turns Upside Down
Is Ondestroy Not Always Called
Upload Video to Facebook in Android
Problem to Achieve Curved Animation
How to Serialize My User Class for the Firebase Database and Fix This Error