Jim
Jim

Reputation: 801

How to pass a value when starting a background service on Android? Using Xamarin and C#

I would like to start a background service that plays an audio file at a specified time on Android. I am using Xamarin and c#

I'm new to the concept of background services and starting them with an intent. At the moment I don't know how to pass a variable using this. If it were a method I would just send something like playAudio(20), I am looking for this equivalent (if it exists, please let me know if not).

My background service looks like this

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;

namespace AudioTour
{
    [Service]
    [IntentFilter(new[] { ActionPlay, ActionPause, ActionStop })]
    public class StreamingBackgroundService : Service
    {

        public const string ActionPlay = "com.xamarin.action.PLAY";
        public const string ActionPause = "com.xamarin.action.PAUSE";
        public const string ActionStop = "com.xamarin.action.STOP";
        public const string ActionPlayAtSpecifiedTime = "com.xamarin.action.PLAYATTIME";


        public override StartCommandResult OnStartCommand(Intent intent, StartCommandFlags flags, int startId)
        {
            switch (intent.Action)
            {
                case ActionPlay:
                    Console.WriteLine("Action Play");
                break;
                case ActionStop:
                    Console.WriteLine("Action Stop");
                break;
                case ActionPause:
                    Console.WriteLine("Action Stop");
                break;
                case ActionPlayAtSpecifiedTime:
                    Console.WriteLine("It would be great if I can get this to play at a certain time");
                break;
            }
            return StartCommandResult.Sticky;
        }

        public override IBinder OnBind(Intent intent)
        {
            // This is a started service, not a bound service, so we just return null.
            return null;
        }
    }
}

and I start the service doing this

using Android.App;
using Android.Widget;
using Android.OS;
using Android.Content;

namespace AudioTour
{
    [Activity(Label = "AudioTour", MainLauncher = true, Icon = "@drawable/icon")]
    public class MainActivity : Activity
    {
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);

            // Set our view from the "main" layout resource
            SetContentView (Resource.Layout.Main);


            var intent = new Intent(StreamingBackgroundService.ActionPlayAtSpecifiedTime);
            StartService(intent);
        }
    }
}

Currently I do not know how to start this service and pass an integer value.

Upvotes: 1

Views: 1795

Answers (1)

Mark Larter
Mark Larter

Reputation: 2363

You can add extra data to the intent before starting the service via the PutExtra method:

protected override void OnCreate(Bundle bundle)
{
    ...

    int songId = 1;
    var intent = new Intent(StreamingBackgroundService.ActionPlayAtSpecifiedTime);
    intent.PutExtra("songId", songId);

    StartService(intent);

    ...

Then you can get that extra data from the intent in the service, and act accordingly:

public override StartCommandResult OnStartCommand(Intent intent, StartCommandFlags flags, int startId)
{
    ...

    var songId = intent?.GetIntExtra("songId");
    switch (intent.Action)
    {
        case ActionPlay:
            Console.WriteLine($"Action Play {songId.GetValueOrDefault()}");
            break;

        ...

Upvotes: 2

Related Questions