应用关闭时的Xamarin Android服务

时间:2018-12-02 18:19:40

标签: xamarin xamarin.android

我希望即使关闭应用程序也可以运行服务。

我尝试了这段代码,但是当我关闭应用程序时,它停止了。

服务等级

 [Service(Exported = true, Process = "com.process_myservice")]
    class MyBackgrounding : Service
    {
        public override IBinder OnBind(Intent intent)
            => null;

        [return: GeneratedEnum]
        public override StartCommandResult OnStartCommand(Intent intent, [GeneratedEnum] StartCommandFlags flags, int startId)
        {
            new Task(() =>
            {
                //StartTimer();
                while (true)
                {
                    CrossLocalNotifications.Current.Show(y++.ToString(), "body");
                    Task.Delay(5000).Wait();
                }
            }).Start();
            return StartCommandResult.StickyCompatibility;
        }
        public override bool StopService(Intent name)
        {
            return base.StopService(name);
        }
    }

使用OnCreate方法启动服务

StartService(new Intent(this, typeof(MyBackgrounding)));

应用程序正在运行

the notifications are pushed while app is running or in background.

申请已关闭

The process still there but no notifications pushed

注意:我不希望该服务推送通知,但是我希望它记录传感器数据......推送通知只是为了尝试该服务。

谢谢

0 个答案:

没有答案
相关问题