Saturday, January 11, 2014

Dynamic receivers: android tutorial

Program description:

This is a sample demo program for dynamic receivers in Android.

This program has an activity with 2 buttons. This activity has a dynamic broadcast receiver created in it.
On clicking first button we will send a broadcast with action "DYNAMIC_RECEIVER".
On clicking second button we will register dynamic receiver with an intent filter that has action "DYNAMIC_RECEIVER".

First Activity:

package com.techpalle.b15_dyn_receivers;

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

public class MainActivity extends Activity {
 Button b1, b2;
 
 //step1 : create a dynamic b.c.receiver in java code
 private BroadcastReceiver br = new BroadcastReceiver() {
  @Override
  public void onReceive(Context context, Intent intent) {
   Toast.makeText(context, "Dyn receiver triggered", 0).show();
  }
 };
 //step2 : create intent filter
 private IntentFilter inf;
 
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  b1 = (Button) findViewById(R.id.button1); //sending b.c
  b2 = (Button) findViewById(R.id.button2); //register receiver
  
  //prepare intent filter by adding the action.
  inf = new IntentFilter();
  inf.addAction("DYNAMIC_RECEIVER");
  
  b1.setOnClickListener(new OnClickListener() {
   @Override
   public void onClick(View v) {
    Intent in = new Intent();
    in.setAction("DYNAMIC_RECEIVER");
    sendBroadcast(in);    
   }
  });
 
  //step3 : register brodacast receiver with intent filter
  b2.setOnClickListener(new OnClickListener() {
   @Override
   public void onClick(View v) {
    registerReceiver(br, inf);
   }
  });  
 }
}

xml file for first activity 
File name : activity_main.xml

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
   xmlns:tools="http://schemas.android.com/tools"  
   android:layout_width="match_parent"  
   android:layout_height="match_parent"  
   android:paddingBottom="@dimen/activity_vertical_margin"  
   android:paddingLeft="@dimen/activity_horizontal_margin"  
   android:paddingRight="@dimen/activity_horizontal_margin"  
   android:paddingTop="@dimen/activity_vertical_margin"  
   tools:context=".MainActivity" >  
   <Button  
     android:id="@+id/button1"  
     android:layout_width="wrap_content"  
     android:layout_height="wrap_content"  
     android:layout_alignParentLeft="true"  
     android:layout_alignParentTop="true"  
     android:layout_marginLeft="20dp"  
     android:text="Send Broadcast" />  
   <Button  
     android:id="@+id/button2"  
     android:layout_width="wrap_content"  
     android:layout_height="wrap_content"  
     android:layout_alignRight="@+id/button1"  
     android:layout_below="@+id/button1"  
     android:text="Register Receiver" />  
 </RelativeLayout>  

Manifest file
File name : AndroidManifest.xml
 <?xml version="1.0" encoding="utf-8"?>  
 <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
   package="com.techpalle.b15_dyn_receivers"  
   android:versionCode="1"  
   android:versionName="1.0" >  
   <uses-sdk  
     android:minSdkVersion="8"  
     android:targetSdkVersion="17" />  
   <application  
     android:allowBackup="true"  
     android:icon="@drawable/ic_launcher"  
     android:label="@string/app_name"  
     android:theme="@style/AppTheme" >  
     <activity  
       android:name="com.techpalle.b15_dyn_receivers.MainActivity"  
       android:label="@string/app_name" >  
       <intent-filter>  
         <action android:name="android.intent.action.MAIN" />  
         <category android:name="android.intent.category.LAUNCHER" />  
       </intent-filter>  
     </activity>  
   </application>  
 </manifest>  

Download complete code : Click to download

Android interview questions on dynamic broadcast receivers:
How to create broadcast receiver with out using manifest file?
How to start a receiver only if the application is running?

Tags: android, dynamic receivers, register receiver, button click

Smartphones

No comments:

Post a Comment