Xamarin – walk-through – (Part 4)

Android Services

A service is started when an application component, such as an activity, starts it by calling startService(). Once started, a service can run in the background indefinitely, even if the component that started it is destroyed.

Extending Service class. This is using same UI thread by default. So if we do not run our methods in separate threads, we can notice that UI may be lagged, since it is sharing same thread.

starting and stopping the service

IntentService usage:

This one is running methods in separate worker thread by default. So it might be a better start point for using services.

registering service in AndroidManifest.xml

Started Services – service that downloads some information in the background.
Bound Services – programmatic interface for callers to interact with, such as a location service.

Create a Started Service

Create a Bound Service


Value is of type StartCommandResult, which can be any of the following:

  • Sticky – A sticky service will be restarted, and a null intent will be delivered to OnStartCommand at restart. Used when the service is continuously performing a long-running operation, such as updating a stock feed.
  • RedeliverIntent – The service is restarted, and the last intent that was delivered to OnStartCommand before the service was stopped by the system is redelivered. Used to continue a long-running command, such as the completion of a large file upload.
  • NotSticky – The service is not automatically restarted.
  • StickyCompatibility – Restart will behave like Sticky on API level 5 or greater, but will downgrade to pre-level 5 behavior on earlier versions.

In order to conserve system resources, a started service implementation calls StopSelf after any long-running work is done. This is important because the service will run independently of the component that called StartService.

can be stopped from outside call as well

When a service is stopped, the OnDestroy method will be called on the service. This is the part of the process where any cleanup of service-wide resources can be done. To provide an implementation, simply override OnDestroy as follows:

To call a service in either the local or remote scenario, use intent filters.

With this attribute in place, the service can be started from any class that inherits from Context, For example, Activity uses the following code, and passes the action to the intent:

Using Notifications

toast message:

it’s possible to create some services that will not be destroyed by the system under normal circumstances. Services of this kind are known as foreground services. Users are actively aware of the presence of foreground services. For example, an Internet radio service is a good candidate to be created as a foreground service because the user is aware of its existence through the audio it plays back.

The IntentService works by sending all intents to a worker queue for processing. This queue processes each intent serially on a separate thread, so as to not block the main thread, passing the intent to the OnHandleIntent method. When all the intents have been processed, the IntentService stops itself by calling StopSelf internally.

Unlike Started Services, Bound Services do not run indefinitely. Instead, they are created when a client connects to them and are destroyed after all bound clients have disconnected.

The following diagram illustrates the lifecycle of a Bound Service:

Android Interface Definition Language (AIDL)

AIDL (Android Interface Definition Language). It allows you to define the programming interface that both the client and service agree upon in order to communicate with each other using interprocess communication (IPC). On Android, one process cannot normally access the memory of another process. So to talk, they need to decompose their objects into primitives that the operating system can understand, and marshall the objects across that boundary for you. The code to do that marshalling is tedious to write, so Android handles it for you with AIDL.

Android uses the AlarmManager class to allow an application to schedule itself to be run at certain times in the future.

The hybrid approach is very powerful and common way to use Services.

Hybrid Service: The lifecycle of both Bound and Started Services can be combined by explicitly starting a Service and then binding to it. This offers the advantage of starting a Service independently of the rest of the application to provide it with an opportunity to do work, and then to also be able to connect to it directly in order to access it.

Since the introduction of Service Messengers, AIDL has largely been superseded, because Service Messengers are much simpler to use and easier to implement.

Any component that inherits from Context (such as an Activity) can start the Service.

the following code snippet shows an OnStartCommand implementation that returns StartResultCommand.Sticky, which will restart the Service automatically. A Sticky Service will get restarted with a null intent if the OS ever shuts it down due to memory pressure.

The following code calls the StopSelf method after it completes its work.

The following diagram illustrates how the caller uses a Binder and ServiceConnection to communicate with a Service running locally:

To get location data from the system to the Service, we need to use a LocationManager. The location manager provides a way for the application to interact with the system location Service.

StopSelf does not work if the binding is still active or if we are still registered to receive location updates from the Location Manager. Remember to stop location updates in the LocationService OnDestroy method:

Android Backgrounding Guidance


MediaPlayer is very state sensitive and calling one of its methods in the wrong state will cause an exception to be thrown.

List views and adapters are one of the most fundamental building blocks of Android Applications.



Fragments are classes that represent a portion of the user interface in an Activity.

Auto Complete Sample

alternative resource

calendar info displayed in the ListView:

Calendar Sample


      android:layout_height="wrap_content" />


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:textSize="16dip" />
      android:textSize="12dip" />

Adding a Calendar Event

Typically, a CardView is used to present a single row item in a ListView or GridViewview group.


Creating a Splash Screen


Leave a Reply

Your email address will not be published. Required fields are marked *