Forum Xamarin.Forms
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

How to add background service in Xamarin forms

I am new developer in xamarin,
That's why i faced some problem.
I can't create background service in (PCL) Android, iOS and also UWP.

Anybody help me to give example code or station.

Best Answer

Answers

  • SmfKarimSmfKarim Member ✭✭

    I want to know about,
    when start my app this time run a service in background like location trace / data update in my web server etc and when closs my app stop backgrount service.
    how to do it (PCL) Android, iOS and also UWP

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    how to do it (PCL) Android, iOS and also UWP

    You don't. Aren't you reading my earlier posts - or the Xamarin tutorials/links/videos I posted on background services? Just because you want it doesn't make it possible. Background services have to be made on a per-platform basis. Period. Accept that.

    If you want to do it within your app on its own thread - you could do that. Just like any other thread you make. But it won't be a service - its just a thread performing a recurring action.

    Since this is only supposed to happen while your app is running, you could just react to the location changed event of the Location plugin, and then upload the data to your server. It doesn't have to be a service. Its just responding to an event, no different than any other event like handling a button.click event or a listview.itemselectionchanged event.

  • wondererwonderer Member ✭✭

    Hi, this is one year old, seem to knowledgeable about this, aware of any xamarin forms packages that wrap this so far?

  • AndreiMisiukevich_AndreiMisiukevich_ USMember ✭✭✭✭✭

    @wonderer said:
    Hi, this is one year old, seem to knowledgeable about this, aware of any xamarin forms packages that wrap this so far?

    Sure, https://github.com/winstongubantes/MatchaBackgroundService

  • wondererwonderer Member ✭✭

    Thanx Andrei, google did not bring up this one, I assumed it should be fairly simple.

    This means I call the dependencies in the plattforms, and from there I can than call more class in the shared code, which gives me the ability to run services on many platforms through the shared code

  • wondererwonderer Member ✭✭
    Hi,

    This calls the code every x seconds, just for clarification, I need to run the service, when I have screen lock or app in background,

    This sample calls thread every x secs, can I still use it for my case somehow adapted

    Thanx

    Walter
  • xamarin_newbiexamarin_newbie USMember ✭✭

    Hi ClintStLaurent and other developers,

    I tried the sample developed by Rob Gibbens

    https://robgibbens.com/backgrounding-with-xamarin-forms/

    On my iPhone 6+ with iOS 12, the sample works well with debug model. But when start the app on my iPhone, start LongRunning task and lock iPhone or press iPhone's home button, the background task will be cancelled automatically after 3 minutes even though check Background model, Background fetch, Background processing on Info.plist page. Do you have any suggestion to solve the issue?

    @ClintStLaurent said:

    I can't create background service in (PCL)

    That's correct. Because each platform has their own way and rules for background services. You have to make services for each platform.

    I don't want to be that guy that says "Google it" - but there are a number of tutorials directly from Xamarin that explain backgrounding. Have you looked? Getting your information directly from Xamarin is always the first place to start.

    https://developer.xamarin.com/guides/ios/application_fundamentals/backgrounding/

    https://www.google.com/search?q=xamarin+university+background+services&ie=utf-8&oe=utf-8&client=firefox-b-1

  • vivek00712vivek00712 Member ✭✭
    edited March 26

    Have any one find the solution? I am experiencing the same. When app heads to background, app pauses the execution of thread/task and when app comes to foreground it resumes the execution of thread/task. I want to execute the peace of code even app is in minimized mode or phone is locked. Anyone has idea for it?

  • vivek00712vivek00712 Member ✭✭

    I am facing same issue. Have you find the solution ?

Sign In or Register to comment.