Run background task every X amount of time

浪尽此生 提交于 2021-01-21 08:54:32

问题


I would like to start a service that once in awhile on all platforms has checked is there a notification to appear or not. Is there any nuget to connect all platforms or some examples?


回答1:


You can use the Device.StartTimer(TimeSpan minutes) method to start a background task that will repeat after the given time span. Here is a code example:

var minutes = TimeSpan.FromMinutes (3); 

Device.StartTimer (minutes, () => {

    // call your method to check for notifications here

    // Returning true means you want to repeat this timer
    return true;
});

This is included with Xamarin Forms, so you don't need any platform specific logic.

http://iosapi.xamarin.com/index.aspx?link=M%3AXamarin.Forms.Device.StartTimer(System.TimeSpan%2CSystem.Func%7BSystem.Boolean%7D)




回答2:


I think that the best that you can do is following:

Unfortunately, the way that these two platforms have evolved to handle executing background code is completely different. As such, there is no way that we can abstract the backgrounding feature into the Xamarin.Forms library. Instead, we going to continue to rely on the native APIs to execute our shared background task.

Further information for this topic can be found here: https://robgibbens.com/backgrounding-with-xamarin-forms/



来源:https://stackoverflow.com/questions/30938008/run-background-task-every-x-amount-of-time

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!