get android contact phone number list

前端 未结 4 1835
逝去的感伤
逝去的感伤 2020-12-05 15:28

I am new to android.When i try to get contact names its working fine but i want to get numbers only, but i am not able to do so. My code is:-

package com.ex         


        
相关标签:
4条回答
  • 2020-12-05 15:57

    My solution to recover all contacts:

        Cursor cursor = null;
        try {
            cursor = context.getContentResolver().query(Phone.CONTENT_URI, null, null, null, null);
            int contactIdIdx = cursor.getColumnIndex(Phone._ID);
            int nameIdx = cursor.getColumnIndex(Phone.DISPLAY_NAME);
            int phoneNumberIdx = cursor.getColumnIndex(Phone.NUMBER);
            int photoIdIdx = cursor.getColumnIndex(Phone.PHOTO_ID);
            cursor.moveToFirst();
            do {
                String idContact = cursor.getString(contactIdIdx);
                String name = cursor.getString(nameIdx);
                String phoneNumber = cursor.getString(phoneNumberIdx);
                //...
            } while (cursor.moveToNext());  
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (cursor != null) {
                cursor.close();
            }
        }
    

    You need this permission in your manifest :

    <uses-permission android:name="android.permission.READ_CONTACTS" />
    

    I hope I have helped you!

    0 讨论(0)
  • 2020-12-05 16:03

    Find the solution below, It will work for getting contact no from contactlist.

    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;
      }
     }
    
    0 讨论(0)
  • 2020-12-05 16:05

    .@Arpit:

    add the permission in your manifest.

    BUT MOST IMPORTANTLY... Didn't you noticed that just below the line where you formatted the phoneNumber you did not add the names? You should add the name to the number just the way you added the phoneNumber (I enclosed in the code below):

    ..System.out.println(".................."+phoneNumber); 
    aa.add(name);
    aa.add(phoneNumber);
    

    the output of the above code will be separate names and numbers on your listview(like this):


    name

    number

    name

    in order to display that in one line, you can do this:

    aa.add(name+"\n"+phoneNumber);

    and the output will be like this:


    name number


    name number


    name number


    good luck!

    0 讨论(0)
  • 2020-12-05 16:15
        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

    enter image description here

    Make sure you have this in manifest

           <uses-permission android:name="android.permission.READ_CONTACTS"/>
    
    0 讨论(0)
提交回复
热议问题