Connectivity Manager

In this article we are going to learn that how to check internet connectivity in Android apps. There are simple steps to follow to get this working in your apps. Android provide ConnectivityManager class to handle this in Android SDK. This class is very usefull to use if you wants to achieve following featurs in your app.

  1. Monitor network connections (Wi-Fi, GPRS, UMTS, etc.)
  2. Send broadcast intents when network connectivity changes
  3. Attempt to “fail over” to another network when connectivity to a network is lost
  4. Provide an API that allows applications to query the coarse-grained or fine-grained state of the available networks
  5. Provide an API that allows applications to request and select networks for their data traffic

more you can read about connectivity manager here.

Creating Android Studio Project

Create a new android studio project and move to build.gradle of your project to paste below code.

 

Now move to res->layout file of your project and open activity_main.xml file.

this will create a layout for your app like below one.

im

Now open MainActivity.xml file of your project and paste below code.

MainActivity.xml

 

about code will show some errors in the code dont worry about this and create a new java class in your project

MyApp.java

 

After these steps we need to write Broadcast reciever in app which will trigger events in your app whenever you mobile device code connected or disconnected to wifi or mobile dats.

CReciever.java

 

Thats it now we just need to Register this BroadCast Reciever in our AndroidManifest.xml file
AndroidManifest.xml

put this code in application tag. also we need to include following permission in Manifest file.

Thats it we are done with the coding you can test the code now in your device and it will run perfectly as you can see in video in start of post.

You can also download source code from here:Connect

Shares

Leave a Reply

Your email address will not be published. Required fields are marked *