Any Android Application is made of four parts, which are Activity, Broadcast receiver, Content Provider and Service. Service is used to perform the background tasks without any interaction with the user and it also works after destroying the application. Generally, the Services are used to perform long running operations in the background. Service can be used to download file operations, perform file I/O operation, play music and interact with the content provider, all from the background. Services can be of four different types, which are given below.
- Started or Unbound Service
- Bound Service
- Intent Service
- Hybrid Service
Depending on the application requirement, we can use the types of Services given above. Mostly, Intent Service and Bound Service are used because both types of Services can perform most of the tasks, which can be performed by an Android Application.
In this article, we learn about unbound Services.
A Service is started by an Application component by StartService() callback method. Started or Unbound Service will be run in the background; even if the Application component that started it is destroyed. This type of Service performs single operations and it does not return a result to the caller. For example, when downloading or uploading a file from Internet, when the download operation is complete, the Service stops itself.
Life cycle of Unbound Service
Lifecycle of Unbound Service is shown the figure given below. Service class content's six methods are described in the table given below.
This method is used to initialize the Service. It can be used to initialize the variable, object required by the Service.
This method must be implemented by all bound Services. It will first call when any client tries to connect with bound Service. For Unbound Service, this method returns null.
This method calls when all clients unbind.
This method calls when the new clients have connected to the Service. Afterwards, it has been disconneted by OnUnbind().
This method must be implemented by all the started Services. It calls first when the type of Service is started Service.
This method call when service is destroyed by StopService() or StopSelf() method from the Application component.
The steps are given below to create Started or unbound Service in an Android Application.
Create new Project for Android Application
I have selected “Blank App(Android)” from the template for this article.
Creating Started Service
Add new class in project by right clicking on project directory, select “Add->New Item”. Select “Class” from the template. I have given the name as “MyServices” as the file name, which is shown below.
Declaring “MyServices” class with Override Methods
Now, we need to extend (inherit) “MyServices” class from the base class “Service” and also implement the base method, which is required by Service. Here, I am implementing “Started Service” so required compulsory methods, which are OnStartCommand(), OnDestroy() and OnBind(). We can add OnCreate() but it is optional. Here, as I said, I am implementing “Started Service”, so OnBind() method returns null. The code snippet is given below.
- class MyServices : Service
- public override StartCommandResult OnStartCommand(Intent intent,StartCommandFlags flags, int startId)
- Toast.MakeText(this, "Started Service is start", ToastLength.Long).Show();
- return base.OnStartCommand(intent, flags, startId);
- public override void OnDestroy()
- Toast.MakeText(this, "Started Service is stop", ToastLength.Long).Show();
- public override IBinder OnBind(Intent intent)
- return null;
As I explained previously, Lifecycle of Unbound or Started Service starts from “OnStartCommand()” method. Thus, when the Service given above starts by an Android component (Here is Activity), which will show the message that “Started Service is start” on the screen. We can see this in output section of this article. Similarly when this Service is terminated by an Android component, it will show the message “Started Service is stop”. Started Service is never bind due to which I am returning “null” in OnBind()method.
Layout of the Main.axml
For start Service, I took one button as “Started Service” and for stop Service, one button as “Stop Started Service”. Thus, in my Main.axml contents, two buttons and Final Main.axml file looks, as shown below.
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:text="Started Service"
- android:id="@+id/btnstartstartedservice" />
- android:text="Stop Started Service"
- android:id="@+id/btnstopstartedservice" />
Screenshot of the layout is shown below.
Now, when the user clicks on “Started Service” button, “MyServices” Service starts using StartService() method and when the user clicks on “Stop Started Service”, “MyServices” Service is terminated by StopService() method. The code snippet of OnCreate() method of MainActivity is given below.
- protected override void OnCreate(Bundle bundle)
- SetContentView (Resource.Layout.Main);
- Button btnstartstartedservice = (Button)FindViewById(Resource.Id.btnstartstartedservice);
- Button btnstopstartedservice = (Button)FindViewById(Resource.Id.btnstopstartedservice);
- btnstartstartedservice.Click += Btnstartstartedservice_Click;
- btnstopstartedservice.Click += Btnstopstartedservice_Click;
- private void Btnstopstartedservice_Click(object sender, System.EventArgs e)
- StopService(new Intent(this, typeof(MyServices)));
- private void Btnstartstartedservice_Click(object sender, System.EventArgs e)
- StartService(new Intent(this, typeof(MyServices)));
In this article, we learned about Unbound Service Life cycle and how to create, Start and stop Started or Unbound types of Service in Xamarin, using Visual Studio 2015.