Services

Started Services:

a. Create a Service Class:

Create a new Java or Kotlin class that extends Service. This class will contain the code for your service.

                  
                    public class MyStartedService extends Service {
                        @Override
                        public int onStartCommand(Intent intent, int flags, int startId) {
                            // Your background logic goes here
                            return START_STICKY; // Or other appropriate return value
                        }
                    
                        @Override
                        public IBinder onBind(Intent intent) {
                            return null; // Return null for started services
                        }
                    }
                    
                  
                

b. Register the Service in the Manifest:

Add the service to your AndroidManifest.xml file.

                  
                    <service android:name=".MyStartedService" />
                  
                

c. Start the Service:

You can start the service from an activity or another component.

                  
                    Intent serviceIntent = new Intent(this, MyStartedService.class);
startService(serviceIntent);
                  
                

Bound Services:

a. Create a Service Class:

Create a new Java or Kotlin class that extends Service. This class will contain the code for your service.

                  
                    public class MyBoundService extends Service {
                        private final IBinder binder = new MyBinder();
                    
                        @Override
                        public IBinder onBind(Intent intent) {
                            return binder;
                        }
                    
                        public class MyBinder extends Binder {
                            MyBoundService getService() {
                                return MyBoundService.this;
                            }
                        }
                    
                        public String getData() {
                            // Your service logic goes here
                            return "Data from the service";
                        }
                    }                    
                  
                

b. Register the Service in the Manifest:

Add the service to your AndroidManifest.xml file.

                  
                    <service android:name=".MyBoundService" />                   
                  
                

c. Bind to the Service:

Bind to the service from an activity or another component.

                  
                    MyBoundService myService;
boolean isBound = false;

private ServiceConnection serviceConnection = new ServiceConnection() {
    @Override
    public void onServiceConnected(ComponentName className, IBinder service) {
        MyBoundService.MyBinder binder = (MyBoundService.MyBinder) service;
        myService = binder.getService();
        isBound = true;
    }

    @Override
    public void onServiceDisconnected(ComponentName arg0) {
        isBound = false;
    }
};

// Bind to the service
Intent intent = new Intent(this, MyBoundService.class);
bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE);

// Unbind from the service
if (isBound) {
    unbindService(serviceConnection);
    isBound = false;
}                  
                  
                

Note:

  1. Ensure that you properly manage the lifecycle of services as they refer to them.
  2. For long-running tasks, consider using a JobIntentService or IntentService.
  3. Make sure to ask for permission if your service is in need of them.
  4. Due to this the services are executed in the main thread thus use of worker threads or AsyncTask for intensive tasks recommends itself.