public override void OnReceive(Context context, Intent intent) { if (intent.Action.Equals(IntentAction)) { var bundle = intent.Extras; if (bundle != null) { var pdus = bundle.Get("pdus"); var castedPdus = JNIEnv.GetArray<Java.Lang.Object>(pdus.Handle); var messages = new SmsMessage[castedPdus.Length]; for (int i = 0; i < castedPdus.Length; i++) { var bytes = new byte[JNIEnv.GetArrayLength(castedPdus[i].Handle)]; JNIEnv.CopyArray(castedPdus[i].Handle, bytes); messages[i] = SmsMessage.CreateFromPdu(bytes); } foreach (var message in messages) { string messagefrom = message.DisplayOriginatingAddress; string messagebody = message.MessageBody; } } } //Log.Info(Tag, "Intent received: " + intent.Action); //if (intent.Action != IntentAction) return; //var bundle = intent.Extras; //if (bundle == null) return; //var pdus = bundle.Get("pdus"); //var castedPdus = JNIEnv.GetArray<Java.Lang.Object>(pdus.Handle); //var msgs = new SmsMessage[castedPdus.Length]; //var sb = new StringBuilder(); //for (var i = 0; i < msgs.Length; i++) //{ // var bytes = new byte[JNIEnv.GetArrayLength(castedPdus[i].Handle)]; // JNIEnv.CopyArray(castedPdus[i].Handle, bytes); // msgs[i] = SmsMessage.CreateFromPdu(bytes); // sb.Append(string.Format("SMS From: {0}{1}Body: {2}{1}", msgs[i].OriginatingAddress, System.Environment.NewLine, msgs[i].MessageBody)); // if (Received != null) Received(msgs[i].MessageBody , msgs[i].OriginatingAddress); //} //Toast.MakeText(context, sb.ToString(), ToastLength.Long).Show(); }
public override void OnReceive(Context context, Intent intent) { if (!intent.Action.Equals(IntentAction)) return; var bundle = intent.Extras; if (bundle == null) return; var pdus = bundle.Get("pdus"); var castedPdus = JNIEnv.GetArray<Java.Lang.Object>(pdus.Handle); var messages = new SmsMessage[castedPdus.Length]; for (var i = 0; i < castedPdus.Length; i++) { var bytes = new byte[JNIEnv.GetArrayLength(castedPdus[i].Handle)]; JNIEnv.CopyArray(castedPdus[i].Handle, bytes); messages[i] = SmsMessage.CreateFromPdu(bytes); } string messageFrom = ""; string messageBody = ""; foreach (var message in messages) { messageFrom = message.DisplayOriginatingAddress; messageBody = message.MessageBody; } var manager = Manager.SharedInstance; var database = manager.GetDatabase("temp"); Console.WriteLine("Got message"); var properties = new Dictionary<string, object>() { {"message", messageBody}, }; var document = database.GetExistingDocument(messageFrom); if (document == null) { document = database.GetDocument(messageFrom); var revision = document.PutProperties(properties); } Console.WriteLine("Retrieved document: " + messageBody); //foreach (var kvp in retrievedDocument.Properties) //{ // Console.WriteLine("{0} : {1}", kvp.Key, kvp.Value); //} }