Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
[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