Xamarin表示后台任务仅在ios上打开应用程序时运行

时间:2018-03-28 19:38:51

标签: c# android ios xamarin xamarin.forms

  1. 预期的行为
    • IOS
      • 当应用程序最小化并且通知每5秒钟发出一次。
    • 的Android
      • 当应用程序最小化并且通知每5秒钟发出一次。
  2. 实际行为
    • IOS
      • 应用程序最小化并且没有通知进入,打开应用程序会导致通知每5秒钟进入一次。
    • 的Android
      • 当应用程序最小化并且通知每5秒进入一次。
        //from app.xaml.cs
        protected override void OnSleep()
            {
                void ScheduleNotification()
                {
                    // Start a timer that runs after 5 seconds.
                    Device.StartTimer(TimeSpan.FromSeconds(5), () =>
                    {
                        System.Threading.Tasks.Task.Factory.StartNew( () =>
                        {
                            // Do the actual request and wait for it to finish.
                             PerformNotification();
                            // Switch back to the UI thread to update the UI
                            Device.BeginInvokeOnMainThread(() =>
                            {
                                // Update the UI
                                // ...
                                // Now repeat by scheduling a new request
                                ScheduleNotification();
                                count++;
                            });
                        }, CancellationToken.None, TaskCreationOptions.DenyChildAttach, TaskScheduler.Default);
        
                        // Don't repeat the timer (we will start a new timer when the request is finished)
                        return false;
                    });
                }
               void PerformNotification()
                {
                    CrossLocalNotifications.Current.Show("title", "body"+ count.ToString());
        
                }
                ScheduleNotification();
            }
        

1 个答案:

答案 0 :(得分:2)

在iOS上,不要求在后台运行的应用程序无法在后台运行超过10秒。为了使一些代码在后台运行,你可以在iOS和android上实现DependencyService。您可以将以下代码放在iOS上的该服务中:

public async Task RunCodeInBackgroundMode(Func<Task> action, string name = "MyBackgroundTaskName")
    {
        nint taskId = 0;
        var taskEnded = false;
        taskId = UIApplication.SharedApplication.BeginBackgroundTask(name, () =>
        {
            //when time is up and task has not finished, call this method to finish the task to prevent the app from being terminated
            Console.WriteLine($"Background task '{name}' got killed");
            taskEnded = true;
            UIApplication.SharedApplication.EndBackgroundTask(taskId);
        });
        await Task.Factory.StartNew(async () =>
        {
            //here we run the actual task
            Console.WriteLine($"Background task '{name}' started");
            await action();
            taskEnded = true;
            UIApplication.SharedApplication.EndBackgroundTask(taskId);
            Console.WriteLine($"Background task '{name}' finished");
        });

        await Task.Factory.StartNew(async () =>
        {
            //Just a method that logs how much time we have remaining. Usually a background task has around 180 seconds to complete. 
            while (!taskEnded)
            {
                Console.WriteLine($"Background task '{name}' time remaining: {UIApplication.SharedApplication.BackgroundTimeRemaining}");
                await Task.Delay(1000);
            }
        });
    }

在Android上,您可以使用以下代码:

public async Task RunCodeInBackgroundMode(Func<Task> action, string name = "MyBackgroundTaskName")
        {
            var powerManager = (PowerManager)Application.Context.GetSystemService(Context.PowerService);
            var wakeLock = powerManager.NewWakeLock(WakeLockFlags.Partial,
                                                    name);
            //acquire a partial wakelock. This prevents the phone from going to sleep as long as it is not released.
            wakeLock.Acquire();
            var taskEnded = false;

            await Task.Factory.StartNew(async () =>
            {
                //here we run the actual code
                Console.WriteLine($"Background task '{name}' started");
                await action();
                Console.WriteLine($"Background task '{name}' finished");
                wakeLock.Release();
                taskEnded = true;
            });

            await Task.Factory.StartNew(async () =>
            {
                //just a method to keep track of how long the task runs
                var stopwatch = new Stopwatch();
                stopwatch.Start();
                while (!taskEnded)
                {
                    Console.WriteLine($"Background '{name}' task with wakelock still running ({stopwatch.Elapsed.TotalSeconds} seconds)");
                    await Task.Delay(1000);
                }
                stopwatch.Stop();
            });
        }

在共享项目中,您可以通过以下方式调用这些方法:

var deviceInfoServic = ServiceLocator.Instance.Get<YourService Interface here>();
await deviceInfoServic.RunCodeInBackgroundMode(async () =>
{
    //your code here
});