Course Content

If you are facing any issue in playing video please use Firefox browser.

Course Content

Description

Android Broadcast Receiver Example

Now we will see that how we can practically use Broadcast Receiver in Android with Example.

For that, we will first create a new project named 'BroadcastReceiver'. And then we'll open its manifest file 'AndroidManifest.xml'.

We are using Broadcast receiver to check the network state, so for that we are using two permissions,

<uses-permission android:name=""android permission.INTERNET""/>

<uses-permission android:name=""android permission.ACCESS_NETWORK_STATE""/>

Now we will need to create a Receiver inside an application,

<receiver android:name=""com.example.ConnectionReceiver"">

<intent-filter>

<action android:name=""android.net.conn.CONNECTIVITY_CHANGE"" />

</intent-filter>

</receiver>

Now we will see 'activity_main.xml' file in our 'layout' folder. Here we have taken a Relative layout and a Button of text 'Send Broadcast'.

Now we will see how we can design the main activity, for that we will open the 'java' folder, in which we will create a public class ConnectionReceiver,

public class ConnectionReceiver extends BroadcastReceiver

{

@Override

public void onReceive(Context context, Intent intent)

{

if(intent.getAction().equals(""com.example.SOME_ACTION"")

}

Toast.makeText(context,""SOME_ACTION is received"", Toast.LENGTH_LONG).show();

}

 

Intent filter basically specifies that what should be received if we give a particular element.

After that, we'll use the 'else' statement and inside it, we will use 'ConnectivityManager' and also 'getSystemService',

else

{

ConnectivityManager cn=(ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

}

 

Now on Condition basis we will check that, if network is received then what should be done and if network is not received then what should be the action taken,

NetworkInfo activeNetwork=cm.getActiveNetworkInfo();

boolean isConnected=activeNetwork !=null &&

activeNetwork.isConnectedOrConnecting();

if(isConnected)

{

try {

Toast.makeText(context,""Network is connected"", Toast.LENGTH_LONG).show();

}             

catch(Exception e)

{

e.printStackTrace();

}

else

{

Toast.makeText(context,""Network is changed or reconnected"", Toast.LENGTH_LONG).show();

}

}

 

Now we'll design our 'MainActivity.java',

public class MainActivity extends AppCompatActivity

{

ConnectionReceiver receiver;

IntentFilter intentFilter;

 

Now in our activity we will use onResume(),

@Override

protected void onResume()

{

super.onResume();

registerReceiver(receiver,intentFilter);

}

 

After that inner activity, we will use onDestroy(),

@Override

protected void onDestroy()

{

super.onDestroy();

unregisterReceiver(receiver);

}

 

Now we’ll use someMethod(),

void someMethod()

{

Intent intent=new Intent(""com.example.SOME_ACTION"");

sendBroadcast(intent);

}

Now after everything when we will go to our phone's setting when we will connect our cellular data to a network, a notification 'Network is Connected' will appear. And when we will switch off our phone's cellular data ‘Network is changed or reconnected’ notification will appear.

FAQs

Recommended Courses

Share With Friend

Have a friend to whom you would want to share this course?

Download The Apps

App Preview Image
App QR Code Image
Code Scan or Download the app
Google Play Store
297K+ Downloads
App Download Section Circle 1
4.5 Rating
App Download Section Circle 2
10K+ Reviews
App Download Section Circle 3
  • Learn anywhere on the go
  • Get regular updates about your enrolled or new courses
  • Share content with your friends
  • Evaluate your progress through practice tests
  • No internet connection needed
  • Enroll for the webinar and join at the time of the webinar from anywhere