2015-02-07 82 views
0

我在Xamarin中開發c#並試圖獲取聯繫人的號碼。我試過這樣:獲取聯繫人的號碼

private static int PICK_NUMAR = 1; 

    private void Click (object sender , EventArgs eventArgs) 
     { 
      Intent intent = new Intent (Intent.ActionPick , Android.Net.Uri.Parse ("content://contacts")); 
      intent.SetType (ContactsContract.Contacts.ContentType); 
      StartActivityForResult (intent , SMS_Send.PICK_NUMAR); 
     } 
    protected override void OnActivityResult (int requestCode, Result resultCode, Intent data) 
    { 
     if ((requestCode == SMS_Send.PICK_NUMAR) && (resultCode == Result.Ok) && (data != null)) { 

      Android.Net.Uri uri = data.Data; 
      string[] projection = new [] { ContactsContract.CommonDataKinds.Phone.Number }; 

      using (Android.Database.ICursor c = ManagedQuery (uri, projection, null, null, null)) { 
       if (c != null) { 
        string NUMAR_TELEFON = c.GetString (c.GetColumnIndex (ContactsContract.CommonDataKinds.Phone.Number)); 
        c.MoveToFirst(); 
        FindViewById<EditText> (Resource.Id.Input2).Text = NUMAR_TELEFON; 
       } 
      } 
     } 
    } 

但沒有奏效。我試圖進行調試,並且在ICursor的聲明後我看到代碼卡住了。我做錯了什麼?

回答

0

Xamarin Mobile組件具有用於訪問Android和iOS聯繫人一個簡化的API(注意的Android需要在清單中的READ_CONTACTS許可)

using Xamarin.Contacts; 
// ... 

var book = new AddressBook (this); 

if (!await book.RequestPermission()) { 
    Console.WriteLine ("Permission denied by user or manifest"); 
    return; 
} 

foreach (Contact contact in book.OrderBy (c => c.LastName)) { 
    Console.WriteLine ("{0} {1}", contact.FirstName, contact.LastName); 
} 
相關問題