A service is a component which runs in the background without direct interaction with the user. A service extends the base class Service.
Services run with a higher priority than inactive or invisible activities and therefore it is less likely that the Android system terminates them for resource management. The only reason Android will stop a Service prematurely is to provide additional resources for a foreground component usually an Activity. When this happens, your Service can be configured to restart automatically.
You can start a service if it is not running using Context.startService(), connect (bind) to a running service using Context.bindService(), and communicate with the running service through an interface that the service exposes. Each service class must have a corresponding <service> declaration in the AndroidManifest.xml file for its package.
Service which call indefinitely in between activity. An Android component may bind itself to a Service using bindservice (). A bound service would run as long as the other application components are bound to it. As soon as they unbind, the service destroys itself.
Unbound Service
Service which call at the life span of calling activity. In this case, an application component starts the service, and it would continue to run in the background, even if the original component that initiated it is destroyed. For instance, when started, a service would continue to play music in the background indefinitely.
Services run with a higher priority than inactive or invisible activities and therefore it is less likely that the Android system terminates them for resource management. The only reason Android will stop a Service prematurely is to provide additional resources for a foreground component usually an Activity. When this happens, your Service can be configured to restart automatically.
You can start a service if it is not running using Context.startService(), connect (bind) to a running service using Context.bindService(), and communicate with the running service through an interface that the service exposes. Each service class must have a corresponding <service> declaration in the AndroidManifest.xml file for its package.
Types of Services
Bound ServiceService which call indefinitely in between activity. An Android component may bind itself to a Service using bindservice (). A bound service would run as long as the other application components are bound to it. As soon as they unbind, the service destroys itself.
Unbound Service
Service which call at the life span of calling activity. In this case, an application component starts the service, and it would continue to run in the background, even if the original component that initiated it is destroyed. For instance, when started, a service would continue to play music in the background indefinitely.
Services Life cycle |
Methods in Activity Class.
Every Service has Methods which we can override.
- onStartCommand() - We can't call this method directly. This method called by System every time the client explicitly calling Context.startService(Intent) to start the service.
- onBind() - You must always implement this method. If you implement this method, you must provide an interface that clients use to communicate with the service, by returning an IBinder object. The system calls this method when another component wants to bind with the service by calling bindService(). If you don't want to allow binding, then you should return null.
- onUnbind() - System calls this method when all clients have disconnected from a particular interface published by the service.
- onRebind() - System calls this method when new clients have connected to the service after it had previously been notified that all had disconnected in its onUnbind(Intent).
- onCreate() - This method call is a one-time set-up. System calls this method when the service is first created using onStartCommand() or onBind().
- onDestroy() - This method used to destroy useless services. Your service should implement this to clean up any resources such as threads, registered listeners, receivers, etc.
package com.abnhive;
import android.app.Service;
import android.os.IBinder;
import android.content.Intent;
import android.os.Bundle;
public class MyService extends Service {
int mStartMode;
IBinder mBinder;
boolean mAllowRebind;
@Override
public void onCreate() {
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return mStartMode;
}
@Override
public IBinder onBind(Intent intent) {
return mBinder;
}
@Override
public boolean onUnbind(Intent intent) {
return mAllowRebind;
}
@Override
public void onRebind(Intent intent) {
}
@Override
public void onDestroy() {
}
}
Comments
Post a Comment