-
-
Save chrisntr/61dbd32792c20cca72c0 to your computer and use it in GitHub Desktop.
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
[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