Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
Broadcast Receivers are not able to interact directly with the user interface. To update the UI from a Broadcast Receiver, we must use a tool like LocalBroadcastManager.
Code for copy/paste
public boolean isOnline() {
ConnectivityManager connMgr = (ConnectivityManager)
getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
return (networkInfo != null && networkInfo.isConnected());
}
Documentation
Downloads
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
When we first started talking
about broadcast receivers,
0:00
I mentioned that they aren't able to
interact directly with the user interface.
0:03
We can do it however by using a tool
called Local Broadcast Manager.
0:06
First, let's talk about
what we're going to do.
0:12
When we detect a network change, let's
alert the user with a snack bar message.
0:14
We'll use our same broadcast receiver so
let's go there.
0:18
Let's delete this line that
logs the intent action.
0:24
The first thing we need to do is add
a quick check to see if the network is
0:27
available or
not when a change is detected.
0:30
We'll do that with some
standard code that's
0:33
available from the Android developer site.
0:34
Check out this page on
Managing Network Usage,
0:37
there are details on how
to check a connection.
0:40
And if we scroll down
a little bit further,
0:42
here is a handy method we can use to
check the status of a network connection.
0:45
This is the standard way to check,
so we want to use it.
0:49
I'm going to copy and
paste this and I'll put the code
0:52
in the teacher's notes if you want
to use it for your own copy paste.
0:55
Okay, so let's paste this new
method below, our onReceive method.
0:59
And we see an error
because getSystemService
1:03
is from the context class.
1:06
Now we have the context up here,
so we just need to store it.
1:08
Let's add a field for it,
private Context mContext; and
1:11
we'll set it here in this onReceive
method, mContext = context.
1:16
Now we can prepend it here in
front of getSystemService.
1:24
So let's add mContext.getSystemService.
1:29
To use this network code we need to
add a permission to our manifest.
1:34
So let's open that up and
1:38
we want to add at the top before
we have the application tag.
1:41
We want our permissions, so
1:44
we say uses-permission and
then the name is ACCESS_NETWORK_STATE.
1:46
So I'll just select it here from
the list and close the permission.
1:50
Good, so back in our receiver,
how is this going to work?
1:54
Well we will use the local Broadcast
Manager to send a broadcast intent
1:58
from here.
2:02
And then we'll use that same
manager in main activity
2:02
to receive it and do something.
2:05
We're basically chaining broadcast
intents and receivers together but
2:08
we're narrowing the scope.
2:11
The first one is a system wide broadcast
for the connectivity change itself.
2:13
And then the second one is
an app broadcast that's going to
2:17
send this information to our activity.
2:21
So first here in onReceive
we need a new intent.
2:23
Intent localIntent = new Intent and
then for
2:26
the action let's create a custom
one called NOTIFY_NETWORK_CHANGE.
2:31
Let's use our quick fix Alt+Enter
to create a constant field.
2:38
There we go.
And we wanna make sure we set String here
2:42
and let's give it the same
value as the name itself.
2:46
Remember this is just a key
that needs to be unique.
2:49
We also want it to be public,
2:53
so I'm gonna change this because we're
gonna reference it from MainActivity.
2:54
While we're up here, we're going to
need another one for the extras.
2:59
So let's add, public static final String,
3:01
let's call this EXTRA_IS_CONNECTED and
set it = the same value.
3:05
So now back here in onReceive, we want
to get that connection information and
3:16
put it as an extra, LocalIntent.putEXTRA.
3:20
We use the name EXTRA_IS_CONNECTED and
we can use our new method isOnline.
3:23
So it's going to put a Boolean
value in there for us.
3:28
And now we can send a local broadcast.
3:31
Start typing local broadcast manager.
3:33
And this is a singleton, so
we can access it with getInstance.
3:36
It needs a context and we have one,
so we can pass in (context).
3:40
And now we want to use this
to send a Broadcast Intent.
3:44
So we add sendBroadcast and for
the intent we'll pass in (localIntent).
3:48
No need to complete the receiver
half in MainActivity.
3:55
So let's open that up.
3:58
Just like before,
we need to register it in code, but
3:59
we need another BroadcastReceiver.
4:02
So let's add it right here at the bottom.
4:05
Scroll all the way down,
4:06
in here before the last brace,
add a new private BroadcastReceiver.
4:08
Lets name it mLocalReceiver, and
set it equal to a new BroadcastReceiver.
4:14
And there we go, we have our onReceive and
we can add a semicolon at the end.
4:21
Before we do anything here
in the onReceive method,
4:25
let's register this new receiver
in the onResume method.
4:28
So scroll back up, here's on resume.
4:32
And just like in the manifest where
we can have multiple intent filters,
4:34
we can have multiple
ones defined in code too.
4:39
So here's a second intent filter.
4:41
And we'll name this customFilter.
4:44
And we'll set it equal to
a new IntentFilter and
4:48
here we can use our new constant,
4:51
NetworkConnectionReceiver.NOTIFY_NETWORK_-
CHANGE.
4:54
This line is bumping off my screen,
so I'm gonna drop it down here so
4:58
we can see everything.
5:01
Next we want to use the same
LocalBroadcastManager with the context,
5:04
getInstance.
5:09
And this to register our new
receiver with this filter.
5:10
So we call .registerReceiver and
we pass in the (customFilter).
5:13
Oops I forgot, this needs two parameters.
5:19
The first one is the receiver itself,
5:21
so we're registering this receiver for
this filter.
5:23
Once again I'm going to drop this down.
5:27
So now we can go back
down to mLocalReceiver.
5:30
Let's get the value we
passed in as a Boolean.
5:34
Boolean isConnected = and then from the
intent we want to call getBooleanExtra.
5:36
The name is
5:44
NetworkConnectionReceiver.EXTRA_IS_CONNEC-
TED.
5:44
And when we call getBoolean we
need to pass in a default value.
5:48
So let's pass in false.
5:51
Once again, will drop this down so
we can see most of it at least.
5:53
Now we can check if isConnected and
we can display a message.
5:59
Let's display a snackbar.make.
6:04
The view, we have mRootLayout available.
6:06
And we'll say "Network is connected".
6:09
And we'll do Snackbar.LENGTH_LONG.show.
6:13
For the else condition,
you wanna do basically the same thing.
6:21
We'll say Snackbar.make(mRootLayout,
"Network is disconnected.",
6:23
Snackbar.LENGTH_LONG).show.
6:31
Okay, let's run this,
and see what happens.
6:39
Okay and right away we get a message
that the network is connected.
6:44
And we can change to airplane mode and
watch for another step bar.
6:48
There we go,
we see the network is disconnected.
6:53
And if we change back,
6:56
turn airplane mode back off, we'll see
that the network is connected again.
6:58
Cool, it's working.
7:02
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up