0

J'essaie de construire une application qui fonctionne en arrière-plan et active sur les appels entrants, après quelques recherches, j'ai découvert que je dois le faire nativelly mais mon code est ne rien faire du tout.Xamarin: Comment construire une application qui exécute du code sur un appel entrant

S'il y a un moyen de le faire sur le projet PCL s'il vous plaît faites le moi savoir. J'utilise un service et un récepteur de diffusion. voici mon code actuel:

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

et 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"); 
     } 
    } 
+0

vous ne pouvez pas faire dans un PCL parce que ce type de code dépend par définition de la plate-forme. iOS n'a pas d'API pour cela. – Jason

+0

Il est étrange que nous ne pouvons pas contrôler les appels entrants dans IOS. –

+0

permettant une application tierce d'interférer avec une fonction de base de l'appareil serait un énorme risque de sécurité – Jason

Répondre

0

Il est impossible, car IOS permet habitude il