Jeez it’s cold here today in Liverpool, UK.  Sub zero and icy cold!!  Anyway in the previous blog we explored a very basic way of working with a Broadcast receiver, but whilst it was great fun to see things working, we also saw some limitations.  Firstly we saw that the broadcast receiver needed some handling when the app went into the background and also if exited.  When in the background it seemed to keep working, but when the app is exited it stops working.  Another limitation is that we have broadcast receiver hard coded into the activity of the app and ideally it would be nice to have it available across the whole app.  This in turn will prepare us for the next stages of improvement too.

So lots to do today! lets try and remedy both of these now.

Handling the OnPause() and OnResume() events

Android is a complex OS but as long as we know how to handle it we can.  When you code a Broadcast receiver into your app in this way, you are usually handling something that is App-wide.  This means that you probably don’t want the broadcast receiver running if you move the app to the background, for example if you were monitoring the camera button being pressed to override it’s behaviour in your own app.  You might find you want to disable this if the app isn’t running.

This behavior is easily achieved by using the OnPause() and onCreate() methods of the activity which we can easily override.

You can see in the code below I have added these events into it.

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();
    }

 };

    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    protected void onResume() {
        super.onResume();

    }
}

However we have had to make a few tweaks to the code so it works properly.  Firstly we need to handle the onPause().  This will be fired every time the app activity is put on hold, when it might go into the background for example, so this will be fine for us here.  Here you simply need to unregsiter the broadcast receiver using the line:

unregisterReceiver(bcr2);

 

Secondly we need to handle the onResume() method so that when the app re-receives focus, it will re-start the Broadcast receiver.  We must do this, despite it being in the onCreate() method because the onCreate() method only fires when the activity first gets created.

Here we use the following code to -re-register the broadcast receiver, looks familiar!

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

 

Depending on what you are doing there are 2 other methods you can override called the onStart() and onStop() methods you could use these instead of onResume() and onPause().

Testing and extra work

So at this point you should have the following 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) {
         Toast.makeText(context, "POWER CONNECTED!", Toast.LENGTH_SHORT).show();
    }

 };

    @Override
    protected void onPause() {
        super.onPause();
        unregisterReceiver(bcr2);
    }

    @Override
    protected void onResume() {
        super.onResume();
        final Intent ibcr = registerReceiver(bcr2, new IntentFilter(Intent.ACTION_POWER_CONNECTED));

    }
}

 

We have a broadcast receiver which will be on whilst your app is in the foreground, and off if it loses focus.  If you don’t want it to turn off then just omit it from the onPause() method but also be sure to not restart it if it is already running so remove the code from the onResume() too.  Remember you can use the onStart() and onStop() methods for this too.

However we do have some limitations still as a lot of the time an app needs to run even if it is closed or not running at all.  In this case if the app is not run or if it is exited, the broadcast receiver will stop working.  If you think about our own Battery settings manager, the app is really only used to tweak some settings, the BATTERY_CHANGED broadcast receiver needs to run all the time.  If this is the case then you need to approach your broadcast receiver slightly differently and we show you how in the next article.