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

时间:2016-11-27 04:34:21

标签: android broadcastreceiver xamarin.forms

使用xamarin表单我正在尝试读取传入的消息并使用广播接收器类显示Toast消息。

以下是我的manifest.xml

[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");
                }
            }
        }
    }
} 

接收者类

 <style>
#preloader 
  {
    display:none;
  }
</style>

$(window).on('load', function () 
        { // makes sure the whole site is loaded 
            if (typeof(Storage) !== "undefined") {
                // Code for localStorage/sessionStorage.                
                if(localStorage.isFirstLoadComplete==="false"){
                    $('#preloader').show(0);
                    $('#status').fadeOut(); // will first fade out the loading animation 
                    $('#preloader').delay(350).fadeOut('slow'); // will fade out the white DIV that covers the website. 
                    $('body').delay(350).css({ 'overflow': 'visible' });
                    localStorage.setItem("isFirstLoadComplete", "true");
                }
            } else {
                // Sorry! No Web Storage support..
            }
        });

但是上面的代码既没有在log cat中显示信息日志消息,也没有在toast文本中显示。 任何人都可以帮帮我

1 个答案:

答案 0 :(得分:-1)

要在Xamarin表单应用程序中显示祝酒词,您需要一个插件。

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