Handling simultaneous service requests

We may require a single service to be able to handle multiple requests from multiple sources simultaneously.

How to do it...

If we want a service to be able to handle multiple requests simultaneously, we inherit from the Service type, which provides the required features:

  1. We first need a type that inherits from Service:
    [Service]
    public class XamarinService : Service {
      public override StartCommandResult OnStartCommand(
        Intent intent, StartCommandFlags flags, int startId) {
        return StartCommandResult.Sticky;
      }
      public override IBinder OnBind(Intent intent) {
        return null;
      }
    }
  2. Now, we can create a method that handles multiple requests:
    private int started = 0; private async void ProcessRequest() { started++; await Task.Run ...

Get Xamarin Mobile Development for Android Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.