So in the last blog we talked all about what broadcast receivers are but in this blog, we’re going to start getting some code written!

Creating a broadcast receiver in code is pretty easy once you know how and for this article we’ll get one running using the most basic way.  We’ll then get more complicated as the series progresses.

Creating a broadcast receiver in an Activity or fragment

The easiest way to create a broadcast receiver is to create one right inside your Main_Activity in your onOncreate() code.  To do this you need to create 2 components and the way i do this is to:

Create the broadcast receiver

You do this by writing the following code into your app:

 

package com.raptorhub.dktestbcr;

public class MainActivity extends AppCompatActivity {

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

    private BroadcastReceiver bcr2 = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            Toast.makeText(context, "BINGO2!", Toast.LENGTH_SHORT).show();
            Log.i("DKi", "OnRec");
        }
    };

}

All we did here was to add in the broadcast receiver function, the onReceive() override will auto complete. This is where we actually do the work when our broadcast receiver runs.

Now we need to create the Intent that will be used to register the intent type you want to use and this is quite literally 1 line of code!

 

package com.raptorhub.dktestbcr;

public class MainActivity extends AppCompatActivity {

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

        final Intent ibcr = registerReceiver(bcr2, new IntentFilter(Intent.ACTION_POWER_CONNECTED));
 
 }

 private BroadcastReceiver bcr2 = new BroadcastReceiver() {
     @Override
     public void onReceive(Context context, Intent intent) {
 
     }
 };

}

 

You can see above we simply created an intent, we told it that it will be registering a receiver and to look for the “ACTION_POWER_CONNECTED” status which will broadcast every time power is connected to the device.

Testing

To test the app, we need to make sure we actually know when this happens so either put a Log.d or Toast into the OnReceive () of the broadcast receiver to double check it’s all working.  I used a Toast in this case.

package com.raptorhub.dktestbcr;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final Intent ibcr = registerReceiver(bcr2, new IntentFilter(Intent.ACTION_POWER_CONNECTED));
 
}

 private BroadcastReceiver bcr2 = new BroadcastReceiver() {
     @Override
     public void onReceive(Context context, Intent intent) {
         Toast.makeText(context, "POWER CONNECTED!", Toast.LENGTH_SHORT).show();
    }

 };

}
Testing and extra things to try

Run the app, keep the app in the foreground and simply plug in your USB charger into the device and you should see the toast message pop up when you connect the power.  Notice you won;t see the popup when you disconnect the power.  That’s because connecting and disconnecting power to an Android device actually have separate intent filter types.  To see the disconnected one you simply change the intent:

final Intent ibcr = registerReceiver(bcr2, new IntentFilter(Intent.ACTION_POWER_DISCONNECTED));

Just to round this off why not try to change the above code so that it will work for both connecting and disconnecting the power. Also you could change the intent filter to something completely different like “BATTERY_LEVEL_CHANGED”, although you might have to wait a while for the battery to change!!

So that’s a very basic Broadcast receiver example.  You can see even at this early stage that we have an app that will be able to listen for ad handle certain broadcast messages that the device is sending out.  However if youy close the app or you put it into the background you’ll notice it stops working.  In the next blog we’ll show you some techniques to stop that!