简体   繁体   English

Xamarin Forms android使用广播接收器接收传入的文本消息

[英]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. 使用xamarin形式,我尝试使用广播接收器类阅读传入消息并显示敬酒消息。

Following is my manifest.xml 以下是我的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. 但是以上代码既未在log cat中显示信息日志消息,也未在toast文本中显示。 Can any one please help me out 有人可以帮帮我吗

To show toasts in Xamarin forms application, you need a plugin. 要在Xamarin表单应用程序中显示敬酒,您需要一个插件。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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