Blog purpose for android basic example for android app developer. any query please my contact

Saturday, 25 December 2021

what is service in android explain it in detail

 what is service in android explain it in detail


Android service is a component that is used to perform operations in the background such as playing music, handling network transactions, interacting with content providers, etc. It doesn't have any UI (user interface). The service runs in the background indefinitely even if the application is destroyed.

Services in Android are a special component that facilitates an application to run in the background in order to perform long-running operation tasks. The prime aim of a service is to ensure that the application remains active in the background so that the user can operate multiple applications at the same time. A user interface is not desirable for android services as it is designed to operate long-running processes without any user intervention. A service can run continuously in the background even if the application is closed or the user switches to another application. Further, application components can bind themselves to service to carry out inter-process communication(IPC). There is a major difference between android services and threads, one must not be confused between the two. Thread is a feature provided by the Operating system to allow the user to perform operations in the background. While service is an android component that performs a long-running operation about which the user might not be aware as it does not have UI.



What is service explain in detail in Android?

A Service is an application component that can perform long-running operations in the background. It does not provide a user interface. ... For example, a service can handle network transactions, play music, perform file I/O, or interact with a content provider, all from the background.



What are the types of services in Android?

Types of Android Services

Foreground Services. Foreground services are those services that are visible to the users. ...

Background Services. These services run in the background, such that the user can't see or access them. 

Bound Services. 

Started Service. 

Bound Service. 

IntentService() 

onStartCommand() 

onBind()



Example android services:

import android. app.Service;

import android. os.IBinder;

import android. content.Intent;

import android. os.Bundle;


public class HelloService extends Service {

   

   /** indicates how to behave if the service is killed */

   int mStartMode;

   

   /** interface for clients that bind */

   IBinder mBinder;     

   

   /** indicates whether onRebind should be used */

   boolean mAllowRebind;


   /** Called when the service is being created. */

   @Override

   public void onCreate() {

     

   }


   /** The service is starting, due to a call to startService() */

   @Override

   public int onStartCommand(Intent intent, int flags, int startId) {

      return mStartMode;

   }


   /** A client is binding to the service with bindService() */

   @Override

   public IBinder onBind(Intent intent) {

      return mBinder;

   }


   /** Called when all clients have unbound with unbindService() */

   @Override

   public boolean onUnbind(Intent intent) {

      return mAllowRebind;

   }


   /** Called when a client is binding to the service with bindService()*/

   @Override

   public void onRebind(Intent intent) {


   }


   /** Called when The service is no longer used and is being destroyed */

   @Override

   public void onDestroy() {


   }

}




No comments:

Post a Comment