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