Android Broadcast Receiver Example

Here is source code of the Program to Demonstrate to Receive BroadCast in Android. The program is successfully compiled and run on a Windows system using Eclipse Ide. The program output is also shown below.

A Broadcast within an activity can be heard by using a Broadcast Receiver , the broadcast receiver can be implemented using the BroadcastReceiver class in android.

Main Activity

package com.example.mc_project;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.widget.Toast;
public class Tracker extends Activity {
    double latitude;
    double longi;
    private static final LatLng JLNStadium = new LatLng(28.590401000000000000,
    LatLng JLNS = new LatLng(28.55, 77.54);
    private double pLati, plongi, dLati, dlongi;// previous latitude and
                                                // longitude
    private GoogleMap map;
    private BroadcastReceiver receiver = new BroadcastReceiver() {
        public void onReceive(Context context, Intent intent) {
            Bundle bundle = intent.getExtras();
            Log.i("Here", "onreceived");
            if (bundle != null) {
                latitude = bundle.getDouble("lati");
                Log.i("Tag", latitude + "");
                longi = bundle.getDouble("longi");
                Log.i("tag", longi + "");
                drawmap(latitude, longi);
    public void drawmap(double latid, double longid) {
        // draw on map here
        // draw line from intial to final location and draw tracker location map
        Log.i("Tag", "map");
        // add line b/w current and prev location.
        LatLng prev = new LatLng(pLati, plongi);
        LatLng my = new LatLng(latid, longid);
        //map.moveCamera(CameraUpdateFactory.newLatLngZoom(my, 15));
        // map.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null);
        Polyline line = map.addPolyline(new PolylineOptions().add(prev, my)
        pLati = latid;
        plongi = longid;
    protected void onResume() {
        registerReceiver(receiver, new IntentFilter("com.example.mc_project"));
    protected void onPause() {
    protected void onStop() {
        // TODO Auto-generated method stub
    protected void onCreate(Bundle savedInstanceState) {
        final double src_lati = getIntent().getDoubleExtra("src_lati", 0.0);
        Log.i("Tracker_Src_lati", src_lati + "");
        final double src_longi = getIntent().getDoubleExtra("src_longi", 0.0);
        final double dest_lati = getIntent().getDoubleExtra("dest_lati", 0.0);
        final double dest_longi = getIntent().getDoubleExtra("dest_longi", 0.0);
        pLati = src_lati;// intialize latitude and longitude here from intent data.
        plongi = src_longi;
        // & get destination lai and longi. from intent data
        dLati = dest_lati;
        dlongi = dest_longi;
        try {
            // Loading map
        } catch (Exception e) {
        Intent loc_intent;
        loc_intent = new Intent(getBaseContext(), Getlocation.class);
        loc_intent.putExtra("lat", dLati);
        loc_intent.putExtra("lon", dlongi);
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(, menu);
        return true;
    private void initilizeMap() {
        if (map == null) {
            map = ((MapFragment) getFragmentManager()
            Log.i("Tag", "map");
            // draw line b/w intial and final location.
            LatLng strt = new LatLng(pLati, plongi);
            LatLng end = new LatLng(dLati, dlongi);
            Marker start_m = map.addMarker(new MarkerOptions().position(strt)
            Marker end_m = map.addMarker(new MarkerOptions().position(end)
            //map.moveCamera(CameraUpdateFactory.newLatLngZoom(strt, 15));
            map.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null);
            Polyline line = map.addPolyline(new PolylineOptions()
                    .add(strt, end).width(5).color(Color.RED));
            // check if map is created successfully or not
            if (map == null) {
                        "Sorry! unable to create maps", Toast.LENGTH_SHORT)

Sanfoundry Global Education & Learning Series – 100+ Java Android Tutorials.

If you wish to look at all Tutorials, go to Java Android Tutorials.

Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.