简体   繁体   中英

Xamarin Forms android Receiving incoming text message using broadcast receiver

Using xamarin forms i am trying to read an incoming message and display a toast message using broadcast receiver class.

Following is my manifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"       android:installLocation="auto">
<uses-sdk android:minSdkVersion="22" android:targetSdkVersion="22" />
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.SEND_SMS" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.WRITE_SMS" />
<application android:label="DemoApp" android:debuggable="true">
</application>
</manifest>

Receiver Class

[BroadcastReceiver(Enabled = true, Exported = true,Label = "SMS Receiver")]
[IntentFilter(new string[] { "android.provider.Telephony.SMS_RECEIVED"}, Priority = Int32.MaxValue)]
public class SmsReceiver : Android.Content.BroadcastReceiver
{
    public static readonly string INTENT_ACTION = "android.provider.Telephony.SMS_RECEIVED";

    public SmsReceiver()
    {
    }

    public override void OnReceive(Context context, Intent intent)
    {
        if (intent.Action == INTENT_ACTION)
        {
            if (ContextCompat.CheckSelfPermission(context,
                "android.permission.READ_SMS") != Permission.Denied)
            {
                Bundle bundle = intent.Extras;

                if (bundle != null)
                {
                    Java.Lang.Object[] pdus = (Java.Lang.Object[])bundle.Get("pdus");

                    if (pdus.Length == 0)
                    {
                        return;
                    }

                    SmsMessage[] msgs;
                    msgs = new SmsMessage[pdus.Length];

                    for (int i = 0; i < msgs.Length; i++)
                    {
                        msgs[i] = SmsMessage.CreateFromPdu((byte[])pdus[i], "3gpp");

                        Log.Info("SmsReceiver", "SMS Received from: " + msgs[i].OriginatingAddress);
                        Log.Info("SmsReceiver", "SMS Data: " + msgs[i].MessageBody.ToString());
                    }

                    Toast.MakeText(context.ApplicationContext, "SUCCESS",
                    ToastLength.Long).Show();

                    Log.Info("SmsReceiver", "SMS Received");
                }
            }
        }
    }
} 

But the above code neither displays the info log message in the log cat nor the toast text. Can any one please help me out

To show toasts in Xamarin forms application, you need a plugin.

https://github.com/EgorBo/Toasts.Forms.Plugin

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM