Android以编程方式从联系人获取设备默认电话号码

Posted

技术标签:

【中文标题】Android以编程方式从联系人获取设备默认电话号码【英文标题】:Android programatically getting device default phone number from contacts 【发布时间】:2015-04-19 09:51:15 【问题描述】:

我通过使用这种方法从 android“我的”联系人中获得了姓名和联系人 ID

                 String[] columnNames = new String[]  Phone._ID,
                        Phone.DISPLAY_NAME ;

                Cursor c = LoginActivity.this.getContentResolver().query(
                        ContactsContract.Profile.CONTENT_URI, columnNames,
                        null, null, null);
                int count = c.getCount();
                boolean b = c.moveToFirst();
                int position = c.getPosition();
                if (count == 1 && position == 0) 
                    for (int j = 0; j < columnNames.length; j++) 
                        contact_id = c.getString(0);
                        namee = c.getString(1);


                    
                

如何获取设备的电话号码?

【问题讨论】:

您需要在 columnNames 数组中请求另一列。然后做c.getString(2); 虽然我建议使用实际的字段名而不是索引。 您要保存在 ME 个人资料中的号码还是 sim 号码? @Doomsknight 添加另一列时出现错误 您是否将Phone.NUMBER 添加到ColumnNames @RahulPatil 我想要保存在我的联系人而不是 sim 号码的号码 【参考方案1】:

在清单中添加以下权限。

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

使用以下代码获取“我”的联系方式。

    public Loader<Cursor> onCreateLoader(int id, Bundle arguments) 
    return new CursorLoader(this,
            // Retrieve data rows for the device user's 'profile' contact.
            Uri.withAppendedPath( ContactsContract.Profile.CONTENT_URI,ContactsContract.Contacts.Data.CONTENT_DIRECTORY),
            ProfileQuery.PROJECTION,

            //Don't select anything here null will return all available fields

            null,
            null,                   
            null);

@Override
public void onLoadFinished(Loader<Cursor> cursorLoader, Cursor cursor) 
    ArrayList<String> DataArray = new ArrayList<String>();
    cursor.moveToFirst();
    while (!cursor.isAfterLast()) 
                //here where you get your data and its type
        TypeName=cursor.getString(ProfileQuery.ADDRESS);//this will give you field name
        Data=cursor.getString(ProfileQuery.NUMBER);//this will give you field data

        cursor.moveToNext();
    

@Override
public void onLoaderReset(Loader<Cursor> cursorLoader) 

private interface ProfileQuery 
    String[] PROJECTION = 
            ContactsContract.Contacts.Data.MIMETYPE,
            ContactsContract.CommonDataKinds.Email.ADDRESS  ,
            ContactsContract.CommonDataKinds.Email.DISPLAY_NAME,
            ContactsContract.CommonDataKinds.Organization.DATA3,

    ;

    int ADDRESS = 0;
    int NUMBER = 1;

【讨论】:

【参考方案2】:

试试这个:

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

在清单中:

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

【讨论】:

我不想要所有的联系人只是我的联系人 很抱歉我没有做对。你只想要你的电话号码? 是的,来自我自己的联系方式【参考方案3】:

这是正确的做法

1) 调用意图

Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
if(intent.resolveActivity(getActivity().getPackageManager()) != null) 
    getParentFragment().startActivityForResult(intent, CONTACT_REQUEST_CODE);
 

2) 实现onActivityResult

@Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) 
        super.onActivityResult(requestCode, resultCode, data);

        if (resultCode == Activity.RESULT_OK) 

            if (requestCode == CONTACT_REQUEST_CODE) 

                uriContact = data.getData();

                // replaceAll("\\D", "") : Remove everything that is not a digit
                String number = retrieveContactNumber();
                if (Connectivity.isConnected(getActivity())) 
                    if (number != null) 
                        String contact_number = number.replaceAll("\\D", "");
                     
                
            
      

3) 实现retrieveContactNumber()

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

        // Using the contact ID to 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));
        

        cursorPhone.close();

        return contactNumber;
    

4) 享受 :)

【讨论】:

它适用于我需要我联系的所有联系人,无需选择 我自己在 sdk >14 android 手机中联系【参考方案4】:

试试这个……

String[] columnNames = new String[]  Phone._ID,
                            Phone.DISPLAY_NAME ;

        Cursor c = LoginActivity.this.getContentResolver().query(
                            ContactsContract.Profile.CONTENT_URI, columnNames,
                            null, null, null);
         int count = c.getCount();
         boolean b = c.moveToFirst();
         int position = c.getPosition();
         if (count == 1 && position == 0) 
         for (int j = 0; j < columnNames.length; j++) 
             contact_id = c.getString(0);
             namee = c.getString(1); 
      if(Integer.parseInt(c.getString(c.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) 

                  Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                                    null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID+ " = ?",new String[]  contact_id  , null);


              while (pCur.moveToNext()) 
                  String noo = pCur.getString(pCur.getColumnIndex(CommonDataKinds.Phone.NUMBER));
                
          
     
                     

【讨论】:

cr 和 pcurgetcolumnindex 是什么? pCur 用于通过contact_id获取电话号码。 forgate 在 pCur 和 getcolumnindex 之间添加点,现在可以更改 你试过这个吗?我得到了 Illegalstateexception。 让我们continue this discussion in chat.【参考方案5】:

我认为您应该使用ContactsContract.CommonDataKinds.Phone.CONTENT_URI,并且有一个名为ContactsContract.CommonDataKinds.Phone.NUMBER 的列,您可以使用它获取联系电话。

看看这段代码

public ArrayList<String> getContactList()

         ArrayList<String> list= new ArrayList<String>();

            Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
            String[] columns    = new String[] ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
                            ContactsContract.CommonDataKinds.Phone.NUMBER;

            Cursor people = getContentResolver().query(uri, columns, null, null, null);

            int _nameIndex = people.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
            int _numberIndex = people.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);

            people.moveToFirst();
            do 
                String name   = people.getString(_nameIndex);
                String number = people.getString(_numberIndex);
                list.add(name+" :"+number);
                // Do work...
             while (people.moveToNext());

        return list;

【讨论】:

【参考方案6】:

这对我有用。

            String[] columnNames = new String[]  Phone._ID,
                    Phone.DISPLAY_NAME, Phone.NUMBER ;

            Cursor c = LoginActivity.this.getContentResolver().query(
                    ContactsContract.Profile.CONTENT_URI, columnNames,
                    null, null, null);
            int count = c.getCount();
            boolean b = c.moveToFirst();
            int position = c.getPosition();
            if (count == 1 && position == 0) 
                for (int j = 0; j < columnNames.length; j++) 
                    contact_id = c.getString(0);
                    name = c.getString(1);
                    number= c.getString(2);
                
            

【讨论】:

在清单中添加这个。

以上是关于Android以编程方式从联系人获取设备默认电话号码的主要内容,如果未能解决你的问题,请参考以下文章

如何从 Android 设备中仅获取电话号码联系人

如何以编程方式在android 10中恢复已删除的联系人

获取 Android 手机联系人的跨设备唯一 ID

如何从电话簿中获取与 android 中特定联系人相关联的所有社交帐户?

android以编程方式编辑联系人

如何以编程方式修改通讯录中的联系电话?