Forum Xamarin Xamarin.Android

Broadcast receiver OnReceive not being called in Xamarin Android

KenMT9KenMT9 Member ✭✭

Hi everybody,

My code file BroadcastReceiver.cs :

[BroadcastReceiver]
[IntentFilter(new[] { "android.provider.Telephony.SMS_RECEIVED" })]
public class ReadODPFromSMS : BroadcastReceiver
{
protected string message, address = "";
public override void OnReceive(Context context, Intent intent)
{
if(intent.HasExtra("pdus"))
{
var smsArray = (Java.Lang.Object[])intent.Extras.Get("pdus");
foreach(var item in smsArray)
{
var sms = SmsMessage.CreateFromPdu((byte[])item);
address = sms.OriginatingAddress;
message = sms.MessageBody;
Toast.MakeText(context, "Number : " + address + ",Message : " + message, ToastLength.Short).Show();
}
}
}
}

and permission :


Why OnReceive function is not called when my device android receives SMS ?

Best Answers

  • PaulNTUPaulNTU USMember ✭✭✭
    Accepted Answer

    Some possibilities

    1) A quick scan through my stuff shows that I usually declare a broadcast receiver with the enabled flag set to true in the declaration. It seems silly that it wouldn't default to true but its worth checking

    [BroadcastReceiver(Enabled = true)]

    2) Sorry its not showing your permission text, is it the manifest permission 'android.permission.RECEIVE_SMS'?

    3) Have you confirmed the user has actually allowed this functionality after they boot your app?

    4) I have seen some mention in forums posts of the priority flag being needed in some circumstances for intents
    Try
    [IntentFilter(new[] { "android.provider.Telephony.SMS_RECEIVED" }, Priority = 500)]

    HTH

  • KenMT9KenMT9 Member ✭✭
    Accepted Answer

    I got it , thank you.

    The device doesn't grant permissions "SMS" . Sorry if my english is bad.

Answers

  • PaulNTUPaulNTU USMember ✭✭✭
    Accepted Answer

    Some possibilities

    1) A quick scan through my stuff shows that I usually declare a broadcast receiver with the enabled flag set to true in the declaration. It seems silly that it wouldn't default to true but its worth checking

    [BroadcastReceiver(Enabled = true)]

    2) Sorry its not showing your permission text, is it the manifest permission 'android.permission.RECEIVE_SMS'?

    3) Have you confirmed the user has actually allowed this functionality after they boot your app?

    4) I have seen some mention in forums posts of the priority flag being needed in some circumstances for intents
    Try
    [IntentFilter(new[] { "android.provider.Telephony.SMS_RECEIVED" }, Priority = 500)]

    HTH

  • KenMT9KenMT9 Member ✭✭
    Accepted Answer

    I got it , thank you.

    The device doesn't grant permissions "SMS" . Sorry if my english is bad.

Sign In or Register to comment.