Xamarin Android从特定联系人获取电话号码

时间:2018-06-15 23:20:17

标签: c# xamarin.android phone-number

我现在已经搜索了很长时间,并且找不到合适的解决方案。我想从特定联系人处获取电话号码。用户可以输入联系人姓名,然后单击呼叫以呼叫联系人,但我无法获得电话号码。我希望有人可以帮助我!

到目前为止我已尝试过:

public string result;
    private Intent pickContactIntent;
    public void pickContact()
    {
        pickContactIntent = new Intent(Intent.ActionPick, ContactsContract.Contacts.ContentUri);
        pickContactIntent.SetType(ContactsContract.CommonDataKinds.Phone.ContentType);
        StartActivityForResult(pickContactIntent, 0);
    }

    protected void OnActivityResult(int requestCode, int resultCode, Intent data)
    {
        if(requestCode == 1)
        {
            if(resultCode == (int)Result.Ok)
            {
                result = data.GetType().ToString();
            }
        }
    }

else if(requestCode == REQUEST_CONTACT) {
    Uri contactsURI = data.getData();

    String[] queryFields = new String[]{
            ContactsContract.Contacts.DISPLAY_NAME
    };


    //Perform your query - the contactURI is like a "where"
    //clause here
    Cursor c = getActivity().getContentResolver().query(contactsURI, queryFields, null, null, null);


    //Double-check that you actually got results
    if (c.getCount() == 0) {
        c.close();
        return;
    }
    contactID = c.getString(c.getColumnIndex(ContactsContract.Contacts._ID));

    //Pull out the first column of the first row of data
    //that is your suspects name
    c.moveToFirst();

    String suspect = c.getString(0);


    mCrime.setmSuspect(suspect);
    mSuspectButton.setText(suspect);
    c.close();

}

0 个答案:

没有答案