Created
December 9, 2011 01:55
-
-
Save Redth/1449755 to your computer and use it in GitHub Desktop.
Send SMS to Contact using Xamarin.Mobile in Mono for Android!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void button_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); | |
} | |
public override void OnActivityResult(int requestCode, Result resultCode, Intent data) | |
{ | |
//See if we are handling the contact picker request code | |
// and that the result code is ok! | |
if (requestCode == 101 && resultCode == Result.Ok) | |
{ | |
//Ensure we have data returned | |
if (data == null || data.Data == null) | |
return; | |
//Xamarin.Mobile code here :) | |
var addressBook = new Xamarin.Contacts.AddressBook(this); | |
//Note: This is important | |
addressBook.PreferContactAggregation = true; | |
//Load the contact via the android contact id | |
// in the last segment of the Uri returned by the | |
// android contact picker | |
var contact = addressBook.Load(data.Data.LastPathSegment); | |
//Use linq to find a mobile number | |
var mobile = (from p in contact.Phones where | |
p.Type == Xamarin.Contacts.PhoneType.Mobile | |
select p.Number).FirstOrDefault(); | |
//See if the contact has a mobile number | |
if (string.IsNullOrEmpty(mobile)) | |
{ | |
Toast.MakeText(this, "No Mobile Number for contact!", | |
ToastLength.Short).Show(); | |
return; | |
} | |
//Send SMS! | |
var smsMgr = Android.Telephony.SmsManager.Default; | |
smsMgr.SendTextMessage(mobile, null, "Hello World!", null, null); | |
} | |
} |
thank you very much!!!!
The following error occurs for linq statement: Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object
it is not work sir.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
thanks , that was helpful ;)