首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >Xamarin:如何构建一个在来电时执行代码的应用程序

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

Stack Overflow用户
提问于 2017-10-17 16:24:08
回答 1查看 497关注 0票数 0

我正试图构建一个运行在后台并对来电进行激活的应用程序,在一些研究之后,我发现我必须自己动手,但我的代码根本不做任何事情。

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

代码语言:javascript
代码运行次数:0
运行
复制
[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);
        }
    }

服务:

代码语言:javascript
代码运行次数:0
运行
复制
[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:

代码语言:javascript
代码运行次数:0
运行
复制
[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");
        }
    }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-11-23 14:48:15

这是不可能的,因为IOS不允许

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46794858

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档