Sunday, January 27, 2013

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

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">
            <intent-filter>
                <action android:name="com.example.codeforfoodtest_two.MY_RECEIVER" />
            </intent-filter>
        </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.codeforfoodtest_two.MY_RECEIVER".

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 {

@Override
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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <Button android:id="@+id/sendButton"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Send broadcast"
        />
    
</LinearLayout>

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{

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

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

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

}

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!

No comments:

Post a Comment