Xamarin:如何构建一个在来电时执行代码的应用程序

Xamarin: How to build an app that executes code on incoming call

我正在尝试构建一个在后台运行并在来电时激活的应用程序,经过一些研究后我发现我必须以本机方式执行此操作,但我的代码根本没有执行任何操作。

如果 PCL 项目有办法做到这一点,请告诉我。 我正在使用服务和广播接收器。 这是我的实际代码:

[Activity(Label = "Teste2", Icon = "@drawable/icon", Theme = "@style/MainTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
    public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
    {

        public static Context AppContext;

        protected override void OnCreate(Bundle bundle)
        {
            TabLayoutResource = Resource.Layout.Tabbar;
            ToolbarResource = Resource.Layout.Toolbar;

            base.OnCreate(bundle);

            global::Xamarin.Forms.Forms.Init(this, bundle);
            LoadApplication(new App());

            AppContext = this.ApplicationContext;

            StartPushService();
        }

        public static void StartPushService()
        {
            AppContext.StartService(new Intent(AppContext, typeof(Services.BackgroundService)));
            if (Android.OS.Build.VERSION.SdkInt >= Android.OS.BuildVersionCodes.Kitkat)
            {

                PendingIntent pintent = PendingIntent.GetService(AppContext, 0, new Intent(AppContext, typeof(Services.BackgroundService)), 0);
                AlarmManager alarm = (AlarmManager)AppContext.GetSystemService(Context.AlarmService);
                alarm.Cancel(pintent);
            }
        }

        public static void StopPushService()
        {
            AppContext.StopService(new Intent(AppContext, typeof(Services.BackgroundService)));

            PendingIntent pintent = PendingIntent.GetService(AppContext, 0, new Intent(AppContext, typeof(Services.BackgroundService)), 0);
            AlarmManager alarm = (AlarmManager)AppContext.GetSystemService(Context.AlarmService);
            alarm.Cancel(pintent);
        }
    }

服务:

[Service(Name = "com.xamarin.Teste2.BackgroundService")]
    public class BackgroundService : Service
    {
        // Magical code that makes the service do wonderful things.
        public override void OnCreate()
        {
            base.OnCreate();
        }

        public override StartCommandResult OnStartCommand(Android.Content.Intent intent, StartCommandFlags flags, int startId)
        {

            return StartCommandResult.Sticky;
        }

        public override Android.OS.IBinder OnBind(Android.Content.Intent intent)
        {

            return null;
        }


        public override void OnDestroy()
        {
            base.OnDestroy();
        }
    }

和广播接收器:

[BroadcastReceiver]
    [IntentFilter(new[] { Android.Content.Intent.ActionAnswer })]
    public class CallReceiver : BroadcastReceiver
    {
        public override void OnReceive(Context context, Intent intent)
        {
            Toast.MakeText(context, "Incoming call from someone", ToastLength.Short).Show();
            System.Console.WriteLine("Incoming call from someone");
        }
    }

不可能,因为IOS不允许