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

Thursday, September 27, 2012

Services And BroadcastReceivers in Android

Seo Directory List

Activity: it is single screen application. it is a foreground process.it contain user interaction.


a service is a component which runs in the background without interacting with user.

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.


package com.example.serviceexample;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity implements OnClickListener {
            Button b1;
            Button b2;
    public void onCreate(Bundle savedInstanceState) {
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;

     public void onClick(View v) {
                        // TODO Auto-generated method stub
                        case R.id.button1:
                                    startService(new Intent(this,ServiceDemo.class));
                        case R.id.button2:
                                    startService(new Intent(this,ServiceDemo.class));

package com.example.serviceexample;

import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.IBinder;

public class ServiceDemo extends Service{
      MediaPlayer mediaplayer;

      public IBinder onBind(Intent arg0) {
            // TODO Auto-generated method stub
            return null;

      public void onCreate() {
            // TODO Auto-generated method stub
            mediaplayer=MediaPlayer.create(this, R.raw.newsong);

      public void onDestroy() {
            // TODO Auto-generated method stub
      public void onStart(Intent intent, int startId) {
            // TODO Auto-generated method stub
            super.onStart(intent, startId);

Declare manifest permission for service

 <service android:name="ServiceDemo"></service>

you can download source code here


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 in the 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

BroadCast Receiver:

It class which extends BroadCastReceiver.  
which is registered in the android app via manifest file.

This class can be able to recieve intents via sendBroadcast()

BroadCast Receiver defines the method  onReceive().  During this method , BroadCast Receiver object will be valid.


public class PhoneReceiver extends BroadcastReciever

public void onReceive()

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