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

时间:2015-02-18 11:47:18

标签: android contacts android-contacts phone-number

我从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);


                    }
                }

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

6 个答案:

答案 0 :(得分:1)

在清单中添加以下权限。

private void PopupCalcEdit_MouseDown(object sender, MouseButtonEventArgs e)
{
    var calcEdit = sender as PopupCalcEdit;

    if (calcEdit == null)
        return;

    calcEdit.EditMode = EditMode.InplaceActive;
}

private void PopupCalcEdit_LostFocus(object sender, RoutedEventArgs e)
{
    var calcEdit = sender as PopupCalcEdit;

    if (calcEdit == null && calcEdit.EditMode != EditMode.InplaceActive)
        return;

    var cellsArea = calcEdit.DataContext as CellsAreaItem;

    if (cellsArea == null)
        return;

    var dataTable = (DataTable)PivotGridControl.DataSource;

    var drillSource = PivotGridControl.CreateDrillDownDataSource(cellsArea.ColumnIndex, cellsArea.RowIndex);

    if (drillSource.RowCount > 0)
    {
        int id = (int)drillSource.GetValue(0, "ID"); //Change only the first row in drilled rows.

        dataTable.Rows.Find(id)["amount"] = calcEdit.EditValue;
    }
    else
    {
        var cellInfo = PivotGridControl.GetCellInfo(cellsArea.ColumnIndex, cellsArea.RowIndex);

        object itemValue = cellInfo.GetFieldValue(PivotGridControl.Fields["item"]);
        object nameValue = cellInfo.GetFieldValue(PivotGridControl.Fields["name"]);

        var row = dataTable.NewRow();
        row["item"] = itemValue;
        row["name"] = nameValue;
        row["amount"] = calcEdit.EditValue;

        dataTable.Rows.Add(row);
    }

    calcEdit.EditMode = EditMode.InplaceInactive;

    PivotGridControl.ReloadData();
}

使用以下代码获取&#34;我&#34;接触。

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

答案 1 :(得分:0)

试试这个:

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" />

答案 2 :(得分:0)

试试这个......

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

答案 3 :(得分:0)

这是正确的做法

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)享受:)

答案 4 :(得分:0)

我认为您应该使用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;
}

答案 5 :(得分:0)

这对我有用。

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