Tuesday, February 28, 2012

Start Service from Broadcast Receiver

Some time we need to start Service from Broadcast Receiver..
how can we achieve this i am going to write step by step.

So lets create a small App to do this things 

-------------------------------------------
App Name: BReceiver2Service
Package Name: com.rdc
Android SDK: Android SDK 2.2 / API 8
-------------------------------------------

MyReceiver.java 
package com.rdc;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;

public class MyReceiver extends BroadcastReceiver {

 @Override
 public void onReceive(Context context, Intent intent) {
  
  Toast.makeText(context, "MyReceiver Started", 
    Toast.LENGTH_SHORT).show();
  Log.v("Info Message", "in Broadcast receiver");
  Intent myIntent=new Intent(context,MyService.class);  
  context.startService(myIntent);
 }

}


MyService
package com.rdc;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.widget.Toast;

public class MyService extends Service {

 @Override
 public IBinder onBind(Intent intent) {  
  return null;
 }
 
 @Override
 public int onStartCommand(Intent intent, int flags, int startId){
  Toast.makeText(getBaseContext(), "Service Started",
    Toast.LENGTH_SHORT).show();
 /* We want this service to continue running until it is explicitly
       stopped, so return sticky. */
        return START_STICKY;  
 }
}


main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
 xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="vertical"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent">
 <TextView
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:text="@string/hello" />
</LinearLayout>


AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest
 xmlns:android="http://schemas.android.com/apk/res/android"
 package="com.rdc"
 android:versionCode="1"
 android:versionName="1.0">
 <uses-sdk android:minSdkVersion="8" />

 <application
  android:icon="@drawable/icon"
  android:label="@string/app_name">
  <service
   android:enabled="true"
   android:name=".MyService">
   <intent-filter>
    <action android:name="com.rdc.MyService">
    </action>
   </intent-filter>
  </service>
  <receiver
  android:enabled="true"
  android:name=".MyReceiver">
  <intent-filter>
  <action android:name="android.intent.action.BOOT_COMPLETED" />
  </intent-filter>
  </receiver>
 </application>

</manifest>

Now Reboot Emulator, Toast will appear.

you can download zip file from here : BReceiver2Service

cheers!!

 I'd love to hear your thoughts!!

2 comments:

  1. Awsome !!!!......what a step by step guidance to achieve the BootCompleted service start .....
    I was wondering if you can post something related to :: "The background Service should continue to run even after Force Stop from the Task Manager "....
    I would really appreciate it.....

    ReplyDelete
    Replies
    1. Thank you Nadim.. I'll keep in mind your words and try to post asap!!

      Delete