无法在另一个事件处理程序(依赖服务)中调用事件

时间:2019-11-07 12:55:43

标签: c# xamarin.forms

我有以下问题。在下面的代码中,我有一个可以从方法GetSmSInbox()调用的事件,但是在OnReceive()内,该事件不起作用,因为它为null。订阅在主项目中完成。所以问题是为什么它为Null,以及如何从OnReceive()调用它。是因为OnReceive是重写方法,然后将执行放在单独的踩踏中吗?

[assembly: Xamarin.Forms.Dependency(typeof(SmsBroadcastRceiver))]
namespace SmsServiceApp.Droid
{
[BroadcastReceiver(Enabled = true, Exported = true)]
[IntentFilter(new[] { "android.provider.Telephony.SMS_RECEIVED" })]
public class SmsBroadcastRceiver : BroadcastReceiver , ISmsReader
{

    SmsBroadcastRceiver()
    {

    }
    public event EventHandler<OnSmsReceivedEventArgs> OnSmsReceive;




    public void GetSmsInbox()
    {
        //Toast.MakeText(Application.Context, "Tess Dependency", ToastLength.Long).Show();
        OnSmsReceive?.Invoke(this, new OnSmsReceivedEventArgs("Test"));

    }


    public override void OnReceive(Context context, Intent intent)
    {
        var msgs = Telephony.Sms.Intents.GetMessagesFromIntent(intent);

        foreach (var msg in msgs)
        {

               OnSmsReceive?.Invoke(this, new OnSmsReceivedEventArgs(msg.DisplayMessageBody));
            // Toast.MakeText(Application.Context, msg.DisplayMessageBody, ToastLength.Short).Show();
        }
    }
}

}

接口实现

public interface ISmsReader
{

     void GetSmsInbox();
     event EventHandler<OnSmsReceivedEventArgs> OnSmsReceive;

}

订阅

    public MainPage()
    {
        InitializeComponent();

        pullRequestButton.Clicked +=  delegate {

            Xamarin.Forms.DependencyService.Get<ISmsReader>().GetSmsInbox();
        };



        Xamarin.Forms.DependencyService.Get<ISmsReader>().OnSmsReceive += OnSmsReceived;

    }

    private void OnSmsReceived(object sender, OnSmsReceivedEventArgs e)
    {

        editorSms.Text = e.MSG;

    }

1 个答案:

答案 0 :(得分:1)

首先,我们讨论有关从OnReceive()调用它的问题。 如果要阅读SMS,请添加以下权限。

<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.SEND_SMS" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.BROADCAST_SMS" />

读短信是dangerous permission。您应该在运行时授予它

接下来,如果要在依赖服务中实现broadcastrecevier,则应创建一个broadcastrecevier,然后将其注册到依赖服务中

这是我关于broadcastrecevier的代码。我使用MessagingCenter将消息从Android发送到PCL

[BroadcastReceiver(Enabled = true, Exported = true)]
[IntentFilter(new[] { "android.provider.Telephony.SMS_RECEIVED" })]
public class SmsBroadcastRceiver : BroadcastReceiver
{

    public SmsBroadcastRceiver()
    {

    }



    public override void OnReceive(Context context, Intent intent)
    {
        var msgs = Telephony.Sms.Intents.GetMessagesFromIntent(intent);

        List<string> msgList = new List<string>();
        foreach (var msg in msgs)
        {
            msgList.Add(msg.DisplayMessageBody);

        }

        MessagingCenter.Send<List<string>>(msgList, "MyMessage");
    }
}

这里是MySmsReader

[assembly: Xamarin.Forms.Dependency(typeof(MySmsReader))]
namespace ScanDemo.Droid
{
 public class MySmsReader : ISmsReader
{
    public void GetSmsInbox()
    {
        IntentFilter filter = new IntentFilter();
        filter.AddAction("android.provider.Telephony.SMS_RECEIVED");


        SmsBroadcastRceiver receiver = new SmsBroadcastRceiver();
        Application.Context.RegisterReceiver(receiver, filter);
       // RegisterReceiver(receiver, filter);
    }
  }
}

这是有关接口的代码

 public interface ISmsReader
{

    void GetSmsInbox();

}

这是前台代码。

 <Button Text="open broadcase" Clicked="Button_Clicked"/>
 <Label x:Name="editorSms"/>

背景代码。

   public MainPage()
    {
        InitializeComponent();
        MessagingCenter.Subscribe<List<string>>(this, "MyMessage", (expense) =>
        {
            List<string> mylist= expense as List<string>;
            string allText= "";
            foreach (string item in mylist)
            {
                allText += item+"  ";
            }
            editorSms.Text = allText;
        });



    }



    private void Button_Clicked(object sender, EventArgs e)
    {
        Xamarin.Forms.DependencyService.Get<ISmsReader>().GetSmsInbox();
    }

这是关于我的演示的正在运行的GIF。 enter image description here