2017-08-11 84 views
1

我试图使用内置Android意图引用here让用户更新联系人。但是,目前在调用我的UpdateContact函数时,没有任何反应,并且在我调试时日志中没有错误。如果将其更改为StartActivityForResult,结果将被取消。任何帮助表示赞赏Xamarin Android编辑联系人意图

此外,contactUri的价值是: “内容://com.android.contacts/data/4”

MainActivity.cs:

Intent editIntent = new Intent(Intent.ActionEdit); 
editIntent.SetDataAndType(Android.Net.Uri.Parse(contactUri), ContactsContract.Contacts.ContentItemType); 
editIntent.PutExtra("finishActivityOnSaveCompleted", true); 
var activity = Xamarin.Forms.Forms.Context as Activity; 
activity.StartActivity(editIntent); 

回答

1

对于那些谁也可能会被卡住,经过大量的试验和错误之后,我终于得到了这个工作。以下是我的Android代码,可以按照预期正确完成此功能。

注意:contactId是一个包含适当Uri的字符串。我得到了我的,因为我有用户选择一个联系人使用不同的意图,该意图返回该Uri作为OnActivityResult中数据的一部分。您也可以使用Xamarin.Mobile或Android游标查询这些信息。

代码:

var activity = Xamarin.Forms.Forms.Context as Activity; 
Android.Net.Uri mUri = Android.Net.Uri.Parse(contactId); 
// The Cursor that contains the Contact row 
var mCursor = activity.ContentResolver.Query(mUri, null, null, null, null); 
mCursor.MoveToFirst(); 
// The index of the lookup key column in the cursor 
int mLookupKeyIndex; 
// The index of the contact's _ID value 
int mIdIndex; 
// The lookup key from the Cursor 
string mCurrentLookupKey; 
// The _ID value from the Cursor 
long mCurrentId; 
// A content URI pointing to the contact 
Android.Net.Uri mSelectedContactUri; 

/* 
* Once the user has selected a contact to edit, 
* this gets the contact's lookup key and _ID values from the 
* cursor and creates the necessary URI. 
*/ 
// Gets the lookup key column index 
mLookupKeyIndex = mCursor.GetColumnIndex(ContactsContract.Contacts.InterfaceConsts.LookupKey); 
// Gets the lookup key value 
mCurrentLookupKey = mCursor.GetString(mLookupKeyIndex); 
// Gets the _ID column index 
mIdIndex = mCursor.GetColumnIndex(ContactsContract.Contacts.InterfaceConsts.Id); 
mCurrentId = mCursor.GetLong(mIdIndex); 
mSelectedContactUri = ContactsContract.Contacts.GetLookupUri(mCurrentId, mCurrentLookupKey); 

// Creates a new Intent to edit a contact 
Intent editIntent = new Intent(Intent.ActionEdit); 
/* 
* Sets the contact URI to edit, and the data type that the 
* Intent must match 
*/ 
editIntent.SetDataAndType(mSelectedContactUri, ContactsContract.Contacts.ContentItemType); 
// Sets the special extended data for navigation 
editIntent.PutExtra("finishActivityOnSaveCompleted", true); 
activity.StartActivity(editIntent);