Skip to content
This repository has been archived by the owner on Oct 20, 2022. It is now read-only.

Latest commit

 

History

History

create_a_simple_service

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
id title brief dateupdated
DCEC2052-373D-C101-30F0-6BFA78C498AB
Create a Simple Service
This recipe shows how to create a service.
2016-12-12

Recipe

This recipe will demonstrate how to start and stop an Android service using an explicit Intent.

  1. Create a new Xamarin.Android application named SimpleService.
  2. Add a new class named SimpleService, setting its base class to Android.App.Service.
  3. Replace the code in the SimpleService with the following:
using System;
using System.Threading;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Util;

namespace SimpleService
{

    [Service]
    public class SimpleStartedService : Service
    {
        static readonly string TAG = "X:" + typeof(SimpleStartedService).Name;
        static readonly int TimerWait = 4000;
        Timer timer;
        DateTime startTime;
        bool isStarted = false;
    
        public override void OnCreate()
        {
            base.OnCreate();
        }
    
        public override StartCommandResult OnStartCommand(Intent intent, StartCommandFlags flags, int startId)
        {
            Log.Debug(TAG, $"OnStartCommand called at {startTime}, flags={flags}, startid={startId}");
            if (isStarted)
            {
                TimeSpan runtime = DateTime.UtcNow.Subtract(startTime);
                Log.Debug(TAG, $"This service was already started, it's been running for {runtime:c}.");
            }
            else
            {
                startTime = DateTime.UtcNow;
                Log.Debug(TAG, $"Starting the service, at {startTime}.");
                timer = new Timer(HandleTimerCallback, startTime, 0, TimerWait);
                isStarted = true;
            }
            return StartCommandResult.NotSticky;
        }
    
        public override IBinder OnBind(Intent intent)
        {
            // This is a started service, not a bound service, so we just return null.
            return null;
        }
    
    
        public override void OnDestroy()
        {
            timer.Dispose();
            timer = null;
            isStarted = false;
    
            TimeSpan runtime = DateTime.UtcNow.Subtract(startTime);
            Log.Debug(TAG, $"Simple Service destroyed at {DateTime.UtcNow} after running for {runtime:c}.");
            base.OnDestroy();
        }
    
        void HandleTimerCallback(object state)
        {
            TimeSpan runTime = DateTime.UtcNow.Subtract(startTime);
            Log.Debug(TAG, $"This service has been running for {runTime:c} (since ${state})." );
        }
    }
}

This code will use an Timer to write a message to the application log every four seconds.

Additional Information

Android services are well suited to long running tasks such as completing a web upload, playing background audio and receiving location updates.