Thursday, December 26, 2013

Broadcast receiver with implicit intent: Android tutorial

Program description:

This is one more program on how to create a broadcast receiver and how to send a broadcast message by using implicit intent.


This program has an activity, and 2 receivers. Activity has one button. On clicking the button an implicit intent will be used to send a broadcast with action com.techpalle.action.RECEIVER1.
This will trigger Receiver1. In Reciever1 we will send one more broadcast with an implicit intent with action com.techpalle.action.RECEIVER2. While sending this broadcast we will generate a random number between 0 to 100, and send that number as extra information in the intent.
This will trigger Receiver2, which displays random number passed from first broadcast receiver.

This program also has a demo of how to send a broadcast from one broadcast receiver to other broadcast receiver, by using implicit intent.

First Activity:

package com.techpalle.b15_asg_receivers;

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 {
 Button b;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  b = (Button) findViewById(R.id.button1);
  b.setOnClickListener(new OnClickListener() {
   @Override
   public void onClick(View v) {
    // TODO Auto-generated method stub
    Intent in = new Intent();
    in.setAction("com.techpalle.action.RECEIVER1");
    sendBroadcast(in);
   }
  });
 }

 @Override
 public boolean onCreateOptionsMenu(Menu menu) {
  // Inflate the menu; this adds items to the action bar if it is present.
  getMenuInflater().inflate(R.menu.main, menu);
  return true;
 }

}

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" >  
   <TextView  
     android:id="@+id/textView1"  
     android:layout_width="wrap_content"  
     android:layout_height="wrap_content"  
     android:text="@string/hello_world" />  
   <Button  
     android:id="@+id/button1"  
     android:layout_width="wrap_content"  
     android:layout_height="wrap_content"  
     android:layout_alignLeft="@+id/textView1"  
     android:layout_below="@+id/textView1"  
     android:text="Send Broadcast" />  
 </RelativeLayout>  


Broadcast receiver
File name : Receiver1.java

package com.techpalle.b15_asg_receivers;

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

public class Receiver1 extends BroadcastReceiver {

 @Override
 public void onReceive(Context context, Intent intent) {
     // TODO Auto-generated method stub
  Toast.makeText(context, "first app..receiver1", 0).show();
  Intent in = new Intent();
  in.setAction("com.techpalle.action.RECEIVER2");
  int ran = (int) (Math.random()*100);
  in.putExtra("ran", ran);
  context.sendBroadcast(in);
 }
}

Broadcast receiver
File name : Receiver2.java

package com.techpalle.b15_asg_receivers;

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

public class Receiver2 extends BroadcastReceiver {

 @Override
 public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub
 int ran = intent.getExtras().getInt("ran");
 Toast.makeText(context, "First app..recvr2..ran.."+ran, 0).show();
 }
}

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_asg_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_asg_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>  
     <receiver android:name="Receiver1">  
       <intent-filter >  
         <action android:name="com.techpalle.action.RECEIVER1"/>  
       </intent-filter>  
     </receiver>  
     <receiver android:name="Receiver2">  
       <intent-filter >  
         <action android:name="com.techpalle.action.RECEIVER2"/>  
       </intent-filter>  
     </receiver>  
   </application>  
 </manifest>  

Download complete code : Click to download

Interview questions on broadcast receivers:
Broadcast receiver runs in which thread?
Is it possible to bind a service from receiver?

Tags: android, broadcast receiver, sendbroadcast, implicit intent

4 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. The following time I read a blog, I trust that it doesn't bomb me similarly as much as this specific one. That is to say, I realize it was my decision to peruse, regardless I really trusted you would presumably have something valuable to discuss. All I hear is a lot of crying about something youRight on target with this review, I really accept that this astonishing site needs substantially more consideration. I'll presumably be getting back to peruse more, a debt of gratitude is in order for the data!

    ReplyDelete
  3. After I at first remarked I appear to have tapped on the - Notify me when new remarks are added-checkbox and now every time a remark is added I get 4 messages with precisely the same remark. There must be a simple strategy you can eliminate me from that help? Credit!
    best interiors

    ReplyDelete