我试图获得SIM卡和手机通讯录。我通过电话联系人(电话联系人和同步联系人)取得了成功,但是通过SIM卡联系我失败了...... 我尝试了很多URI,但没有人工作,所以我认为你们可以帮助我。
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 uriContact = data.Data;
ICursor cursor = ManagedQuery (uriContact,null,null,null,null);
if (cursor.MoveToFirst ()) {
string NUME_CONTACT = cursor.GetString (cursor.GetColumnIndex(ContactsContract.Contacts.InterfaceConsts.DisplayName));
string NUMAR_TELEFON = getPhoneNumber (NUME_CONTACT);
ISharedPreferences NumeNumar = GetPreferences (FileCreationMode.Private);
ISharedPreferencesEditor Editor = NumeNumar.Edit ();
Editor.PutString ("NUMAR_TELEFON" , NUMAR_TELEFON);
Editor.PutString ("NUME_CONTACT" , NUME_CONTACT);
Editor.Apply ();
}
}
}
public string getPhoneNumber(string name)
{
string ret = null;
string selection = ContactsContract.Contacts.InterfaceConsts.DisplayName +" like '%" + name +"%'";
string[] projection = new string[] { ContactsContract.CommonDataKinds.Phone.Number };
ICursor c = ManagedQuery (ContactsContract.CommonDataKinds.Phone.ContentUri,projection,selection,null,null);
if (c.MoveToFirst ()) {
ret = c.GetString (0);
}
return ret;
}
提前谢谢!
答案 0 :(得分:0)
这项工作适合我!
public class MyContacts {
public static String getContacts(Context context){
StringBuffer outBuffer = new StringBuffer();
outBuffer.append("");
String strOrder = ContactsContract.Contacts.DISPLAY_NAME + " ASC";
ContentResolver cr = context.getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,null, null, null, strOrder);
outBuffer.append("<contacts>");
if (cur.getCount() > 0 ) {
while (cur.moveToNext() ) {
outBuffer.append("<contact>").
append("<fullname>").
append(ServerData.getStartcdataxml() + cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)) + ServerData.getEndcdataxml()).
append("</fullname>").
append("<id>").
append(cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID))).
append("</id>").
append("</contact>");
}
cur.close();
}
outBuffer.append("</contacts>");
return outBuffer.toString();
}
public static String getContactByID(Context context,String contact_id){
StringBuffer outBuffer = new StringBuffer();
outBuffer.append("");
//Long inizioLettura = System.currentTimeMillis();
Uri CONTENT_URI = ContactsContract.Contacts.CONTENT_URI;
String HAS_PHONE_NUMBER = ContactsContract.Contacts.HAS_PHONE_NUMBER;
Uri PhoneCONTENT_URI = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
String Phone_CONTACT_ID = ContactsContract.CommonDataKinds.Phone.CONTACT_ID;
String NUMBER = ContactsContract.CommonDataKinds.Phone.NUMBER;
Uri EmailCONTENT_URI = ContactsContract.CommonDataKinds.Email.CONTENT_URI;
String EmailCONTACT_ID = ContactsContract.CommonDataKinds.Email.CONTACT_ID;
String DATA = ContactsContract.CommonDataKinds.Email.DATA;
Uri AdressCONTENT_URI = ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_URI;
String AdressCONTACT_ID = ContactsContract.CommonDataKinds.StructuredPostal.CONTACT_ID;
ContentResolver contentResolver = context.getContentResolver();
Cursor cursor = contentResolver.query(CONTENT_URI, null,null, null, null);
if (cursor.getCount() > 0) {
cursor.moveToNext();
int hasPhoneNumber = Integer.parseInt(cursor.getString(cursor.getColumnIndex( HAS_PHONE_NUMBER )));
outBuffer.append("<contact>");
if (hasPhoneNumber > 0) {
outBuffer.append("<phonenumbers>");
Cursor phoneCursor = contentResolver.query(PhoneCONTENT_URI, null, Phone_CONTACT_ID + " = ?", new String[] { contact_id }, null);
if(phoneCursor != null)
while (phoneCursor.moveToNext()) {
outBuffer.append("<phonenumber>").
append("<number>").
append(phoneCursor.getString(phoneCursor.getColumnIndex(NUMBER))).
append("</number>");
if(phoneCursor.getString(phoneCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE)) != null){
switch (Integer.valueOf(phoneCursor.getString(phoneCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE)))) {
case ContactsContract.CommonDataKinds.Phone.TYPE_HOME:outBuffer.append("<type>").append("home").append("</type>"); break;
case ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE:outBuffer.append("<type>").append("mobile").append("</type>"); break;
case ContactsContract.CommonDataKinds.Phone.TYPE_WORK:outBuffer.append("<type>").append("work").append("</type>"); break;
case ContactsContract.CommonDataKinds.Phone.TYPE_FAX_WORK:outBuffer.append("<type>").append("work fax").append("</type>"); break;
case ContactsContract.CommonDataKinds.Phone.TYPE_FAX_HOME:outBuffer.append("<type>").append("home fax").append("</type>"); break;
case ContactsContract.CommonDataKinds.Phone.TYPE_PAGER:outBuffer.append("<type>").append("pager").append("</type>"); break;
case ContactsContract.CommonDataKinds.Phone.TYPE_OTHER:outBuffer.append("<type>").append("other").append("</type>"); break;
case ContactsContract.CommonDataKinds.Phone.TYPE_CUSTOM:outBuffer.append("<type>").append("custom").append("</type>"); break;
case ContactsContract.CommonDataKinds.Phone.TYPE_CALLBACK:outBuffer.append("<type>").append("callback").append("</type>"); break;
case ContactsContract.CommonDataKinds.Phone.TYPE_CAR:outBuffer.append("<type>").append("car").append("</type>"); break;
case ContactsContract.CommonDataKinds.Phone.TYPE_COMPANY_MAIN:outBuffer.append("<type>").append("company main").append("</type>"); break;
case ContactsContract.CommonDataKinds.Phone.TYPE_ISDN:outBuffer.append("<type>").append("ISDN").append("</type>"); break;
case ContactsContract.CommonDataKinds.Phone.TYPE_MAIN:outBuffer.append("<type>").append("main").append("</type>"); break;
case ContactsContract.CommonDataKinds.Phone.TYPE_ASSISTANT:outBuffer.append("<type>").append("assistant").append("</type>"); break;
case ContactsContract.CommonDataKinds.Phone.TYPE_MMS:outBuffer.append("<type>").append("mms").append("</type>"); break;
case ContactsContract.CommonDataKinds.Phone.TYPE_OTHER_FAX:outBuffer.append("<type>").append("other fax").append("</type>"); break;
case ContactsContract.CommonDataKinds.Phone.TYPE_RADIO:outBuffer.append("<type>").append("radio").append("</type>"); break;
case ContactsContract.CommonDataKinds.Phone.TYPE_TTY_TDD:outBuffer.append("<type>").append("TTY TDD").append("</type>"); break;
case ContactsContract.CommonDataKinds.Phone.TYPE_WORK_PAGER:outBuffer.append("<type>").append("workpager").append("</type>"); break;
}
outBuffer.append("</phonenumber>");
}
}
outBuffer.append("</phonenumbers>");
phoneCursor.close();
}
Cursor emailCursor = contentResolver.query(EmailCONTENT_URI, null, EmailCONTACT_ID+ " = ?", new String[] { contact_id }, null);
outBuffer.append("<emails>");
if(emailCursor != null)
while (emailCursor.moveToNext()) {
outBuffer.append("<infoemail>").
append("<email>").
append(ServerData.getStartcdataxml() + emailCursor.getString(emailCursor.getColumnIndex(DATA)) + ServerData.getEndcdataxml()).
append("</email>");
if(emailCursor.getString(emailCursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.TYPE))!= null){
switch (Integer.valueOf(emailCursor.getString(emailCursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.TYPE)))) {
case ContactsContract.CommonDataKinds.Email.TYPE_HOME:outBuffer.append("<type>").append("home").append("</type>"); break;
case ContactsContract.CommonDataKinds.Email.TYPE_MOBILE:outBuffer.append("<type>").append("mobile").append("</type>"); break;
case ContactsContract.CommonDataKinds.Email.TYPE_WORK:outBuffer.append("<type>").append("work").append("</type>"); break;
case ContactsContract.CommonDataKinds.Email.TYPE_OTHER:outBuffer.append("<type>").append("other").append("</type>"); break;
case ContactsContract.CommonDataKinds.Email.TYPE_CUSTOM:outBuffer.append("<type>").append("custom").append("</type>"); break;
}
}
outBuffer.append("</infoemail>");
}
outBuffer.append("</emails>");
emailCursor.close();
Cursor adressCursor = contentResolver.query(AdressCONTENT_URI, null, AdressCONTACT_ID+ " = ?", new String[] { contact_id }, null);
outBuffer.append("<addresses>");
if(adressCursor!=null)
while (adressCursor.moveToNext()) {
outBuffer.append("<address>").
append("<street>").
append(ServerData.getStartcdataxml() + adressCursor.getString(adressCursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.STREET)) + ServerData.getEndcdataxml()).
append("</street>").
append("<city>").
append(ServerData.getStartcdataxml() + adressCursor.getString(adressCursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.CITY)) + ServerData.getEndcdataxml()).
append("</city>").
append("<country>").
append(ServerData.getStartcdataxml() + adressCursor.getString(adressCursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.COUNTRY)) + ServerData.getEndcdataxml()).
append("</country>").
append("<postalcode>").
append(ServerData.getStartcdataxml() + adressCursor.getString(adressCursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.POSTCODE)) + ServerData.getEndcdataxml()).
append("</postalcode>").
append("<neighborhood>").
append(ServerData.getStartcdataxml() + adressCursor.getString(adressCursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.NEIGHBORHOOD)) + ServerData.getEndcdataxml()).
append("</neighborhood>");
if(adressCursor.getString(adressCursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.TYPE)) != null){
switch (Integer.valueOf(adressCursor.getString(adressCursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.TYPE)))) {
case ContactsContract.CommonDataKinds.StructuredPostal.TYPE_HOME:outBuffer.append("<type>").append("home").append("</type>"); break;
case ContactsContract.CommonDataKinds.StructuredPostal.TYPE_WORK:outBuffer.append("<type>").append("work").append("</type>"); break;
case ContactsContract.CommonDataKinds.StructuredPostal.TYPE_OTHER:outBuffer.append("<type>").append("other").append("</type>"); break;
case ContactsContract.CommonDataKinds.StructuredPostal.TYPE_CUSTOM:outBuffer.append("<type>").append("custom").append("</type>"); break;
}
}
outBuffer.append("</address>");
}
outBuffer.append("</addresses>");
Cursor orgCursor = contentResolver.query(ContactsContract.Data.CONTENT_URI, null, ContactsContract.Data.CONTACT_ID + " = ? AND " + ContactsContract.Data.MIMETYPE + " = ?",new String[]{contact_id,
ContactsContract.CommonDataKinds.Organization.CONTENT_ITEM_TYPE}, null);
if(orgCursor!=null)
while (orgCursor.moveToNext()) {
outBuffer.append("<organization>").
append("<company>").
append(ServerData.getStartcdataxml() + orgCursor.getString(orgCursor.getColumnIndex(ContactsContract.CommonDataKinds.Organization.COMPANY)) + ServerData.getEndcdataxml()).
append("</company>").
append("<title>").
append(ServerData.getStartcdataxml() + orgCursor.getString(orgCursor.getColumnIndex(ContactsContract.CommonDataKinds.Organization.TITLE)) + ServerData.getEndcdataxml()).
append("</title>").
append("</organization>");
}
orgCursor.close();
outBuffer.append("</contact>");
adressCursor.close();
}
return outBuffer.toString();
}
}
答案 1 :(得分:0)
最后我发现了问题..我不得不用以下方法覆盖意图类型:
intent.SetType (ContactsContract.CommonDataKinds.Phone.ContentType);