we need to distinguish between A Service and a Thread or an AsyncTask: Threads or Async task perform their tasks in a background thread thus they do not block the main thread, while a service performs it's work in the main thread. so if a service is performing an intensive task such as calling a web service, it may block the main thread until it finishes. So for intensive tasks a service should run it's work in a background thread.
A service runs in the same process of the application and keeps running until stopped by itself, stopped by the user or killed by the system if it needs memory.
Creating a service:
to create a service we create a class that extends android.app.Service and it would be like this:public class DemoService extends Service {next we need to define our service in our AndroidManifest.xml file:
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
}
<service android:name="DemoService"></service>The service life cycle has the following events
- onCreate(): called when the service is created.
- onStart(): Called when the service starts by a call to startService(Intent intent).
- onDestroy(): Called as the service is terminates.
Calling a service:
A service can be called from an activity in two ways:- By calling startService(Intent intent).
- By binding to the service through an Binder object.
calling startService(Intent intent):
to start a service from an activity using this method, we create an intent and start the service like this:Intent intent=new Intent(this,DemoService.class);the startService(intent) method causes the onStart() method of the service to be called, so the service can execute it's work like this:
startService(intent);
public class DemoService extends Service {the service will keep running until it stops itself via stop stopSelf() after finishing work:
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
doSomething();
}
public void doSomething(){
// do some work
}
}
@Overrideor it can be stopped from the activity via stopService(Intent intent).
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
doSomething();
stopSelf();
}
Binding to a service through an Binder object:
As the service runs in the same process of the application the service has only one instance (singleton) instance running. you may want to keep reference to this instance to perform periodical tasks or to call the service methods themselves.to make the service bind-able we extends Binder class and return an instance of it in the service's onBind(Intent intent) method:
public class DemoService extends Service {then we bind the service from our activity by first creating a ServiceConnection object to handle the service connection/disconnection then binding to the service by an intent like this:
private final IBinder binder = new LocalBinder();
@Override
public IBinder onBind(Intent arg0) {
return binder;
}
public class LocalBinder extends Binder {
DemoService getService() {
return DemoService.this;
}
}
@Override
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
doSomething();
stopSelf();
}
public void doSomething(){
// do something
}
}
public class MainActivity extends Activity {notice that we unbind the service in the activity's onDestroy() method to disconnect from the service and stop it from executing any further
DemoService mService;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
ServiceConnection serviceConn=new ServiceConnection() {
/**
* service unbound, release from memory
**/
@Override
public void onServiceDisconnected(ComponentName name) {
mService=null;
}
/**
* service is bound, start it's work
**/
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
mService=((LocalBinder)service).getService();
mService.doSomething();
}
};
@Override
protected void onResume() {
super.onResume();
// bind to the service by an intent
Intent intent=new Intent(this,DemoService.class);
// AUTO CREATE: creates the service and gives it an importance so that it won't be killed
// unless any process bound to it (our activity in this case) is killed to
bindService(intent, serviceConn, Context.BIND_AUTO_CREATE);
}
@Override
protected void onDestroy() {
super.onDestroy();
/ unbind the service whena ctivity is destroyed
unbindService(serviceConn);
}
}
and that's was all about Android services, stay tuned for another Android tutorial.
No comments:
Post a Comment