/** * Functions hooked into colormag_action_head action. * * @hooked colormag_head - 10 */ do_action( 'colormag_action_head' ); wp_head(); ?>

Android Service tutorial with Example

In this tutorial,we will learn about the android service which is a very important topic of android to perform background operations

Service is an android component which runs into the background to perform long-running operations

It does not provide user interface( UI) i.e user can’t interact with an application

It runs on main thread of the application

It does not create own thread  & does not run in a separate process unless you specify

Types of Services

There are three types of services

1.Foreground Service

2.Background Service

3.Bound Service

1.Foreground Service

A foreground service performs some operations into the background that are noticeable by the user

Example

Playing audio track then Notification Display outside the phone

2.Background Service

A background  service performs some operations into the background that are not noticeable by the user

Example

When send image via whatsapp then we don’t know how it internally works

3.Bound Service

A service is bound when an application component binds to it by calling bindService( ).A bound service offers a client-server interface that allows components to interact with the service ,send requests,and receive results.

Example

In this example ,we will see how to make a service class and use it inside the activity.

This is the service class where we use two methods of onStartCommand( ) and onDestroy( )

onStartCommand( ) : It is used to start the service and initialize the object

onDestroy( ) : It  is used to stop  the service

MyService.java


public class MyService extends service
{

@Nullable
@Override
public IBinder onBind(Intent intent)
{
return null;
}

@Override
public int onStartCommand(Intent intent , int flags , int startId)
{
Toast.make(MyService.this,”Service is started”,Toast.LENGHT_SHORT).show( );
return START_STICKY;
}

@Override
public void onDestroy()
{
super.onDestroy( );
Toast.make(MyService.this,”Service is stopped”,Toast.LENGHT_SHORT).show( );
}
}

Now the above service class,we will use inside the activity to perform the operations

Here we will use two methods of service 1.startService( ) & 2.stopService( ) for start and stop the service respectively

MainActivity.java

public class MainActivity extends Activity
{
@Override
Protected void onCreate(Bundle onSaveInstanceState)
{
super.onCreate(saveInstanceState);
setContentView(R.layout.main_activity);

}

public void startService(View view)
{
startService(new Intent(getBaseContext( ),MyService.class));
}

public void stopService(View view)
{
stopService(new Intent(getBaseContext( ),MyService.class));
}
}

Now ,Open the AndroidManifest.xml file & put the below code

<application>

<service android:name=”.MyService”/>

</application>

I hope , I have explained the service step by step.If any query please comment to me.

Best of Luck!!!

sumankumar80

Hello Friends,I have written and developed this website because you can learn technology easily ,prepare for the placement and also for the any type of examination . By :Suman Kumar

Leave a Reply

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