Learn Android for Free,issues for developers, mobengineers website provide complete solutions for developers issues posts.It also provides online training for android development

Monday, October 29, 2012

Service in android

Activity is a foreground process. It contains user interaction. To run long running operations, we have to create a service.It will run in background. It does not contain user interaction.

Types of services:

1. Unbound Service: it runs in the background indefinitely even started activity with service ends also.
2. Bound Service : it will run till life time of activity.

 Activity can start service via startService()and it will stop via stopService().
If activity want to interact with service,it can use bindService().

first onCreate() is called, after onStartCommand is called with the intent data provided by the activity.

Android provides many predefined services exposed via Manager class.

Own services must be declared in the manifest file..

Some methods:

An activity can start a service via startService() method.

stop the service by using stopService() method.

bindService() - this is for activity to interact with service.

it needs "ServiceConnection" object.

Once service is started    onCreate()  is called.

onBind()- the system calls when other component want to bind the service.

onStop() - for stopping the service.

OnDestroy()- called when service is no longer used. and it is destroyed.

Own services must be declared in manifest file.

how to declare own service:

<service android:name="your class"></service>
implementing class must extend the class   " service or one of its subclasses.

Ex: AsyncTask:
using services in android allows to have app running inthe background. if u want to do a simple task run in the background, use AsyncTask.

AsyncTask class:

private class someclassname extends AsyncTask<x,y,z>

protected void onPreExecute()
// starting new thread i.e    initilizing

protected void doInBackground()

// perform the action or do the task

protected void onPostExecute(){
// closing the process


package com.example.servicetest;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;

public class MainActivity extends Activity {

    public void onCreate(Bundle savedInstanceState) {
        startService(new Intent(this,ServiceAct.class));

    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;

package com.example.servicetest;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;

public class ServiceAct extends Service{
    String tag="service tag";
    public IBinder onBind(Intent intent) {
        // TODO Auto-generated method stub
        return null;

    public void onDestroy() {
        // TODO Auto-generated method stub
        Log.d(tag, "service destroyed");   
    public void onStart(Intent intent, int startId) {
        // TODO Auto-generated method stub
        super.onStart(intent, startId);
        Log.d(tag, "service started");

manifest permission

<service android:name="ServiceAct" />

0 coment�rios:

Post a Comment

20% off

Online Training

Your Name :
Your Email: (required)
Your Message: (required)

NDTV Gadgets360

Powered by Blogger.

Recent Posts

Find Us On Facebook

Popular Posts