以下代码参考网上代码改写,链接http://blog.csdn.net/jason0539/article/details/21855825
首先需要添加如下权限:
<uses-permission android:name="android.permission.READ_CONTACTS" />
public class MainActivity : Activity
{
TextView tvPhone;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.Main);
tvPhone = FindViewById<TextView>(Resource.Id.tvPhone);
Button btnSend = FindViewById<Button>(Resource.Id.btnSend);
btnSend.Click += delegate { };
var intent = new Intent(Intent.ActionPick, ContactsContract.Contacts.ContentUri);
this.StartActivityForResult(intent, 100);
}
protected override void OnActivityResult(int requestCode, [GeneratedEnum] Result resultCode, Intent data)
{
//base.OnActivityResult(requestCode, resultCode, data);
if(requestCode == 100)
{
if(data != null)
{
//tvPhone.Text = GetName(data.Data);
tvPhone.Text = GetPhone(data.Data);
}
}
}
/// <summary>
/// 获取联系人姓名
/// </summary>
/// <param name="uri"></param>
/// <returns></returns>
private string GetName(Android.Net.Uri uri)
{
string result = "";
var cursor = ContentResolver.Query(uri, null, null, null, null);
if (cursor.Count > 0)
{
cursor.MoveToFirst();
var index = cursor.GetColumnIndex(ContactsContract.ContactsColumns.DisplayName);
result = cursor.GetString(index);
}
return result;
}
/// <summary>
/// 获取联系人手机号码
/// </summary>
/// <param name="uri"></param>
/// <returns></returns>
private string GetPhone(Android.Net.Uri uri)
{
string result = "";
var cursor = ContentResolver.Query(uri, null, null, null, null);
if (cursor.Count > 0)
{
cursor.MoveToFirst();
int phoneColumn = cursor.GetColumnIndex(ContactsContract.Contacts.InterfaceConsts.HasPhoneNumber);
int phoneNum = cursor.GetInt(phoneColumn);
if (phoneNum > 0)
{
// 获得联系人的ID号
int idColumn = cursor.GetColumnIndex(ContactsContract.Contacts.InterfaceConsts.Id);
String contactId = cursor.GetString(idColumn);
// 获得联系人电话的cursor
ICursor phone = ContentResolver.Query(ContactsContract.CommonDataKinds.Phone.ContentUri, null,
ContactsContract.CommonDataKinds.Phone.InterfaceConsts.ContactId + "="
+ contactId, null, null);
if (phone.MoveToFirst())
{
for (; !phone.IsAfterLast; phone.MoveToNext())
{
int index = phone.GetColumnIndex(ContactsContract.CommonDataKinds.Phone.Number);
result = phone.GetString(index);
}
}
}
}
return result;
}
发送短消息
添加权限“SEND_SMS”。
在应用程序中使用如下方法发送短信
SmsManager.Default.SendTextMessage ("1234567890", null, "Hello from Xamarin.Android", null, null);