Skip to content

Instantly share code, notes, and snippets.

@chrisntr
Created October 16, 2011 21:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chrisntr/61dbd32792c20cca72c0 to your computer and use it in GitHub Desktop.
Save chrisntr/61dbd32792c20cca72c0 to your computer and use it in GitHub Desktop.
[BroadcastReceiver(Enabled = true)]
[IntentFilter(new string[] { "android.provider.Telephony.SMS_RECEIVED"})]
public class SMSReceiver : Android.Content.BroadcastReceiver
{
protected string logTag = "SMSExample";
public static readonly string INTENT_ACTION = "android.provider.Telephony.SMS_RECEIVED";
public override void OnReceive(Context context, Intent intent)
{
if (intent.Action == INTENT_ACTION)
{
Bundle bundle = intent.Extras;
if (bundle != null)
{
var pdus = bundle.Get ("pdus");
Java.Lang.Object[] castedPdus = JNIEnv.GetArray<Java.Lang.Object>(pdus.Handle);
var msgs = new SmsMessage[castedPdus.Length];
var str = string.Empty;
for( int i = 0; i < msgs.Length; i++)
{
// Works
//var bytes = new Byte[JNIEnv.GetArrayLength(castedPdus[i].Handle)];
//JNIEnv.CopyArray(castedPdus[i].Handle, bytes);
// Fails
//var bytes = JNIEnv.GetArray<Byte>(castedPdus[i].Handle);
msgs[i] = SmsMessage.CreateFromPdu(bytes);
str = "SMS from " + msgs[i].OriginatingAddress;
str += " :";
str += msgs[i].MessageBody.ToString();
str += "\n";
}
Toast.MakeText(context, str, ToastLength.Short).Show();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment