Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
Next up, lets see how to register a Broadcast Receiver in code, so that it only receives messages while our app is currently running.
Downloads
-
0:00
Now we're going to change this broadcast receiver to only work when our
-
0:03
activity is running.
-
0:04
Let's start by commenting out our manifest receiver.
-
0:07
We can select it all, and hit command plus forward slash.
-
0:10
Or control plus forward slash on Windows.
-
0:14
Now we can work in main activity.
-
0:16
So let's open that back up.
-
0:17
And we'll start by adding a field for our receiver.
-
0:20
So up at the top let's add a new private
-
0:25
NetworkConnectionReceiver and let's call it mReceiver and
-
0:30
now let's set it here equals a new NetworkConnectionReceiver.
-
0:35
Okay, so think back to the activity life cycle for a moment.
-
0:39
Which events do we use to make sure that something is only running when an activity
-
0:42
is alive and visible?
-
0:45
That's right, onResume and onPause.
-
0:47
Let's scroll down to the bottom.
-
0:49
And here we already have onStart and onStop.
-
0:52
So I'm gonna group them together and add onResume and below that, onPause.
-
1:01
All right, first things first let's add some log messages for
-
1:04
what's going on with our activity.
-
1:05
Log.i(TAG, and then App is in the foreground.
-
1:11
I'm going to copy this paste for pause and
-
1:16
say App is in the background and
-
1:20
now we want to register in on resume and unregister in on pause.
-
1:25
First thing we need to do to register is to declare our intent filter in code.
-
1:29
We're just building the same thing we had in the manifest.
-
1:31
So here in on resume, let's say,
-
1:33
IntentFilter, let's name it filter equals a new intent filter.
-
1:39
And we'll use the same value we had in the manifest,
-
1:42
android.net.com.CONNECTIVITY_CHANGE.
-
1:50
And now we can hook up our receiver and filter with registerReceiver.
-
1:58
We'll pass in mReceiver that we just set up top and this new filter.
-
2:03
We need to register in unpause with
-
2:06
unregister receiver and it's that same mReceiver.
-
2:11
All right and now we can test this out.
-
2:17
Okay let's get into the notification drawer and if we go back to log it, we can
-
2:21
scroll up a little bit and we see that our app is currently in the foreground.
-
2:25
So let's turn on airplane mode again and then let's go back to lock it.
-
2:32
And here is our connectivity change again, cool.
-
2:36
Now let's test it in the background.
-
2:38
So go back to our app and
-
2:42
we'll back out of it and here in logcat we see that the app is now in the background.
-
2:49
And now if we turn off airplane mode in this case we don't get a message for
-
2:55
the connectivity change.
-
2:56
Our receiver is no longer operating.
-
3:00
So which method should you normally use to register a receiver,
-
3:03
in the manifest or in code.
-
3:05
Well it depends on what you are monitoring for and when you need to act on it.
-
3:10
Usually you only want to listen when your activity or your service is running.
-
3:14
In which case, you'll register in code like this.
-
3:16
But occasionally, you may want to monitor for
-
3:18
things whenever they occur, even if your app isn't running.
-
3:21
In those cases, you'll need to register in the manifest.
You need to sign up for Treehouse in order to download course files.
Sign up