Android Broadcast Receiver Notification Tutorial

In this tutorial, you will learn how to implement a Broadcast Receiver in your Android application. Broadcast Receiver is completely asynchronous, and is considered to be a foreground process and will be kept running with the Android system. We will be creating an activity that consists of a ToggleButton and a CheckBox that switches the Wifi and Broadcast Receiver on or off. A notification will be shown if the Broadcast Receiver is turned on while switching the Wifi on or off. So lets begin…

Download the Latest Support Library

Download the latest support library revision 13

Link : http://developer.android.com/tools/extras/support-library.html

Replace the old support library (android-support-v4.jar) with the new support library in your project.

Create a new project in Eclipse File > New > Android Application Project. Fill in the details and name your project BroadcastTutorial.

Application Name : BroadcastTutorial

Project Name : BroadcastTutorial

Package Name : com.androidbegin.broadcasttutorial

Open your MainActivity.java and paste the following code.

MainActivity.java

In this activity, we have created a toggle and a checkbox button. On toggle button click will switch on or off the wifi services and on checkbox check or uncheck will start or stop the broadcast receiver.

Next, create an XML graphical layout for your MainActivity. Go to res > layout > Right Click on layout > New > Android XML File

Name your new XML file activity_main.xml and paste the following code.

activity_main.xml

Output:

BroadcastReceiver Main XML

Next, create a Broadcast Receiver class. Go to File > New > Class and name it BroadcastManager.java. Select your package named com.androidbegin.broadcasttutorial and click Finish.

Open your BroadcastManager.java and paste the following code.

BroadcastManager.java

Broadcast Receiver checks for network availability asynchronously. So if the wifi is turned on or off, the application will be notified immediately and a notification will be shown.

Output:

Broadcast Receiver Notification

Next, create an activity to view results on notification click. Go to File > New > Class and name it NotificationView.java. Select your package named com.androidbegin.broadcasttutorial and click Finish.

Open your NotificationView.java and paste the following code.

NotificationView.java

Next, create an XML graphical layout to view a notification. Go to res > layout > Right Click on layout > New > Android XML File
Name your new XML file notificationview.xml and paste the following code.

notificationview.xml

Next, change the application name and texts. Open your strings.xml in your res > values folder and paste the following code.

strings.xml

In your AndroidManifest.xml, we need to declare an activity, a broadcast manager and permissions to allow the application to change the wifi state and ability to access network state. Open your AndroidManifest.xml and paste the following code.

AndroidManifest.xml

Output:

Broadcast Receiver ScreenShot

Source Code 

BroadcastTutorial (846.9 KiB, 363 downloads)