What is Service in Android?

service in android

Service is nothing but a ghost that exist but we can not see. In relation to Android, service is an application component that can perform long-running operations without having a user interface.

Not going to some jargon we explain you service in very easy way

Did you ever play music in music player, Then you see after closing music player in the notification panel there is a notification continuously showing that which music is currently playing

Yes, This is done using a service. Service can handle many things like network transaction, playing music, file I/O, etc.

Types of Service in Android

In Android there are three type of service

Foreground Service

Foreground service is the type of service that is visible to the user and the user is notified that the particular service is running. Just like Music Player but in foreground service you must have to show a notification to the user that service is running in the foreground

Background Service

Background service is the type of service that is not visible to the user and there is no need to notify that the particular service is running.

Bound Service

When an application component bind to a service it is known as Bound Service. A bound service run as over another application bind to it when another application unbind it Bound service are destroyed

How to start a service in android

If you want to create a service then there is 2 options

  1. Create by going to File-> New -> Service -> Service
service in android

2. By creating a java class and then extends Service and override some method’s

In the First option, you don’t need to declare service is manifest because android studio automatically add it but using the second option you manually need to declare in the android studio like this

<manifest ... >
  ...
  <application ... >
      <service android:name=".ExampleService" />
      ...
  </application>
</manifest>

These are some important method which you need to know before creating service

onStartCommand()

This method called by using startService() in activity or fragment whenever you need to start a service and after service start it is user responsibility to destroy the service otherwise service will run indefinitely

onBind()

This method called by using bindService() when another component want to bind with service

onDestroy()

To destroy you need to call this method.

Creating a Background Service Practical

First, we need to create a Service by going through File->new> new> service and named as GeeksToCode.class

public class MyService extends Service {  
    public IBinder onBind(Intent intent) {  
        return null;  
    }  
  
    @Override  
    public void onStartCommand() {  
        Toast.makeText(this, "Service Started", Toast.LENGTH_LONG).show();    
    }  
    @Override  
    public void onDestroy() {  
        Toast.makeText(this, "Service Stopped", Toast.LENGTH_LONG).show();  
      
    }  
}  

In Activity_main.xml


<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/button"
android:text = "Start Service"/>
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/button2"
android:text = "Stop Service"/>

In MainActiviy.java

Button button1,button2;
Intent intent;
@Override
    protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_next);
       button = findViewById(R.id.button);
       button1.setOnClickListener(new View.OnClickListener
{
          intent  = new Intent(this, GeeksToCode.class);
           startService(intent);

});
 button2.setOnClickListener(new View.OnClickListener
{
         
           stopService(intent);

});
        


}

When you run this app and click on start button you will see a Toast indicating Service is started

Reference

Tagged :

Leave a Reply

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