广播接收器无法接收SMS

时间:2018-10-19 08:53:12

标签: xamarin.forms xamarin.android sms

我想让一个移动应用程序在SMS公开后做事。 我创建了定义所有方法的“ SMS类”接口。 然后,我在Main Xamarin Forms项目中从接口继承了一个class(这样我就不会错过任何创建方法)。 接下来,我在项目的Android部分中创建了一个类,女巫在接收时应阅读(以及更多)SMS。

我从主项目中的类中使用DependencyService调用了Android部分的方法。

要在android中管理传入的SMS,我执行了以下操作: 我做了一个方法(在Android项目中-“ CheckForIncommingSMS”)来注册一个BroadcastReceiver巫婆,它应该是调用类本身。 从主项目的MainPage中调用提到的方法。 MainPage => SMSHelper.CheckForIncommingSMS()=> Android项目=>用于接收SMS的registring BroadcastReceiver 收到短信时的屏幕消息。

广播接收器本身似乎可以工作。 我通过其他目的对其进行了测试,并且可以正常工作。

我发现没有调用OnReceive方法。

我怎么了?

谢谢!

using System.Collections.Generic;
using App.Models;

namespace App.Services
{
    public interface ISMSHelper
    {
        void CheckForIncommingSMS();
        void Create(string sender, string receiver, string message);
        void Send();
        List<SMS> ReadAll();
        SMS Get();
    }
}


using System.Collections.Generic;
using App.Models;
using Xamarin.Forms;

// Class in Main Xamarin Forms Project

[assembly: Xamarin.Forms.Dependency(typeof(App.Services.SMSHelper))]
namespace App.Services
{
    class SMSHelper : ISMSHelper
    {
        public void CheckForIncommingSMS()
        {
            DependencyService.Get<ISMSHelper>().CheckForIncommingSMS();
        }

        public void Create(string sender, string receiver, string message)
        {
            DependencyService.Get<ISMSHelper>().Create(sender, receiver, message);

        }

        public SMS Get()
        {
            return DependencyService.Get<ISMSHelper>().Get();
        }

        public List<SMS> ReadAll()
        {
            return DependencyService.Get<ISMSHelper>().ReadAll();
        }

        public void Send()
        {
            DependencyService.Get<ISMSHelper>().Send();
        }
    }
}


//Class in Xamarin Forms Project (Android)

using Android.App;
using Android.Content;
using Android.Provider;
using Android.Telephony;
using Android.Util;
using Android.Widget;
using Plugin.Messaging;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using App.Models;
using App.Services;
using Xamarin.Forms;

[assembly: Xamarin.Forms.Dependency(typeof(App.Droid.SMSHelper))]
namespace App.Droid
{
    [BroadcastReceiver(Enabled = true, Label = "SMS Receiver")]
    [IntentFilter(new[] { "android.provider.Telephony.SMS_RECEIVED" })]
    public class SMSHelper : BroadcastReceiver, ISMSHelper
    {
        private const string Tag = "SMSBroadcastReceiver";
        private const string IntentAction = "android.provider.Telephony.SMS_RECEIVED";

        SMS sms = new SMS();

        public SMSHelper() {
            /* https://blog.xamarin.com/cross-platform-messaging-for-ios-android-and-windows https://forums.xamarin.com/discussion/13682/broadcastreceiver-for-sms*/}

        public void Create(string sender, string receiver, string message)
        {
            sms.Sender = sender;
            sms.Receiver = receiver;
            sms.Message = message;
        }

        public void Send()
        {
            if ((!sms.Receiver.Equals("")) && (!sms.Message.Equals("")))
            {
                var smsMessenger = CrossMessaging.Current.SmsMessenger;

                if (smsMessenger.CanSendSmsInBackground)
                {
                    smsMessenger.SendSmsInBackground(sms.Receiver, sms.Message);
                }
            }
        }

        public SMS Get()
        {
            return sms;
        }

        public List<SMS> ReadAll()
        {
            throw new System.NotImplementedException();
        }

        public override void OnReceive(Context context, Intent intent)
        {
            Log.Info(Tag, "Intent received: " + intent.Action);


            if (intent.Action != IntentAction) return;

            SmsMessage[] messages = Telephony.Sms.Intents.GetMessagesFromIntent(intent);

            for (var i = 0; i < messages.Length; i++)
            {
                sms.Sender = messages[i].OriginatingAddress;
                sms.Message = messages[i].MessageBody;
                sms.Timestamp = DateTime.Now;

                Toast.MakeText(context, sms.Message, ToastLength.Long).Show();
                Debug.WriteLine("SMS ER");
            }

            Toast.MakeText(context, "sdfghhj", ToastLength.Long).Show();
        }

        public void CheckForIncommingSMS()
        {
            Debug.WriteLine("HALLO");
            Forms.Context.RegisterReceiver(this, new IntentFilter(IntentAction));
        }

    }
}

0 个答案:

没有答案