Service Basics

  • A Service is an application component that can used for long-running operations in the background.
  • Service might handle network transactions, play music, perform file I/O, or interact with a content provider, all from the background.
  • A Service can run in the background indefinitely, even if the component that started it is destroyed
  • A Service can used for download or upload a file over the network. When the operation is done, the service should stop itself.
  • ** A Service runs with the main application thread , so some time user interaction will lock the current application . Use Service inside a separate thread, you will reduce the risk of Application Not Responding (ANR) errors . So  application's main thread can remain dedicated to user interaction with your activities.

  • ** If you are starting service inside any activity then it will block activity so when created service done then only your activity will unlock. in this mean time if user will interacting to activity then Application Not Responding (ANR) errors will come.

  • ** If you want user interaction on when activity is visible and also some server or time taking tasks(performs intensive or blocking operations) then you might create a thread in onCreate(), start running it in onStart(), then stop it in onStop(). Also consider using AsyncTask or HandlerThread, instead of the traditional Thread class.

  • A Service will start by calling startService() method.

Declaring a service in the manifest

 

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

Caution:

A services runs in the same process as the application in which it is declared and in the main thread of that application, by default. So, if your service performs intensive or blocking operations while the user interacts with an activity from the same application, the service will slow down activity performance. To avoid impacting application performance, you should start a new thread inside the service.

 

   Intent intent = new Intent(this, HelloService.class);
   startService(intent);

Stopping a service

 

      stopService()

Service Lifecycle Callbacks :

 

public class ExampleService extends Service {

            int mStartMode;       // indicates how to behave if the service is killed
            IBinder mBinder;      // interface for clients that bind
            boolean mAllowRebind; // indicates whether onRebind should be used
        
            @Override
            public void onCreate() {
                // The service is being created
            }
            @Override
            public int onStartCommand(Intent intent, int flags, int startId) {
                // The service is starting, due to a call to startService()
                return mStartMode;
            }
            @Override
            public IBinder onBind(Intent intent) {
                // A client is binding to the service with bindService()
                return mBinder;
            }
            @Override
            public boolean onUnbind(Intent intent) {
                // All clients have unbound with unbindService()
                return mAllowRebind;
            }
            @Override
            public void onRebind(Intent intent) {
                // A client is binding to the service with bindService(),
                // after onUnbind() has already been called
            }
            @Override
            public void onDestroy() {
                // The service is no longer used and is being destroyed
            }
}
 

 

service_lifecycle