How to call Android contacts list?
I'm not 100% sure what your sample code is supposed to do, but the following snippet should help you 'call the contacts list function, pick a contact, then return to [your] app with the contact's name'.
There are three steps to this process.
1. Permissions
Add a permission to read contacts data to your application manifest.
<uses-permission android:name="android.permission.READ_CONTACTS"/>
2. Calling the Contact Picker
Within your Activity, create an Intent that asks the system to find an Activity that can perform a PICK action from the items in the Contacts URI.
Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
Call startActivityForResult
, passing in this Intent (and a request code integer, PICK_CONTACT
in this example). This will cause Android to launch an Activity that's registered to support ACTION_PICK
on the People.CONTENT_URI
, then return to this Activity when the selection is made (or canceled).
startActivityForResult(intent, PICK_CONTACT);
3. Listening for the Result
Also in your Activity, override the onActivityResult
method to listen for the return from the 'select a contact' Activity you launched in step 2. You should check that the returned request code matches the value you're expecting, and that the result code is RESULT_OK
.
You can get the URI of the selected contact by calling getData()
on the data Intent parameter. To get the name of the selected contact you need to use that URI to create a new query and extract the name from the returned cursor.
@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 = getContentResolver().query(contactData, null, null, null, null);
if (c.moveToFirst()) {
String name = c.getString(c.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
// TODO Whatever you want to do with the selected contact name.
}
}
break;
}
}
Full source code: tutorials-android.blogspot.com (how to call android contacts list).
how to show list contact phone in android studio with sdk 27
You can try LoaderManager
Create following fields
private static final int CONTACTS_LOADER_ID = 101;
private static final String[] PROJECTION = {
ContactsContract.Contacts._ID,
ContactsContract.Contacts.DISPLAY_NAME_PRIMARY,
ContactsContract.CommonDataKinds.Phone.NUMBER
};
Create follwoing class
public class Contact {
private String id;
private String name;
private String number;
public Contact(String id, String name, String number) {
this.id = id;
this.name = name;
this.number = number;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
}
[1] Create instance of LoaderManager.LoaderCallback
private LoaderManager.LoaderCallbacks<Cursor> loaderCallbacks = new LoaderManager.LoaderCallbacks<Cursor>() {
@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
switch (id) {
case CONTACTS_LOADER_ID:
return new CursorLoader(
getActivity(),
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
PROJECTION,
null,
null,
ContactsContract.Contacts.DISPLAY_NAME_PRIMARY + " ASC"
);
default:
return null;
}
}
@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
switch (loader.getId()) {
case CONTACTS_LOADER_ID:
//Use ContactUtils class here
break;
}
}
@Override
public void onLoaderReset(Loader<Cursor> loader) {
}
};
[2] Use following class to convert cursor data to List<Contact>
import android.content.ContentResolver;
import android.database.Cursor;
import android.provider.ContactsContract;
import java.util.ArrayList;
import java.util.List;
public class ContactUtils {
private ContactUtils() {
}
public static List<Contact> parseContacts(Cursor cursor) {
List<Contact> contacts = new ArrayList<>();
if (cursor.getCount() == 0) {
return contacts;
}
int nameColumnIndex = cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME_PRIMARY);
int idColumnIndex = cursor.getColumnIndex(ContactsContract.Contacts._ID);
int numberColumnIndex = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
while (cursor.moveToNext()) {
String contactName = cursor.getString(nameColumnIndex);
String id = cursor.getString(idColumnIndex);
String number = cursor.getString(numberColumnIndex);
Contact contact = new Contact(id, contactName, number);
if(!isContactAddedInList(contact, contacts)){
contacts.add(contact);
}
}
return contacts;
}
public boolean isContactAddedInList(Contact contact, List<Contact> contacts) {
for (Contact listContact : contacts
) {
if (listContact.number.equals(contact.name)) {
return true;
}
}
return false;
}
}
[3] Call getLoaderManager().initLoader
method to load contacts in your Fragment or Activity's onStart
method
getLoaderManager().initLoader(CONTACTS_LOADER_ID, null, loaderCallbacks);
- Add
<uses-permission android:name="android.permission.READ_CONTACTS" />
permission in menifest file - You need to ask for runtime permission before calling
getLoaderManager().initLoader
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"/>
How to get contact number from contactlist in Android?
I got this answer from the following link
http://tutorials-android.blogspot.in/2011/11/how-to-call-android-contacts-list.html
I need to call a particular number in the contacts
You just want to initiate the intent with ACTION_CALL like this.
First of all grab the phone number from the item from which the click happens.And use the code to initiate a call.
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:"+phonenumber));
startActivity(callIntent);
Hope you got the answer.
android get all contacts
Try this too,
private void getContactList() {
ContentResolver cr = getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,
null, null, null, null);
if ((cur != null ? cur.getCount() : 0) > 0) {
while (cur != null && cur.moveToNext()) {
String id = cur.getString(
cur.getColumnIndex(ContactsContract.Contacts._ID));
String name = cur.getString(cur.getColumnIndex(
ContactsContract.Contacts.DISPLAY_NAME));
if (cur.getInt(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 phoneNo = pCur.getString(pCur.getColumnIndex(
ContactsContract.CommonDataKinds.Phone.NUMBER));
Log.i(TAG, "Name: " + name);
Log.i(TAG, "Phone Number: " + phoneNo);
}
pCur.close();
}
}
}
if(cur!=null){
cur.close();
}
}
If you need more reference means refer this link Read ContactList
Related Topics
Save Arraylist to Sharedpreferences
Trying to Start a Service on Boot on Android
How to Enable Multidexing With the New Android Multidex Support Library
Error in Launching Avd With Amd Processor
Android: Listview Elements With Multiple Clickable Buttons
How to Send a Json Object Over Request With Android
Google-Services.Json For Different Productflavors
How to Convert an Image into a Base64 String
Firebase Data Desc Sorting in Android
Android Marshmallow Request Permission
Firebase Android: Make Username Unique
How to Refresh Android Listview
How to Change Fontfamily of Textview in Android
Java.Lang.Outofmemoryerror: Bitmap Size Exceeds Vm Budget - Android