【问题标题】:Keep task working when the app is in the background当应用程序在后台时保持任务工作
【发布时间】:2020-02-17 12:12:51
【问题描述】:

我正在 Xamarin 表单上编写地图跟踪应用程序。我正在使用async tasks(with Device.StartTimer) 来跟踪位置和计数器计时器的另一个任务,但是当我需要打开另一个应用程序时,例如音乐应用程序或应用程序在后台无法跟踪的任何应用程序时。当我在后台发送应用程序时,所有任务都停止了。当我再次启动应用程序时,任务不会继续。 我只需要在应用运行时继续工作。

这是怎么做到的?

【问题讨论】:

    标签: xamarin xamarin.forms xamarin.android xamarin.ios background-process


    【解决方案1】:

    在这种情况下,您应该使用后台任务,它在应用程序的生命周期之外运行。你可以在这里找到更多信息Xamarin background tasks

    【讨论】:

      【解决方案2】:

      由于Background Execution Limits in Android 8.0或更高版本,在后台正常服务将被杀死。

      在 Android 8.0 或更高版本中,我建议您实现 foreground service 来实现(接收比“常规”服务更高的优先级,并且前台服务必须提供 Android 将显示的通知,只要该服务是跑步)。

      您可以使用依赖服务以xamarin形式打开前台服务。

      IService.cs创建android启动服务的接口。

      public interface IService
      {
        void Start();
      }
      

      然后实现DependentService启动前台服务。

      DependentService.cs

      [assembly: Xamarin.Forms.Dependency(typeof(DependentService))]
      namespace TabGuesture.Droid
      {
        [Service]
      public class DependentService : Service, IService
      {
      public void Start()
      {
          var intent = new Intent(Android.App.Application.Context, 
          typeof(DependentService));
      
      
          if (Android.OS.Build.VERSION.SdkInt >= Android.OS.BuildVersionCodes.O)
          {
              Android.App.Application.Context.StartForegroundService(intent);
          }
          else
          {
              Android.App.Application.Context.StartService(intent);
          }
      }
      
      public override IBinder OnBind(Intent intent)
      {
          return null;
      }
      public const int SERVICE_RUNNING_NOTIFICATION_ID = 10000;
      public override StartCommandResult OnStartCommand(Intent intent, 
      StartCommandFlags flags, int startId)
      {
          // From shared code or in your PCL
      
          CreateNotificationChannel();
          string messageBody = "service starting";
      
          var notification = new Notification.Builder(this, "10111")
          .SetContentTitle(Resources.GetString(Resource.String.app_name))
          .SetContentText(messageBody)
          .SetSmallIcon(Resource.Drawable.main)
          .SetOngoing(true)
          .Build();
          StartForeground(SERVICE_RUNNING_NOTIFICATION_ID, notification);
          //do you work
          return StartCommandResult.Sticky;
      }
      
      
      void CreateNotificationChannel()
      {
          if (Build.VERSION.SdkInt < BuildVersionCodes.O)
          {
              // Notification channels are new in API 26 (and not a part of the
              // support library). There is no need to create a notification
              // channel on older versions of Android.
              return;
          }
      
          var channelName = Resources.GetString(Resource.String.channel_name);
          var channelDescription = GetString(Resource.String.channel_description);
          var channel = new NotificationChannel("10111", channelName, NotificationImportance.Default)
          {
              Description = channelDescription
          };
      
          var notificationManager = (NotificationManager)GetSystemService(NotificationService);
          notificationManager.CreateNotificationChannel(channel);
       }
      }
      }
      

      这是关于您的需求的类似主题。 How to create service doing work at period time in Xamarin.Forms?

      用于 IOS 后台任务。可以参考this thread

      【讨论】:

      • 谢谢,我会查的。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多