
What is Services/Background Service ?
Services is an application component that can perform background operation.it can not provide user interface.Another Application component can start a service and its continue to run in the background event if user switches to another application.
Services component is used to perform operations on the background.Like Playing Music,Internet download process.Services is bounded by component to perform interactive or inter process communications.
Life Cycle Of Services
Started Service : A Service is started when component (Ex : activity) called startService() Method used.Now in this method to call and run the background services.When service is stopped to call stopService() Method Used.Service Can be stopped itself to use stopSelf() Method.
Bound Service : A Service is bound when Application component to used bindService() Method call.And Service is unbind to used unBindService() Method used.
Now Service Life cycle Flow Given Below :
1 |

Above Image Flow to Different Different Service Life Cycle First one is startService and Second Flow is bindService.
Methods of Services
onCreate() : In this method to used when the service is first time created to using this method.if the service is already running this method is does not called.
onStartCommand() : In this method to used when another activity component request to the service will be started to call this method.when this method is call to service is started and run the background.if this is implement it is your responsibility to stop the service when its work to called method is stopService() or stopSelf().
onBind() : onBind() Method is used to bind the services and its used to bindService() method.if this method is implement to you must be provide interface that can clients are communicate to service and its returning is IBinder object.
onRebind() : onRebind() Method is when the client is already connected to the service after it had previously all the disconnected to in its onUnbind() to use this method.
onUnbind() : onUnbind() Method is used of when all clients has disconnected from particular interface published by service.
onDestroy() : In This method when the service is no longer used and its destroyed. Your service should implement this to clean up any resources such as threads, registered listeners, receivers, etc.
Implementation of Services
First You can Create activity_main.xml File
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_services" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:orientation="vertical" android:background="#965302" tools:context="com.pstudy.bhaumik.programmingstudy.MainActivity"> <Button android:id="@+id/btn_start" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Start Services" android:layout_gravity="center_horizontal" android:textAllCaps="false" android:textSize="18dp"/> <Button android:id="@+id/btn_stop" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Stop Services" android:layout_marginTop="20dp" android:layout_gravity="center_horizontal" android:textAllCaps="false" android:textSize="18dp"/> </LinearLayout> |
Now Create MainActivity.java File
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
package com.pstudy.bhaumik.programmingstudy; import android.content.Intent; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.Button; public class MainActivity extends AppCompatActivity { Button start,stop; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); start = (Button) findViewById(R.id.btn_start); stop = (Button) findViewById(R.id.btn_stop); start.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(MainActivity.this,MyServices.class); startService(intent); } }); stop.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(MainActivity.this,MyServices.class); stopService(intent); } }); } } |
Now Another Create MyService.java Class
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
package com.pstudy.bhaumik.programmingstudy; import android.app.Service; import android.content.Intent; import android.os.IBinder; import android.support.annotation.Nullable; import android.widget.Toast; public class MyServices extends Service { @Nullable @Override public IBinder onBind(Intent intent) { return null; } @Override public void onCreate() { Toast.makeText(this, "Service Created", Toast.LENGTH_LONG).show(); } @Override public int onStartCommand(Intent intent, int flags, int startId) { Toast.makeText(MyServices.this,"Services Start ",Toast.LENGTH_SHORT).show(); return START_STICKY; } @Override public void onDestroy() { super.onDestroy(); Toast.makeText(MyServices.this,"Services Destroy ",Toast.LENGTH_SHORT).show(); } } |
Important You need to register service class in AndroidManifest.xml File write Code in AndroidManifest.xml file following this :
1 |
<service android:name=".MyServices" /> |
Now In this Example of Two button create and start and stop service used method when user to click button and called this methods. and start service in run background to display toast message.