Xamarin 访问通讯录以获取信息
Xamarin Access to contact book to get informations
我正在 Android (Xamarin) 上开发一个应用程序,这是我想做的事情:
当用户点击一个按钮时,他可以在通讯录中select一个联系人,然后在2个editText中写入姓名和phone号码(姓名和phone数)
我怎样才能访问通讯录并获取我需要的数据?
这是我现在的代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.Preferences;
using Android.Provider;
namespace EmergencyCall
{
[Activity(Label = "AddContact")]
public class AddContact : Activity
{
EditText nom;
EditText numero;
Button save;
Button addFromReper;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.add_Contact);
nom = FindViewById<EditText>(Resource.Id.nomPersonne);
numero = FindViewById<EditText>(Resource.Id.NumeroTel);
save = FindViewById<Button>(Resource.Id.saveNewContact);
addFromReper = FindViewById<Button>(Resource.Id.addFromRepert);
ISharedPreferences prefs;
ISharedPreferencesEditor editor;
prefs = PreferenceManager.GetDefaultSharedPreferences(ApplicationContext);
editor = prefs.Edit();
save.Click += delegate
{
ISet<String> names = new HashSet<String> (prefs.GetStringSet("NomsContacts", new HashSet<String>()));
names.Add(nom.Text);
editor.PutStringSet("NomsContacts", names);
editor.PutString(nom.Text, numero.Text);
editor.Apply();
StartActivity(typeof(ModifyContacts));
};
addFromReper.Click += delegate
{
Intent intent = new Intent(Intent.ActionPick, Contacts.ContentUri);
StartActivityForResult(intent, 1);
};
}
}
}
谢谢:)
如果有人需要,我终于找到了怎么做:
protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
{
base.OnActivityResult(requestCode, resultCode, data);
if (requestCode == 1)
{
if (resultCode == Result.Ok)
{
Android.Net.Uri contactData = data.Data;
ICursor cursor = ContentResolver.Query(contactData, null, null, null, null);
cursor.MoveToFirst();
string name = cursor.GetString(cursor.GetColumnIndexOrThrow(ContactsContract.Contacts.InterfaceConsts.DisplayName));
string number = cursor.GetString(cursor.GetColumnIndexOrThrow(Android.Provider.ContactsContract.CommonDataKinds.Phone.Number));
nom.Text=name;
numero.Text = number;
}
}
我正在 Android (Xamarin) 上开发一个应用程序,这是我想做的事情:
当用户点击一个按钮时,他可以在通讯录中select一个联系人,然后在2个editText中写入姓名和phone号码(姓名和phone数)
我怎样才能访问通讯录并获取我需要的数据?
这是我现在的代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.Preferences;
using Android.Provider;
namespace EmergencyCall
{
[Activity(Label = "AddContact")]
public class AddContact : Activity
{
EditText nom;
EditText numero;
Button save;
Button addFromReper;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.add_Contact);
nom = FindViewById<EditText>(Resource.Id.nomPersonne);
numero = FindViewById<EditText>(Resource.Id.NumeroTel);
save = FindViewById<Button>(Resource.Id.saveNewContact);
addFromReper = FindViewById<Button>(Resource.Id.addFromRepert);
ISharedPreferences prefs;
ISharedPreferencesEditor editor;
prefs = PreferenceManager.GetDefaultSharedPreferences(ApplicationContext);
editor = prefs.Edit();
save.Click += delegate
{
ISet<String> names = new HashSet<String> (prefs.GetStringSet("NomsContacts", new HashSet<String>()));
names.Add(nom.Text);
editor.PutStringSet("NomsContacts", names);
editor.PutString(nom.Text, numero.Text);
editor.Apply();
StartActivity(typeof(ModifyContacts));
};
addFromReper.Click += delegate
{
Intent intent = new Intent(Intent.ActionPick, Contacts.ContentUri);
StartActivityForResult(intent, 1);
};
}
}
}
谢谢:)
如果有人需要,我终于找到了怎么做:
protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
{
base.OnActivityResult(requestCode, resultCode, data);
if (requestCode == 1)
{
if (resultCode == Result.Ok)
{
Android.Net.Uri contactData = data.Data;
ICursor cursor = ContentResolver.Query(contactData, null, null, null, null);
cursor.MoveToFirst();
string name = cursor.GetString(cursor.GetColumnIndexOrThrow(ContactsContract.Contacts.InterfaceConsts.DisplayName));
string number = cursor.GetString(cursor.GetColumnIndexOrThrow(Android.Provider.ContactsContract.CommonDataKinds.Phone.Number));
nom.Text=name;
numero.Text = number;
}
}