Send SMS in C# with Xamarin MonoForAndroid

Android_robot.svg I’ve started to develop android app’s in C# with the ubber great Xamarin MonoForAndroid framework which allow us to develop android apps in C#/.Net. If you don’t know it yet, take a look at their website, a truly great product 🙂
Anyway, I just share to you a little code snippet to send SMS from your MonoForAndroid app’s with two different mode. The first one, “Direct mode”, directly send the sms without asking the user to take a decision. The second one, based on android intent, will request the user to pick a sending service (i.e. Sms, Facebook, LinkedIn, Twitter), and will start the selected apps.

So to send the sms in direct mode, we just need these two lines of code :

                    PendingIntent pendingIntent = PendingIntent.GetActivity(this, 0, new Intent(this, typeof(/*Type of the activity*/)), 0);
                    SmsManager.Default.SendTextMessage(number, null, textToSend, pendingIntent, null);

And then, the intent mode, not more complex, but require few additional lines :

            var smsUri = Android.Net.Uri.Parse(String.Format("smsto:{0}", number));
            var smsIntent = new Intent(Intent.ActionSendto, smsUri);
            smsIntent.PutExtra("sms_body", textToSend);
            smsIntent.AddFlags(ActivityFlags.NewTask);
            StartActivity(smsIntent);

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s