【问题标题】:Xamarin Android Edit Contact IntentXamarin Android 编辑联系人意图
【发布时间】:2017-08-11 00:33:36
【问题描述】:

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

另外,contactUri 的值为:“content://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);

【问题讨论】:

    标签: android-intent xamarin.android


    【解决方案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);
    

    【讨论】:

      猜你喜欢
      • 2012-12-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多