未在设备上调用OnActivityResult

时间:2014-10-20 06:23:44

标签: android xamarin onactivityresult

我尝试从联系人列表中选择联系人:

contactsButton.Click += (object sender, EventArgs e) => 
            {
                //Create a new intent for choosing a contact  
                var contactPickerIntent = new Intent(Intent.ActionPick, Android.Provider.ContactsContract.Contacts.ContentUri);
                //Start the contact picker expecting a result  
                // with the resultCode '101'  
                StartActivityForResult(contactPickerIntent, 101);
            };

和方法 OnActivityResult 在xamarin调试器下调用,而不是在我的真实设备(SGS4)上调用。 当用户取消联系人列表中的操作时,在调试器和实际设备下调用方法 OnActivityResults

protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
        {
            phoneNumberText.Text = String.Format("request={0}, result={1}", requestCode, resultCode);
            base.OnActivityResult(requestCode, resultCode, data);
            ...
         }

更新: 我不明白为什么,但下一行解决了这个问题:contactPickerIntent.SetType(ContactsContract.CommonDataKinds.Phone.ContentType);

    contactsButton.Click += (object sender, EventArgs e) => 
    {
        //Create a new intent for choosing a contact  
        var contactPickerIntent = new Intent(Intent.ActionPick, Android.Provider.ContactsContract.Contacts.ContentUri);
        contactPickerIntent.SetType(ContactsContract.CommonDataKinds.Phone.ContentType);//(Phone.CONTENT_TYPE); // Show user only contacts w/ phone numbers
        //Start the contact picker expecting a result  
        // with the resultCode '101'  
        StartActivityForResult(contactPickerIntent, 101);
    };

2 个答案:

答案 0 :(得分:1)

contactPickerIntent.SetType(...) 以某种方式解决了这个问题。

    contactsButton.Click += (object sender, EventArgs e) => 
    {
        //Create a new intent for choosing a contact  
        var contactPickerIntent = new Intent(Intent.ActionPick, Android.Provider.ContactsContract.Contacts.ContentUri);
        contactPickerIntent.SetType(ContactsContract.CommonDataKinds.Phone.ContentType);//(Phone.CONTENT_TYPE); // Show user only contacts w/ phone numbers
        //Start the contact picker expecting a result  
        // with the resultCode '101'  
        StartActivityForResult(contactPickerIntent, 101);
    };

答案 1 :(得分:-1)

我从未使用过xamarin但是在Android开发方面尝试将onActivityResult代码更改为:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data){
        ....
}