xamarin.iosxamarin.formsxamarin.androidbroadcastreceiverincoming-call

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


I am trying to build an app that runs on background and activates on incoming calls, after some research I found out I have to do it natively but my code is doing nothing at all.

If there is a way to do it on the PCL project please let me know. I am using a service and a broadcast receiver.

Here is my actual code:

[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:

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

and BroadcastReceiver:

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

Solution

  • It is not possible because IOS wont permit it