Tutorial details

Android beginner tutorial: Part 64 - Receiving an Intent using BroadcastReceiver | App Code for Sale | Preview

Android beginner tutorial: Part 64 - Receiving an Intent using BroadcastReceiver | Android Tutorial

How to create a BroadcastReceiver and an appliaction that sends a broadcast

Overview PAGE TOP

In this tutorial we are going to create a BroadcastReceiver and an appliaction that sends a broadcast.

There will be 2 applications - one that contains the BroadcastReceiver, the other has an Activity with a button that sends a broadcast. If you've followed my previous tutorials you most likely have 2 applications already, so you can use those.

The first thing we need to do is declare the receiver in the manifest xml of the second application (CodeForFoodTestTwo in my case).

Go to the AndroidManifest.xml of that application and add this to the application tags:

    <receiver android:name="TestReceiver">
            <action android:name="com.example.codeforfoodtest_two.MY_RECEIVER" />

As you can see, I added an intent-filter. BroadcastReceivers respond to implicit Intents, so I specified one filter field - action with a name "com.example.codeforfoodtesttwo.MYRECEIVER".

Now create a TestRecevier.java class in this application, give it an onReceive() callback function. We'll receive a string value from the broadcast with the key "myText" and display it using a Toast to the user. That's all our receiver is going to do this time.

package com.example.codeforfoodtest_two;

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

public class TestReceiver extends BroadcastReceiver {

public void onReceive(Context context, Intent intent) {
Bundle extras = intent.getExtras();
String txt = extras.getString("myText");
    Toast.makeText(context, "Broadcast received! Text: " + txt, Toast.LENGTH_SHORT).show();


Now go back to the first application, open it's layout xml and add a button there:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    tools:context=".MainActivity" >

    <Button android:id="@+id/sendButton"
        android:text="Send broadcast"


Listen to the click of the button and dispatch an Intent using sendBroadcast() method when the button is clicked. We use the putExtra() method to send a text value with the key "myText".

package com.kircode.codeforfood_test;

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

public class MainActivity extends Activity{

protected void onCreate(Bundle savedInstanceState) {

final Button sendbtn = (Button)findViewById(R.id.sendButton);
sendbtn.setOnClickListener(new View.OnClickListener() {

public void onClick(View v) {
Intent intent = new Intent("com.example.codeforfoodtest_two.MY_RECEIVER");
intent.putExtra("myText", "Hello!");


Now we have 2 applications, with one being able to send a broadcast message to the other one. The second application receives the message and handles it by displaying a passed text value from the first application.

That's all for today!

Thanks for reading!

Reference PAGE TOP


0 Comments Leave a comment

Please login in order to leave a comment.

Newest first

to your Chupamobile Account.

The Easiest way to Launch your next App or Game.

Join Chupamobile and get instant access to thousands of ready made App and Game Templates.

Creating an account means you’re okay with Chupamobile’s Terms of Service and Privacy Policy.