My Blog List

Sunday 4 December 2011

Service in Android.


Hello friends,
Today’s post is about services in android.
Service in android, a facility for the application to tell the system about something it wants to be doing in the background. Service is not an Activity(it mean Your service class does not extend Activity class). A Service is not a separate process, it runs in the same application process. So if you want to perform some task in background you  will write service for it. Just like Activity ,Service class has its own method like onStart(), onCreate()(is called when service process is created), onStartCommand()(called each time the service is sent acommand via startService() ), onDestroy()(which is called as the service is being shut down).
Now here are some steps to create and start/stop service.
1. Create a class which will extend Service class
2. Register  service in AndroidMenifest.xml file with <service> declaration.
3.  Start your service with Context.startService() or Context.bindService().

1.       So for creating services you have to write your own service class which must extends Service class. And as your class extends Service class .you have to override some methods.
Here i am providing  one demo this service will play audio in background when user press start button and to stop service there is stop button.
Here  is my main.xml file
 
Through this service I just played an audio so for store your audio in res/raw folder
 

Create  service for playing audio in background.
 
2.Register  service in AndroidMenifest.xml file with <service> declaration.
 3.Create an activity . start service via intent.

That's all we have created one service in just couple of minute.
for any query mail me : bpsingh216@gmail.com
keep coding... ;-)


No comments:

Post a Comment